Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: updated doc for field and entity caching #104

Merged
merged 4 commits into from
Feb 13, 2024
Merged

Conversation

tusharmath
Copy link
Contributor

@tusharmath tusharmath commented Jan 31, 2024

Updating documentation for caching

@@ -0,0 +1,25 @@
### Entity caching configuration
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can drop this doc.

Comment on lines 3 to 4
Field level caching enables caching for any field or query
which contains a resolver.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Field level caching enables caching for any field or query
which contains a resolver.
Field level caching enables caching for any field which contains a resolver.

type Post {
id: Int
title: String
user_id: Int @cache(maxAge: 100)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user_id: Int @cache(maxAge: 100)
userId: Int @cache(maxAge: 100)

id: Int
title: String
user_id: Int @cache(maxAge: 100)
user: User @http(path: "/user/{{user_id}}") @cache(maxAge: 200)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user: User @http(path: "/user/{{user_id}}") @cache(maxAge: 200)
user: User @http(path: "/user/{{value.userId}}") @cache(maxAge: 200)

}
```

For the above config, the result of `user` field will be cached because it contains an http resolver. However, the output of `user_id` will not be cached because it doesn't have a resolver for itself, it's value is fetched by the resolver at the `posts` query.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For the above config, the result of `user` field will be cached because it contains an http resolver. However, the output of `user_id` will not be cached because it doesn't have a resolver for itself, it's value is fetched by the resolver at the `posts` query.
For the above config, the result of `user` field will be cached because it contains an http resolver. However, the output of `userId` & `title` will not be cached because it doesn't have a resolver for itself, it's value is fetched by the resolver at the `posts` field having the `@http(path: "/posts")` directive.

}
```

When the `cache` directive is applied to a type then it is inherited by each of the field of the type. Hence, the above config is equivalent to:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When the `cache` directive is applied to a type then it is inherited by each of the field of the type. Hence, the above config is equivalent to:
When the `cache` directive is applied to a type then it is inherited by each of the field of the type. Hence, the above config can be reduced into:

id: Int
title: String
user_id: Int
user: User @http(path: "/user/{{user_id}}") @cache(maxAge: 100)
Copy link
Contributor Author

@tusharmath tusharmath Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user: User @http(path: "/user/{{user_id}}") @cache(maxAge: 100)
user: User @http(path: "/user/{{value.userId}}") @cache(maxAge: 100)

id: Int
title: String
user_id: Int
user: User @http(path: "/user/{{user_id}}") @cache(maxAge: 100)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user: User @http(path: "/user/{{user_id}}") @cache(maxAge: 100)
user: User @http(path: "/user/{{value.userId}}") @cache(maxAge: 100)

}
```

### Field level caching implementation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Field level caching implementation
### Implementing Cache Key

id: Int
title: String
user_id: Int
user: User @http(path: "/user/{{user_id}}") @cache(maxAge: 100)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user: User @http(path: "/user/{{user_id}}") @cache(maxAge: 100)
user: User @http(path: "/user/{{value.id}}") @cache(maxAge: 100)

Comment on lines 153 to 158
For the config above, field `user` will be cached and the key will be the result of hashing the following values:
* String "Post"
* String "user"
* value of the field `Post.id`

If `Post` doesn't contain a field `id` or the value isn't present in the response, then the value will not be cached.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation needs to change:

  • cache directive to read the resolver applied and use only the arguments on the resolver as key. It should not matter where the resolver is called from, for each query.

For eg: in @http(path: "/users/{{value.id}}") we should have /users/{{value.id}} as the key. So for a request made to /users/1 irrespective of where the resolver is attached, we should read from the same cache. For eg: query {todos {user {name}}} and query {comments {user {name}}} and query {posts {user {name}}} should all share the same cache if to resolve user we hit /users API.

@shashitnak shashitnak marked this pull request as ready for review February 5, 2024 17:16
@shashitnak shashitnak changed the title updated doc for field and entity caching docs: updated doc for field and entity caching Feb 5, 2024
@tusharmath tusharmath merged commit f859f99 into develop Feb 13, 2024
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants