In the example above, the function uses a session-id header from the original operation request. The cache uses the return value of this function to identify the user who can later access the cached PRIVATE response. Important: If you don't define a sessionId function, PRIVATE responses are not cached at all. To override this default behavior, you can add a directive to an entity's definition: Subgraph schemas contain an _entities root field on the Query type, so all query plans that require entity resolution will have a maxAge of 0 set by default. The gateway will then calculate the cache hint for the overall response based on the most restrictive settings among all of the responses received from the subgraphs involved in query plan execution. An Apollo Server-based subgraph will calculate and set the cache hint for the response that it sends to the gateway as it would for a non-federated Apollo Server sending a response to a client. When using Apollo Federation, the directive and CacheControlScope enum may be defined in a subgraph's schema. Using cache control with Apollo Federation requires v0.1.0 of (previously v0.28 of in your subgraph, v0.36 of in your router, and v3.0.2 of Apollo Server in both servers. You can set a default maxAge that's applied to fields that otherwise receive the default maxAge of 0. For more on this, see Recommended starting usage. Most other fields can then inherit their cache hint from their parent (fields with resolvers that don't fetch data less commonly have specific caching needs). Ideally, you can provide a maxAge for every field with a resolver that actually fetches data from a data source (such as a database or REST API). You can always set your own cache hint for any field with an undesirable default behavior. Of course, these heuristics aren't always correct! For example, the resolver for a non-root scalar field might indeed fetch remote data. Consequently, these fields inherit their default maxAge from their parent to reduce schema clutter. Resolvers for scalar, non-root fields rarely fetch data and instead usually populate data via the parent argument.Consequently, we also set their default maxAge to 0. Resolvers for other non-scalar fields (objects, interfaces, and unions) also commonly fetch data because they contain arbitrarily many fields.Root field resolvers are extremely likely to fetch data (because these fields have no parent), so we set their default maxAge to 0 to avoid automatically caching data that shouldn't be cached.At the same time, we don't think developers should have to specify cache hints for every single field in their schema. Our philosophy behind Apollo Server caching is that a response should only be considered cacheable if every part of that response opts in to being cacheable. Fields that return a non-scalar type (object, interface, or union) or a list of non-scalar types.Īll other schema fields (i.e., non-root fields that return scalar types) instead inherit their default maxAge from their parent field.Because every GraphQL operation includes a root field, this means that by default, no operation results are cached unless you set cache hints!.Root fields (i.e., the fields of the Query, Mutation, and Subscription types).The response's scope is PRIVATE if any field's scope is PRIVATE.īy default, the following schema fields have a maxAge of 0 if you don't specify one:.If that value is 0, the entire result is not cached. The response's maxAge equals the lowest maxAge among all fields.Calculating cache behaviorįor security, each operation response's cache behavior is calculated based on the most restrictive cache hints among the result's fields: This can be useful when resolving a union or interface field, which might return one of multiple object types. This method enables you to get the default cache hint for a particular object type. To use the directive, you must add the following definitions to your server's schema: In your schema (static)Īpollo Server recognizes the directive, which you can use in your schema to define caching behavior either for a single field, or for all fields that return a particular type. These details can vary significantly, even among the fields of a single object type. Whether a cached value is global or user-specific.How long a cached value should remain valid.Which fields of your schema can be cached safely.Note that when setting cache hints, it's important to understand: You can define field-level cache hints statically in your schema definition or dynamically in your resolvers (or both). You can then use this calculation to support any form of cache implementation you want, such as by providing it to your CDN via a Cache-Control header. When Apollo Server resolves an operation, it calculates the result's correct cache behavior based on the most restrictive settings among the result's fields.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |