2017-02-22 142 views
0

看来我没有得到一些基本的东西与graphql。graphql抛出未知的参数错误

我想通过它的id得到一个用户,而这又是另一个查询的结果。在这种情况下,查询某些会话数据。

我不明白为什么发生错误。

这里是我的代码:

{ 
    session(key: "558fd6c627267d737d11e758f1ae48cae71fc9b584e2882926ad5470c88d7c3ace08c9c7") { 
     userId 
     expires 
     user(id: userId) { 
      name 
     } 
    } 
} 

我也得到

对类型的字段 “用户”, “会议” 未知参数 “ID”

我的模式是这样的:

type Session { 
    userId: String, 
    expires: String, 
    user: User 
} 

type User { 
    _id: String 
    name: String 
    email: String 
    firstName: String 
    lastName: String 
} 

type Query { 
    session(key: String!): Session 
    user(id: String!): User 
} 

附录Feb 23 2017

我很抱歉,在我的初始文章中,我对相应的解析器没有足够的了解。是的,我的解决方案是定义和e。 G。如果我不添加用户,该查询适用于会话。

这里是我的根:

{ 
    Query: { 
     async user(parentValue, args, req) { 
      let user = await adapters.users.byId(args.id); 
      return user; 
     }, 
     async session(parentValue, args, req) { 
      let session = await adapters.session(args.key); 
      let userId = session.session.userId; 
      let expires = session.expires; 
      return {userId: userId, expires: expires}; 
     } 
    } 

} 

回答

0

您需要创建Session式现场user一些resolver功能,因为GraphQL不知道如何返回user。在graphql-js它看起来像

const Session = new GraphQLObjectType({ 
    name: 'Session', 
    fields: { 
     userId: { type: GraphQLString }, 
     expires: { type: GraphQLString }, 
     user: { 
      type: User, // it is your GraphQL type 
      resolve: function(obj, args, context){ 
       // obj is the session object returned by ID specified in the query 
       // it contains attributes userId and expires 
       // however graphql does not know you want to use the userId in order to retrieve user of this session 
       // that is why you need to specify the value for field user in type Session 
       return db.User.findById(obj.userId).then(function(user){ 
        return user; 
       }); 
      } 
     } 
    } 
}); 

距离Node.js的只是一个简单的例子来告诉你如何可能会返回user实例。您可以指定如何检索每个GraphQL类型中的每个字段的值(每个字段可以具有它自己的resolve函数)。

我建议你阅读有关root and resolvers

编辑

我会告诉你一个例子,你如何处理这样的情况GraphQL文档。让我们考虑两种类型:UserSession

type User { 
    _id: String 
    name: String 
} 

type Session { 
    expires: String 
    user: User 
} 

如果你希望你的查询返回Session对象连同它的User,你并不需要在Session类型的userId领域。 您可以通过两种方式解决这种情况。首先是为session查询使用单个解析器。

{ 
    Query: { 
     async session(parentValue, args, req) { 
      let session = await adapters.session(args.key); 
      // now you have your session object with expires and userId attributes 
      // in order to return session with user from query, you can simply retrieve user with userId 
      let user = await adapter.users.byId(session.userId); 
      return { expires: session.expires, user: user } 
     } 
    } 
} 

这是您的第一个选项。使用该解决方案,您可以返回session对象,其中user通过单个查询分配给它,并且在Session类型的任何字段上没有额外的resolve方法。第二种解决方案是一个我先前的研究显示,当您使用Session类型的字段userresolve方法 - 所以你只要告诉GraphQL应该如何获取此字段的值。

+0

好了,我的错,我想这不用说,还有每个查询解析器。如果我没有解析器,程序甚至会在这个错误消息之前崩溃。但为了完整和明确,我会将解析器添加到上面的代码中。 如果我只是查询没有用户的会话,该查询就可以工作。 一件事:我使用Apollo的graphql工具的架构定义,所以它的定义是基于GraphQL类型语言。看看2017年2月23日的附录 – LongHike

+0

我编辑了答案,为您的案例提供示例 – piotrbienias

+0

非常感谢。这工作! – LongHike

相关问题