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

Integrating Apollo cacheControl feature into schema not easily possible #782

Closed
ChristianIvicevic opened this issue Feb 7, 2023 · 2 comments

Comments

@ChristianIvicevic
Copy link

ChristianIvicevic commented Feb 7, 2023

I was looking into integrating the automatic caching of Apollo which I use as a server into my schema and there are two approaches. Either declaring a directive or setting it dynamically. Unfortunately there are two issues:

  1. The directive that I tried to add via the directive plugin is not appearing in the schema at all. Thus it has no effect when declaring it for certain fields. I guess I need a transformer but there is none that exists for that as far as I can tell.
  2. Setting the cacheControl dynamically works, but is not part of the info object according to TypeScript.
schemaBuilder.queryField('cachedData', (t) =>
  t.field({
    type: 'String',
    resolve: (_1, _2, _3, info) => {
      // @ts-expect-error
      info.cacheControl.setCacheHint({ maxAge: 30 });
      console.log('heavy computation');
      return 'result';
    }
  })
);

Do you have suggestions on how to support this within Pothos? Either option would be nice, the former slightly more as it clearly documents the behavior for consumers within the schema.

@hayes
Copy link
Owner

hayes commented Feb 7, 2023

You could do something like this to add the cacheControl types to the info object

declare module 'graphql' {
  export interface GraphQLResolveInfo {
    cacheControl: {
      setCacheHint: (hint: { maxAge: number }) => void;
    };
  }
}

This can either be added to a declaration file that ts automatically imports, or add or import this from the file that defines the builder to ensure that the definition is always imported.

For the directives plugin, to get the directives in the schema, you need to use something like graphQL tools to print the schema with directives. Unfortunately the printSchema method from graphql does not have a way to print directives

@ChristianIvicevic
Copy link
Author

Yeah I was also considering to go for module augmentation as this is one of the very rare occasions where it's actually less detrimental and unexpected. I'll just use that for now even though the DX of explicitly calling it over adding a directive is less convenient. Will research at some point how to print the directives as you proposed.

Thanks once again.

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

No branches or pull requests

2 participants