Open rozsival opened 1 year ago
I had a similar issue when trying to set cacheHint (maxAge, scope):
This code won't set headers for http cache header
import { list } from "@keystone-6/core";
import { image, text } from "@keystone-6/core/fields";
import { CacheScope } from "apollo-server-types";
export default list({
fields: {
title: text({ validation: { isRequired: true } }),
image: image({
storage: "images",
graphql: { cacheHint: { maxAge: 60000, scope: CacheScope.Public } },
}),
},
});
Also can't pass scope as a string and I've imported the related enum from apollo-server-types
which is weird!
There is a link on the official keystone's docs refering to this apollo document which mentions that setting cache should generate a graphql that affects that particular field.
For the example above the graphql still is:
type Image {
id: ID!
title: String
image: ImageFieldOutput
}
but It should actually be:
type Image {
id: ID!
title: String
image: ImageFieldOutput @cacheControl(maxAge: 60000, scope: PRIVATE)
}
Sorry if this was supposed to be another issue, I just thought bringing it up here is helpful.
Hey @devmor-j, your comment is not related to this issue, however, I would like to mention these points:
scope
property is of type enum CacheScope
. You cannot assign a string to an enum, TSC checks this correctly for you. It is not a bug, it's a compiler feature and it's absolutely correct it makes you import the enum
from the package where it's originally defined. That way your code stays type safe and in sync with all the type definitions.Thanks for your comment @rozsival
@rozsival
Have you tried graphql-middleware?
It can be used at extendGraphQLSchema
import { applyMiddleware } from 'graphql-middleware'
const useContext = async (resolve, root, args, context, info) => {
const myContext = { ...context, isFoo: true }
const result = await resolve(root, args, myContext, info)
console.log({ result })
return result
}
export const extendGraphQLSchema = (schema: GraphQLSchema) => {
return applyMiddleware(schema, useContext)
}
In my tests, it works. But the extendGraphQLSchema is not asynchronous. It causes delay and it's not scalable.
I'm trying a possible solution for the main problem of the issue.
Steps to reproduce
config
from@keystone-6/core
context
function tographql.apolloConfig.context
context
object from the functioncontext
in any resolvercontext
value isundefined
(only default Keystonecontext
values are present)What should happen?
ApolloServer
context should be extended with the customcontext
provided ingraphql.apolloConfig.context
as typings allow it. If provided, it should be merged with the default Keystonecontext
, or an alternative API to extend the GQL context should be available.What actually happens?
ApolloServer
instance is passed only the default Keystonecontext
. The value fromgraphql.apolloConfig.context
is never used.System Info