Skip to content

Use getDirectives when retrieving cacheControl directives #6870

Closed
@benatshippabo

Description

@benatshippabo

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,
      },
    },
  },
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions