ReasonJun

GraphQL : Fetch Policy 본문

Backend/GraphQL

GraphQL : Fetch Policy

ReasonJun 2023. 9. 6. 00:48
728x90

In GraphQL, a fetch policy is a strategy for determining how to fetch data from the server. The fetch policy is specified when a query is made, and it can be used to control whether the data is fetched from the cache, from the server, or from both.

 

There are five main fetch policies in GraphQL:

  • cache-first : This policy checks the cache first, and only fetches data from the server if the data is not found in the cache.

⇒ It is best used mainly when data does not change frequently.

  • network-only : This policy always fetches data from the server, and does not check the cache. But set cache.
  • cache-and-network : This policy checks the cache first, and then fetches data from the server if the data is not found in the cache. The server data is then merged with the cached data and returned to the client.

⇒ Because a request is sent to the server regardless of whether there is a cache, it quickly shows what is in the cache and shows data that may be changed later.

⇒ It is good to use when importing data that changes frequently in real time.

  • fetch-first : This policy always fetches data from the server, and then caches the data for future requests.
  • no-cache : This policy does not cache data, and always fetches data from the server. And does not set cache.

The default fetch policy in GraphQL is cache-first. This means that queries will first check the cache for the requested data, and only fetch data from the server if the data is not found in the cache.

 

The fetch policy can be specified on a per-query basis, or it can be set globally for all queries. To specify the fetch policy on a per-query basis, you can use the fetchPolicy option when making the query. For example:

const { data } = await client.query({
  query: GET_USERS,
  fetchPolicy: "cache-and-network"
});

To set the fetch policy globally, you can use the fetchPolicy option when creating the client. For example:

const client = new ApolloClient({
  fetchPolicy: "cache-and-network"
});

The fetch policy can be a helpful way to improve the performance of GraphQL queries. By caching data, you can reduce the number of network requests that need to be made, which can improve the responsiveness of your application. However, it is important to use the fetch policy carefully to avoid caching stale data.

 

Here are some additional things to keep in mind when using fetch policies in GraphQL:

  • The fetch policy can be used to control how data is cached. For example, you can use the cache-first policy to cache data for a specified amount of time, or you can use the no-cache policy to never cache data.
  • The fetch policy can be used to handle stale data. For example, you can use the cache-and-network policy to fetch data from the server if the data in the cache is stale.
  • The fetch policy can be used to handle concurrent requests. For example, you can use the fetch-first policy to ensure that all requests for the same data get the same results.
728x90

'Backend > GraphQL' 카테고리의 다른 글

GraphQL : Normalization  (0) 2023.09.06
GraphQL : Cache  (0) 2023.09.06
GraphQL : Apollo  (0) 2023.09.05
GraphQL : Resolver  (0) 2023.09.04
GraphQL : Concept  (0) 2023.09.04
Comments