Support
Quality
Security
License
Reuse
Coming Soon for all Libraries!
Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
🚀 fullstack tutorial 2021,后台技术栈/架构师之路/全栈开发社区,春招/秋招/校招/面试
Should a query in Apollo Client look for the results cached by different queries before making a network request?
todo(id: $id) {
title
completed
}
try {
return client.readFragment({
id: 'Todo:5', // The value of the to-do item's unique identifier
fragment: gql`
fragment TodoFragment on Todo {
id
title
completed
}
`,
});
} catch(_e) { // if no fragment is found there will be an error
client.query(QUERY, variables: { id: 5})
}
todos {
id
title
completed
}
todo(id: $id) {
id
title
completed
}
-----------------------
todo(id: $id) {
title
completed
}
try {
return client.readFragment({
id: 'Todo:5', // The value of the to-do item's unique identifier
fragment: gql`
fragment TodoFragment on Todo {
id
title
completed
}
`,
});
} catch(_e) { // if no fragment is found there will be an error
client.query(QUERY, variables: { id: 5})
}
todos {
id
title
completed
}
todo(id: $id) {
id
title
completed
}
-----------------------
todo(id: $id) {
title
completed
}
try {
return client.readFragment({
id: 'Todo:5', // The value of the to-do item's unique identifier
fragment: gql`
fragment TodoFragment on Todo {
id
title
completed
}
`,
});
} catch(_e) { // if no fragment is found there will be an error
client.query(QUERY, variables: { id: 5})
}
todos {
id
title
completed
}
todo(id: $id) {
id
title
completed
}
-----------------------
todo(id: $id) {
title
completed
}
try {
return client.readFragment({
id: 'Todo:5', // The value of the to-do item's unique identifier
fragment: gql`
fragment TodoFragment on Todo {
id
title
completed
}
`,
});
} catch(_e) { // if no fragment is found there will be an error
client.query(QUERY, variables: { id: 5})
}
todos {
id
title
completed
}
todo(id: $id) {
id
title
completed
}
Apollo - endpoint http not found in root project
mkdir -p app/src/main/graphql/com/bangenergy/ ./gradlew :app:downloadApolloSchema -P com.apollographql.apollo.endpoint='http://localhost:4000/graphql/' -P com.apollographql.apollo.schema='src/main/graphql/com/bangenergy/schema.json'
-----------------------
gradlew :app:downloadApolloSchema --endpoint="http://****REPLACE_WITH_YOUR_ENDPOINT/graphql/" --schema="src/main/graphql/com/example/programbuilder/schema.json"
Apollo GraphQL tutorial: "GraphQLError: Cannot query field \"id\" on type \"LaunchConnection\"."
type LaunchConnection {
cursor: String!
hasMore: Boolean!
launches: [Launch]!
}
type Query {
launches: LaunchConnection!
}
query GetLaunches {
launches {
id
mission {
name
}
}
}
query GetLaunches {
launches {
launches {
id
mission {
name
}
}
}
}
-----------------------
type LaunchConnection {
cursor: String!
hasMore: Boolean!
launches: [Launch]!
}
type Query {
launches: LaunchConnection!
}
query GetLaunches {
launches {
id
mission {
name
}
}
}
query GetLaunches {
launches {
launches {
id
mission {
name
}
}
}
}
-----------------------
type LaunchConnection {
cursor: String!
hasMore: Boolean!
launches: [Launch]!
}
type Query {
launches: LaunchConnection!
}
query GetLaunches {
launches {
id
mission {
name
}
}
}
query GetLaunches {
launches {
launches {
id
mission {
name
}
}
}
}
QUESTION
Should a query in Apollo Client look for the results cached by different queries before making a network request?
Asked 2020-Nov-23 at 17:57I'm trying to figure out how queries in Apollo Client are supposed to interact with the cache.
Specifically, I want to know if we run a query that fetches all todos:
todos {
title
completed
}
And then later we run a query that fetches a single todo that was already fetched by the todos query and requests the exact same fields:
todo(id: $id) {
title
completed
}
Should the second query a) fetch the data from the cache, or b) make a network request?
My assumption was that it would be case A. This is based on this quote from an official Apollo blog post:
https://www.apollographql.com/blog/demystifying-cache-normalization/
For example, if we were to:
- Perform a GetAllTodos query, normalizing and caching all todos from a backend
- Call GetTodoById on a todo that we had already retrieved with GetAllTodos
...then Apollo Client could just reach into the cache and get the object directly without making another request.
However, in my app I kept getting case B, it was always making an additional network request even though I had already requested all the data in a different query.
I assumed that I was doing something wrong, so I checked out this Apollo Full-stack Tutorial repo (https://github.com/apollographql/fullstack-tutorial) and updated the LaunchDetails query to only request the same data that was already requested in the GetLaunchList query. This replicated the same scenario I detailed above with the todos.
The queries now look like this:
export const GET_LAUNCHES = gql`
query GetLaunchList($after: String) {
launches(after: $after) {
cursor
hasMore
launches {
...LaunchTile
}
}
}
${LAUNCH_TILE_DATA}
`;
export const GET_LAUNCH_DETAILS = gql`
query LaunchDetails($launchId: ID!) {
launch(id: $launchId) {
...LaunchTile
}
}
${LAUNCH_TILE_DATA}
`;
I ran the application, and found that a new network request was made for the LaunchDetails query, even though all the required data was already in the cache after the GetLaunchList query was run.
I haven't been able to find any answer to this in the documentation, and the results I'm seeing from the example tutorial app seem to be at odds with the quote from the blog piece above.
Is it the case that a query will only look to the cache if the query has already been run before? Can it not fetch cached data if that data was cached by a different query? Am I missing something?
ANSWER
Answered 2020-Nov-23 at 17:57the query will make a network query.
todo(id: $id) {
title
completed
}
Apollo cache isn't very smart. It is just storage. You need to read/write for more complicated operations manually.
The reason for this is Apollo doesn't know about your schema and data structure. It doesn't know that todo(id: $id)
will do DB search by, so it can't optimize to look in the cache.
If you don't want a second fetch, you have to implement your data fetch structure with fragment:
try {
return client.readFragment({
id: 'Todo:5', // The value of the to-do item's unique identifier
fragment: gql`
fragment TodoFragment on Todo {
id
title
completed
}
`,
});
} catch(_e) { // if no fragment is found there will be an error
client.query(QUERY, variables: { id: 5})
}
The way Apollo cache is that if you do two queries:
todos {
id
title
completed
}
todo(id: $id) {
id
title
completed
}
If you list a list of todos and load the second one - it will update the todo data.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit