feat: client options and client context #45
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This introduces a new feature called "client options". It should make it easier to pass context information from within the Nuxt app to the GraphQL middleware on the server.
A common use case is to pass the current language along with every request to the middleware.
Currently to achieve the same you'd have to set
fetchOptions
viauseGraphqlState()
in a plugin by setting additional query params on the outgoing request.New
graphqlMiddleware.clientOptions.ts
fileSimilar to the existing
serverOptions
file, this new file can be used to define options that are used for everything non-server/nitro related. Currently a single method can be implemented calledbuildClientContext
.buildClientContext()
This method should return an object with both string properties and values. Whenever a request to the middleware is made, this method is called and the object is passed along via query parameters:
The resulting request URL:
/api/graphql_middleware/loadProduct?id=123&__gqlc_language=en&__gqlc_country=US
On the server, these query params are then extracted and converted back to an object with the same properties as returned in
buildClientContext()
.This context object is then passed to all
serverOptions
methods: