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

问题描述:

我一直有很多麻烦试图让突变的工作。 鉴于此GraphQL架构,任何人都可以帮我创建一个简单的创建用户变异?我不明白我错过了什么。我知道它会从GraphQL服务器抛出一个400错误,并且它不会触发解析函数。React本地返回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: "[email protected]", businessDisplayName: "businessDN"}) { 
    id 
    email 
    business 
    businessDisplayName 
    } 
} 

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

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

我终于解决了这一问题。试图了解问题的根源,因此我使用了一个新的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 }) 

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