2017-10-17 111 views
1

我一直有很多麻煩試圖讓突變的工作。 鑑於此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 
    } 
} 

回答

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 }) 

當然,你總是需要清理緩存,並重新啓動打包。