Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 385
Rafael Sedrakyan Avatar asked Nov 16 '16 23:11

Rafael Sedrakyan


People also ask

How do you pass an argument in GraphQL query?

When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use $ syntax to define variables in your query, and pass the variables as a separate map. . then(data => console.

How do you pass two arguments in GraphQL query?

Multiple arguments can be used together in the same query. For example, you can use the where argument to filter the results and then use the order_by argument to sort them.

What is __ Typename in GraphQL?

The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).

Are there any restrictions on arguments in GraphQL queries?

Just like fields, there are no type restrictions. Arguments can be of different types, too. The result of the previous query with the id argument will look like the following: One possible pitfall here is that GraphQL queries look almost the same for single items and lists of items.

What is the GraphQL query language?

If you've seen a GraphQL query before, you know that the GraphQL query language is basically about selecting fields on objects. So, for example, in the following query: For the object returned by hero, we select the name and appearsIn fields

How do you pass arguments in GraphQL?

Unlike languages like JavaScript and Python where functions take a list of ordered arguments, all arguments in GraphQL are passed by name specifically. In this case, the length field has one defined argument, unit. Arguments can be either required or optional.

What are the list types available in GraphQL?

GraphQL has list types which are ordered lists containing items of other types. The following examples use the GraphQL Schema Definition Language (SDL). Fields may return a single scalar value (eg String ), or a list of scalar values (eg, [String], a list of strings):


2 Answers

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]
  }
}
like image 154
browserless Avatar answered Oct 17 '22 16:10

browserless


I just do this:

query nameOfYourQuery {
  allEvents(filter: { id: { in: [1,2,3] } }) {
    nodes {
      name
    }
  }
}

If the array is a variable, then it would look like this (in Gatsby, at least):

query nameOfYourQuery($arrayOfID: [String]) {
  allEvents(filter: { id: { in: $arrayOfID: [String] } }) {
    nodes {
      name
    }
  }
}
like image 1
Félix Paradis Avatar answered Oct 17 '22 18:10

Félix Paradis