From 43836f717030439b0b443d494a58b6de79db45d0 Mon Sep 17 00:00:00 2001 From: Shashi Kant Date: Tue, 26 Dec 2023 18:04:59 +0530 Subject: [PATCH 1/4] adding docs for cache --- docs/operators/cache.md | 31 +++++++++++++++++++++++++++++++ docs/operators/index.md | 1 + 2 files changed, 32 insertions(+) create mode 100644 docs/operators/cache.md diff --git a/docs/operators/cache.md b/docs/operators/cache.md new file mode 100644 index 0000000000..fb22af6052 --- /dev/null +++ b/docs/operators/cache.md @@ -0,0 +1,31 @@ +--- +title: "@cache" +sidebar_position: 7 +--- + +The **@cache** operator enables caching for the query, field or type it is applied to. For eg: + +```graphql +schema { + query: Query +} + +type Query { + posts: [Post] @cache(maxAge: 3000) + user(id: Int): User +} + +type Post { + title: String + body: String + user: User +} + +type User @cache(maxAge: 4000) { + id: Int + name: String @cache(maxAge: 8000) + age: Int +} +``` + +In the above example, the entire result of `posts` query will be cached for 3000ms. When the **@cache** operator is applied to a type, it is equivalent to applying it to each field individually. If for a type, one of the fields needs to be cached differently then this operator can be applied to that field separately and it will override the values provided for the type, as can be seen in the above example for `name` field in the `User` type. \ No newline at end of file diff --git a/docs/operators/index.md b/docs/operators/index.md index 9ed72d609e..e09d3c6d06 100644 --- a/docs/operators/index.md +++ b/docs/operators/index.md @@ -19,3 +19,4 @@ Certainly! Here's the table with hyperlinks added back to the operator names: | [@modify](modify.md) | Enables changes to attributes of fields or nodes in the schema. | | [@server](server.md) | Provides server configurations for behavior tuning and tailcall optimization in various use-cases. | | [@upstream](upstream.md) | Controls aspects of the upstream server connection, including timeouts and keep-alive settings. | +| [@cache](cache.md) | Enables caching for the query, field or type it is applied to. | From 580df71a55d746b79edc23e91c3e5110e2fb6717 Mon Sep 17 00:00:00 2001 From: Shashi Kant Date: Tue, 26 Dec 2023 18:16:10 +0530 Subject: [PATCH 2/4] fixing formatting --- docs/operators/cache.md | 2 +- docs/operators/index.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/operators/cache.md b/docs/operators/cache.md index fb22af6052..2337917bb9 100644 --- a/docs/operators/cache.md +++ b/docs/operators/cache.md @@ -28,4 +28,4 @@ type User @cache(maxAge: 4000) { } ``` -In the above example, the entire result of `posts` query will be cached for 3000ms. When the **@cache** operator is applied to a type, it is equivalent to applying it to each field individually. If for a type, one of the fields needs to be cached differently then this operator can be applied to that field separately and it will override the values provided for the type, as can be seen in the above example for `name` field in the `User` type. \ No newline at end of file +In the above example, the entire result of `posts` query will be cached for 3000ms. When the **@cache** operator is applied to a type, it is equivalent to applying it to each field individually. If for a type, one of the fields needs to be cached differently then this operator can be applied to that field separately and it will override the values provided for the type, as can be seen in the above example for `name` field in the `User` type. diff --git a/docs/operators/index.md b/docs/operators/index.md index e09d3c6d06..0ffed3932a 100644 --- a/docs/operators/index.md +++ b/docs/operators/index.md @@ -19,4 +19,4 @@ Certainly! Here's the table with hyperlinks added back to the operator names: | [@modify](modify.md) | Enables changes to attributes of fields or nodes in the schema. | | [@server](server.md) | Provides server configurations for behavior tuning and tailcall optimization in various use-cases. | | [@upstream](upstream.md) | Controls aspects of the upstream server connection, including timeouts and keep-alive settings. | -| [@cache](cache.md) | Enables caching for the query, field or type it is applied to. | +| [@cache](cache.md) | Enables caching for the query, field or type it is applied to. | From 1eeb7cc755daef9647130f3c70084898840c7e1e Mon Sep 17 00:00:00 2001 From: Shashi Kant Date: Wed, 27 Dec 2023 00:07:23 +0530 Subject: [PATCH 3/4] adding description for maxAge and description of how caching works --- docs/operators/cache.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/operators/cache.md b/docs/operators/cache.md index 2337917bb9..7e13385db8 100644 --- a/docs/operators/cache.md +++ b/docs/operators/cache.md @@ -28,4 +28,12 @@ type User @cache(maxAge: 4000) { } ``` +## maxAge + +the parameter `maxAge` takes a non-zero unsigned integer value which signifies the duration, in milliseconds, for which the value will be cached. + In the above example, the entire result of `posts` query will be cached for 3000ms. When the **@cache** operator is applied to a type, it is equivalent to applying it to each field individually. If for a type, one of the fields needs to be cached differently then this operator can be applied to that field separately and it will override the values provided for the type, as can be seen in the above example for `name` field in the `User` type. + +# How does the caching work? + +If **@cache** is set for a query or a field, the resolver for it will run once and the result will be stored in memory for `maxAge` milliseconds, and will expire after this duration. After the cache expires, the resolver will be run again to fetch the latest value and that value will then be cached. From c6b7fe50f6d0e6b14e0996708be7a369a04cc366 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Wed, 27 Dec 2023 11:21:29 +0530 Subject: [PATCH 4/4] update order --- docs/operators/cache.md | 1 - docs/operators/index.md | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/operators/cache.md b/docs/operators/cache.md index 7e13385db8..268f5a3043 100644 --- a/docs/operators/cache.md +++ b/docs/operators/cache.md @@ -1,6 +1,5 @@ --- title: "@cache" -sidebar_position: 7 --- The **@cache** operator enables caching for the query, field or type it is applied to. For eg: diff --git a/docs/operators/index.md b/docs/operators/index.md index 0ffed3932a..1063cdcabb 100644 --- a/docs/operators/index.md +++ b/docs/operators/index.md @@ -13,10 +13,10 @@ Certainly! Here's the table with hyperlinks added back to the operator names: | Operator | Description | | ------------------------- | ------------------------------------------------------------------------------------------------------------ | | [@addField](add-field.md) | Simplifies data structures and queries by adding, inlining, or flattening fields or nodes within the schema. | +| [@cache](cache.md) | Enables caching for the query, field or type it is applied to. | | [@const](const.md) | Allows embedding of a constant response within the schema. | | [@graphQL](graphql.md) | Resolves a field or node by a GraphQL API. | | [@http](http.md) | Resolves a field or node by a REST API. | | [@modify](modify.md) | Enables changes to attributes of fields or nodes in the schema. | | [@server](server.md) | Provides server configurations for behavior tuning and tailcall optimization in various use-cases. | | [@upstream](upstream.md) | Controls aspects of the upstream server connection, including timeouts and keep-alive settings. | -| [@cache](cache.md) | Enables caching for the query, field or type it is applied to. |