React-apollo: Is there possibility to batch queries using useQuery hook?

Created on 31 Aug 2019  路  7Comments  路  Source: apollographql/react-apollo

e.g.

const bookQuery = gql`
  query ($bookId: ID!) {
    book(id: $bookId) {
      id
      author
    }
  }
`
const carQuery = gql`
  query ($carId: ID!) {
    car(id: $carId) {
      id
      name
    }
  }

//useQuery(bookQuery, variables: {bookId: 1})
//useQuery(carQuery, variables: {carId: 2})
const { data, loading } = combineQuery([
{query: bookQuery, variables: {bookId: 1}},
{query: carQuery, variables: {carId: 2}}
])
data.bookQuery ...
data.carQuery...

I would like to combine two query to one. Is there possibility to do this?
If it is not implemented maybe we can implement it.
This can reduce the request times. we can use one query to get what we want even when we have multiple defined queries.

tried to solved by graphql-tag but the related feature is not implemented yet.
https://github.com/apollographql/graphql-tag/issues/169

Most helpful comment

I think this is exactly the use case with graphql ? I mean you can make a query like this

query ($bookId: ID!, $carId: ID!) {
    book(id: $bookId) {
      id
      author
    }
   car(id: $carId) {
      id
      name
    }
  }

All 7 comments

I think this is exactly the use case with graphql ? I mean you can make a query like this

query ($bookId: ID!, $carId: ID!) {
    book(id: $bookId) {
      id
      author
    }
   car(id: $carId) {
      id
      name
    }
  }

yup but I want to use existed queries for reusability. Above example works but I have to write that GraphQL query if the combination is different.

If the goal is to do both queries in one network request, maybe the batch-http link will help: https://www.apollographql.com/docs/link/links/batch-http/

@crokobit also just want to say I love your profile pic. Natsume is one of my favorite shows 馃槉

looks like apollo-link-batch-http didn't integrate with react apollo hooks (useQuery....) though.

@crokobit It should work with useQuery

Issues here are reserved for React Apollo bugs. You鈥檒l have a much better chance of getting this answered in Apollo鈥檚 Spectrum community or on Stack Overflow. Thanks!

looks like apollo-link-batch-http didn't integrate with react apollo hooks (useQuery....) though.

Can you provide an example?

Was this page helpful?
0 / 5 - 0 ratings