How to query list of objects with array as an argu

2019-02-16 12:15发布

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条回答
Summer. ? 凉城
2楼-- · 2019-02-16 12:51

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]
  }
}
查看更多
登录 后发表回答