How to query list of objects with array as an argument in GraphQL

做~自己de王妃 提交于 2019-12-18 14:38:30

问题


I'm trying to query a list of objects having array of IDs. Something similar to following SQL query:

SELECT name FROM events WHERE id IN(1,2,3,...);

How do I achieve this in GraphQL?


回答1:


You can definitely query with an array of values! Here's what the query itself would look like:

{
  events(containsId: [1,2,3]) {
    ...
  }
}

And the type would look something like:

const eventsType = new GraphQLObjectType({
  name: 'events',
  type: // your type definition for events,
  args: {
    containsId: new GraphQLList(GraphQLID)
  },
  ...
});

If you wanted to parameterize this query, here's an example of that:

{
  query: `
    query events ($containsId: [Int]) {
      events(containsId: $containsId) {
        id
        name
      }
    }
  `,
  variables: {
    containsId: [1,2,3]
  }
}


来源:https://stackoverflow.com/questions/40644296/how-to-query-list-of-objects-with-array-as-an-argument-in-graphql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!