2017-10-17 13 views
1

나는 돌연변이를 일으키려고 많은 어려움을 겪어왔다. 이 GraphQL 스키마가 주어지면 누구나 을 작성하면 간단한 사용자 생성 mutation을 만들 수 있습니까? 내가 빠진 것을 이해하지 못한다. 나는 그것이 GraphQL 서버로부터 400 에러를 던지는 지점에 도달하고, resolve 함수를 실행하지 않는다.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를 쿼리 할 수 ​​없습니다". Look up을 살펴보면 객체를 변경할 수 있으려면 GraphQL 유형에 해당 필드가 있어야합니다. 그래서 나는 그것을 추가했다.

학습 한 내용 : 반응 릴레이 네트워크 레이어을 사용하는 것이 좋습니다.


자세한 내용 :

은 여기 내 코드입니다 :

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

물론 당신은 항상 캐시를 청소하고 패키저를 다시 시작해야합니다.