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

Use getDirectives when retrieving cacheControl directives #6870

Closed
benatshippabo opened this issue Aug 31, 2022 · 2 comments
Closed

Use getDirectives when retrieving cacheControl directives #6870

benatshippabo opened this issue Aug 31, 2022 · 2 comments

Comments

@benatshippabo
Copy link

benatshippabo commented Aug 31, 2022

Issue

Currently we retrieve directives by calling .directives on the AST node, but this limits users with a code-first approach since in order for users to specify inheritMaxAge it has to be statically defined

Context

function cacheAnnotationFromType(t: GraphQLCompositeType): CacheAnnotation {
if (t.astNode) {
const hint = cacheAnnotationFromDirectives(t.astNode.directives);
if (hint) {
return hint;
}
}
if (t.extensionASTNodes) {
for (const node of t.extensionASTNodes) {
const hint = cacheAnnotationFromDirectives(node.directives);
if (hint) {
return hint;
}
}
}
return {};
}

function cacheAnnotationFromType(t: GraphQLCompositeType): CacheAnnotation {
if (t.astNode) {
const hint = cacheAnnotationFromDirectives(t.astNode.directives);
if (hint) {
return hint;
}
}
if (t.extensionASTNodes) {
for (const node of t.extensionASTNodes) {
const hint = cacheAnnotationFromDirectives(node.directives);
if (hint) {
return hint;
}
}
}
return {};
}

function cacheAnnotationFromField(
field: GraphQLField<unknown, unknown>,
): CacheAnnotation {
if (field.astNode) {
const hint = cacheAnnotationFromDirectives(field.astNode.directives);
if (hint) {
return hint;
}
}
return {};
}

graphql/graphql-js#3213 (comment)

graphql/graphql-js#1343

Proposal

We currently already use parts of @graphql-tools/*. We can use @graphql-tools/utils getDirectives so that users with a code-first approach are able to specify directives via extensions.directives.

So something like:

cacheAnnotationFromDirectives([
  // read directives from schema
  ...t.astNode.directives,
  // read directives specified under `extensions`
  ...getDirectives(schema, t.astNode)
])

and then users could do:

new GraphQLObjectType({
  name: 'ObjectInheritingMaxAge',
  fields: {
    date: { type: new GraphQLNonNull(GraphQLDate) },
  },
  extensions: {
    directives: {
      cacheControl: {
        inheritMaxAge: true,
      },
    },
  },
})
@glasser
Copy link
Member

glasser commented Sep 8, 2022

It's an interesting point. I do know that some movement has happened lately in the GraphQL world as far as potentially providing a more standard way of exposing directives (eg graphql/graphql-spec#300) and it might be a bit of a shame to adopt the non-standard extensions.directives from graphql-tools if a more standard way is coming soon. I'm not sure why it's impossible for TypeGraphQL to just create AST nodes directly...

@benatshippabo
Copy link
Author

Got it. Makes sense to me

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants