最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - GraphQL List or single object - Stack Overflow

programmeradmin0浏览0评论

I got the following "problem". I am used to having an API like that.

/users
/users/{id}

The first one returns a list of users. The second just a single object. I would like the same with GraphQL but seem to fail. I got the following Schema

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      users: {
        type: new GraphQLList(userType),
        args: {
          id: {type: GraphQLString}
        },
        resolve: function (_, args) {
          if (args.id) {
            return UserService.findOne(args.id).then(user => [user]);
          } else {
            return UserService.find()
          }
        }
      }
    }
  })
});

How can I modify the type of users to either return a List OR a single object?

I got the following "problem". I am used to having an API like that.

/users
/users/{id}

The first one returns a list of users. The second just a single object. I would like the same with GraphQL but seem to fail. I got the following Schema

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      users: {
        type: new GraphQLList(userType),
        args: {
          id: {type: GraphQLString}
        },
        resolve: function (_, args) {
          if (args.id) {
            return UserService.findOne(args.id).then(user => [user]);
          } else {
            return UserService.find()
          }
        }
      }
    }
  })
});

How can I modify the type of users to either return a List OR a single object?

Share Improve this question asked Sep 21, 2016 at 7:24 DanielDaniel 6092 gold badges7 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

You shouldn't use the same field for different purposes. Make two fields instead: One for a single object, and another for a list of objects.

It's better practice and better for testing

fields: {
    user: {
        type: userType,
        description: 'Returns a single user',
        args: {
            id: {type: GraphQLString}
        },
        resolve: function (_, args) {
            return UserService.findOne(args.id);
        }
    },
    users: {
        type: new GraphQLList(userType),
        description: 'Returns a list of users',
        resolve: function () {
            return UserService.find()
        }
    }
}

The above answer is correct, the usual approach is to add singular and plural form of queries. However, in large schema, this can duplicate a lot of logic and can be abstracted a little bit for example with Node interface and node, nodes queries. But the nodes query is usually applied with ids as argument (in Relay viz node Fields), but you can build your own abstracted way for fetching so that you have just nodes with some argument for type and based on that you can say what type of list to fetch. However, the simpler approach is to just duplicate the logic for every type and use singular and plural form of query and do the same type of queries as above or in this code snippet for every type. For more detail explanation on implementing GraphQL list modifiers in queries or even as an input for mutations. I just published the article on that.

发布评论

评论列表(0)

  1. 暂无评论