'React Native - Apollo client and partial fetch in multiple queries
I would like to know if Apollo client.query can handle multiple query and fetch only missing data, which are not in cache.
Client is defined like this:
import { ApolloClient, InMemoryCache } from "@apollo/react-hooks";
export const client = new ApolloClient({
uri: 'http://localhost:4000/graphql',
cache: new InMemoryCache(),
});
and sample query like this:
import { gql } from "@apollo/react-hooks";
export const GET_CLASS = gql`
query GetClass($courseId: ID!, $lessonId: ID!){
course(id: $courseId) {
id
title
author
description
}
lesson(id: $lessonId) {
id
title
teacher
}
}
`
When I call my first query:
client.query({
query: GET_CLASS,
variables: { courseId:111, lessonId:222, },
})
I see on backend requests for course with id 111 and lesson with id 222, which is OK. When I recall query with the same variables, data are returned from cache (no request on backend), which is OK.
But when I then call this query:
client.query({
query: GET_CLASS,
variables: { courseId:333, lessonId:222, },
})
I see on backend requests for course with id 333 and also lesson with id 222.
I thought that Apollo will handle this and it will get:
- course with id 333 from backend
- lesson with id 222 from cache
I tried returnPartialData:true, but it just got stuck.
I tried many combinations and it looks like, that if there are missing data for at least one object, it will send whole query to backend.
Is it possible to tell client, that he should request only for missing data from query?
Note: I don't want to query lesson and course separately. This is just a small sample from more complex queries.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
