首页 文章

graphql抛出未知参数错误

提问于
浏览
0

好像我没有得到graphql的基本功能 .

我试图通过它的id来获取用户,而id又是另一个查询的结果 . 在这种情况下,对某些会话数据进行查询 .

我不明白为什么会发生错误 .

这是我的代码:

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

我明白了

Unknown argument "id" on field "user" of type "Session"

我的架构如下所示:

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
}

Addendum 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};
        }
    }

}

1 回答

  • 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文档

    EDIT

    我将向您展示如何处理这种情况 . 我们考虑两种类型: 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 方法 . 第二个解决方案是我之前显示的那个,你在 user 的字段 user 上使用 resolve 方法 - 所以你只需告诉GraphQL它应该如何获得该字段的值 .

相关问题