React本地返回400错误请求的中继突变?

时间:2017-10-17 00:09:10

标签: react-native graphql relayjs relay

我一直在努力让突变发挥作用。 鉴于此GraphQL架构,任何人可以帮助我创建一个简单的创建用户变异吗?我不明白我错过了什么。我得到它从GraphQL服务器抛出400错误,它不会触发解析功能。

var userType = new GraphQLObjectType({
  name: 'User',
  description: 'User creator',
  fields: () => ({
    id: {
      type: new GraphQLNonNull(GraphQLString),
      description: 'The id of the user.'
    },
    email: {
      type: GraphQLString,
      description: 'The email of the user.'
    },
    business: {
      type: GraphQLString,
      description:
        'The name of the business of the user as the app refers to it.'
    },
    businessDisplayName: {
      type: GraphQLString,
      description: 'The name of the business of the user as they typed it in.'
    },
    trips: {
      type: new GraphQLList(tripType),
      description: 'The trips of the user, or an empty list if they have none.',
      resolve: (user, params, source, fieldASTs) => {
        var projections = infoToProjection(fieldASTs)
        return Trip.find(
          {
            _id: {
              // to make it easily testable
              $in: user.trips.map(id => id.toString())
            }
          },
          projections,
          function(err, docs) {
            return docs
          }
        )
      }
    }
  })
})


var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'root',
    fields: {
      trips: {
        type: new GraphQLList(tripType),
        resolve: function() {
          return Trip.find({})
        }
      },
      users: {
        type: new GraphQLList(userType),
        resolve: function() {
          return User.find({})
        }
      },
      user: {
        type: userType,
        args: {
          id: {
            name: 'id',
            type: new GraphQLNonNull(GraphQLString)
          }
        },
        resolve: (root, { id }, source, fieldASTs) => {
          return User.findOne(
            { _id: id },
            infoToProjection(fieldASTs),
            function(err, doc) {
              return doc
            }
          )
        }
      },
      trip: {
        type: tripType,
        args: {
          id: {
            name: 'id',
            type: new GraphQLNonNull(GraphQLString)
          }
        },
        resolve: (root, { id }, source, fieldASTs) => {
          var projections = infoToProjection(fieldASTs)
          return Trip.findOne({ _id: id }, projections, function(err, doc) {
            return doc
          })
        }
      }
    }
  }),

  // mutation
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: {
      createUser: {
        name: 'createUser',
        type: userType,
        args: {
          input: { type: new GraphQLInputObjectType({
            name: 'user',
            fields: {
              business: { type: GraphQLString },
              email: { type: GraphQLString },
              businessDisplayName: { type: GraphQLString }
            }
          })
        }},
        resolve: (parentValue, args) => {
          let user = new User({ ...args.input })
          user.save()
          return user
        }
      }
  })
})

export var getProjections = infoToProjection
export default schema

这适用于使用以下查询或突变的GraphiQL:

mutation {
  createUser(input:{business:"business", email: "e@mai.l", businessDisplayName: "businessDN"}) {
    id
    email
    business
    businessDisplayName
  }
}

fragment UserFragment on User {
    id
    business
    businessDisplayName
    trips{
      title
    }
}

{
    hideya: user(id: "someid") {
    ...UserFragment
  }  
}

1 个答案:

答案 0 :(得分:0)

我终于解决了这个问题。试图了解问题的根源,因此我使用新的NetworkLayer来启用适当的日志记录和有意义的错误消息。然后当我的突变失败时抛出一个错误。错误消息为:“无法查询字段clientMutationId ”。看了一下,发现能够改变你需要在GraphQL类型上拥有该字段的对象。所以我加了它。

经验教训:强烈建议您使用 react-relay-network-layer

更多详情:

这是我的代码:

import {
  RelayNetworkLayer,
  urlMiddleware,
  batchMiddleware,
} from 'react-relay-network-layer';

Relay.injectNetworkLayer(new RelayNetworkLayer([
  batchMiddleware({
    batchUrl: 'http://localhost:3000/graphql',
  }),
  urlMiddleware({
    url: 'http://localhost:3000/graphql',
  }),
]));

注意:这会启用日志记录,默认情况下它是一个简单的console.log。

以下是我抛出错误的方法:

const params = {
        email: email.toLowerCase(),
        businessDisplayName: business,
        business: business.toLowerCase()
      }
      var onSuccess = () => {
        console.log('Mutation successful!')
      }
      var onFailure = transaction => {
        var error = transaction.getError() || new Error('Mutation failed.')
        console.error(error)
      }

      Relay.Store.commitUpdate(new FindOrCreateUser({ user: { ...params } }), { onFailure, onSuccess })

当然,您总是需要清理缓存并重新启动打包程序。

相关问题