Search code examples
reactjsgraphqlapolloreact-apollo

How to send a similar GraphQL query with diffirent ID's using Apollo?


I'm using Apollo GraphQL in my React app.

For instance, I have a query

{
  files(id: 1) {
    name
  }
}

I've got 10 files and I want to get just one field.

As far as I know, I can't create something like this

{
  files(id: 1) {
    name
  }
  files(id: 2) {
    name
  }
}

It gives me an error.

I'm ready to iterate through id's [1,2,3,..10], but it looks like it's impossible using useQuery.

I'm stuck. Appreciate any help!


Solution

  • You can do it in 1 query

    {
      file1: files(id: 1) {
        name
      }
      file2: files(id: 2) {
        name
      }
    }
    

    or something like this if it support multiple parameter

    query findFiles($files: [Int!]!) {
      files(ids: $id) {
        name
      }
    }