GraphQL - 向 ObjectType 传递参数

Tho*_*mas 2 node.js express graphql graphql-js

我正在使用GraphQL并且它工作得很好,但是,我似乎无法弄清楚如何将参数传递到fields我的Event GraphQLObjectType.

我希望能够将currentUserId(通过令牌给我的)传递到 中,Event GraphQLObjectType以便我可以添加isAttending属性。

我已附上代码,其中包含我基本上想做的事情的注释:

const Event = new GraphQLObjectType({
  name: 'Event',
  description: 'This represents an Event',
  fields: (currentUserId) => { // currentUserId is the parameter I would like to pass in 
    return {
      id: {
        type: GraphQLInt,
        resolve (event) {
          return event.id;
        }
      },
      title: {
        type: GraphQLString,
        resolve (event) {
          return event.title;
        }
      },
      attendees: {
        type: new GraphQLList(User),
        resolve (event) {
          return event.getAttendees()
        }
      },
      // this is what I would like to do
      isAttending: {
        type: GraphQLBool,
        resolve (event) {
          return event.getAttendees({
            where: {
              id: currentUserId // that's the parameter I would like pass in
            }
          }).then(attendee => {
            return (attendee.length > 0 ? true : false);
          )};
        }
      }
      // end of what I'm trying to do //
    };
  }
});

const Query = new GraphQLObjectType({
  name: 'Query',
  description: 'Root query object',
  fields: () => {
    return {
      events: {
        type: new GraphQLList(Event),
        args: {
          id: {
            type: GraphQLInt
          }
        },
        resolve (root, args) {
          // here is the parameter I would like to pass to the event object
          let currentUserId = root.userId; 
          ////////

          return Db.models.event.findAll({ where: args });
        }
      },
      ...
Run Code Online (Sandbox Code Playgroud)

更新

我不能这样做的原因data.currentUserId = root.userId是因为当我返回 a 时它不可见collection of event objects,因为传递到 my 的Event GraphQLOBjectType只是{event}对象。

当我这样做data.currentUserId并且里面有一个对象数组时,它看起来data像这样:

[{objects}, currentUserId: 1] 
Run Code Online (Sandbox Code Playgroud)

与我们想要的相反的是:

[{object, currentUserId: 1}, {anotherObject, currentUserId: 1}] 
Run Code Online (Sandbox Code Playgroud)

如果我想访问currentUserIdin Event GraphQLObject,我唯一能想到的就是循环遍历每个对象并将 currentUserId 添加到它上面,如下所示:

return events.map(event => { 
  event.currentUserId = currentUserId; 
  return event; 
});`
Run Code Online (Sandbox Code Playgroud)

这是最好的解决方案吗?

Lor*_*ave 5

恐怕你做不到。fields没有收到任何参数,因此您也不会发送任何参数。

幸运的是,您可以通过更方便的方式实现这一点。

父类型 ( Query) 在resolve函数中返回的所有内容都在子解析的参数中可见root

const Query = new GraphQLObjectType({
    name: 'Query',
    description: 'Root query object',
    fields: () => ({
            events: {
                type: new GraphQLList(Event),
                args: {
                    id: {
                        type: GraphQLInt
                    }
                },
                resolve (root, args) {

                    return Db.models.event.findAll({ where: args })
                            .then(data => {
                                // pass the parameter here
                                data.currentUserId = root.userId;
                                return data;
                            });
                }
            },
            ...
Run Code Online (Sandbox Code Playgroud)

那么你的Event对象将如下所示:

const Event = new GraphQLObjectType({
    name: 'Event',
    description: 'This represents an Event',
    fields: () => ({

        ...

        isAttending: {
            type: GraphQLBool,
            resolve: (event) => {
                return event.getAttendees({
                    where: {
                        id: event.currentUserId // that's the parameter you've passed through parent resolve
                    }
                }).then(attendee => {
                    return (attendee.length > 0 ? true : false);
                });
            }
        }
    })
});
Run Code Online (Sandbox Code Playgroud)