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 not working #1059

Closed
thomaswr opened this issue Oct 4, 2023 · 2 comments
Closed

Integrating Apollo cacheControl feature not working #1059

thomaswr opened this issue Oct 4, 2023 · 2 comments

Comments

@thomaswr
Copy link

thomaswr commented Oct 4, 2023

I'm trying to get Apollo server 4's server side caching feature via cachecontrol headers to work, as described on https://www.apollographql.com/docs/apollo-server/performance/caching/#in-your-resolvers-dynamic but Pothos seems to interfere.

interface SchemaTypes {
  Context: GQLContext;
  DefaultFieldNullability: true;
  SubGraphs: 'subgraph1' | 'subgraph2';
}

export const builder = new SchemaBuilder<SchemaTypes>({
  defaultFieldNullability: true,
  plugins: ['subgraph1', 'subgraph2'],
  subGraphs: {
    defaultForTypes: [],
    fieldsInheritFromTypes: true,
  },
});

builder.queryType({ subGraphs: ['subgraph1', 'subgraph2'] });
import { cacheControlFromInfo } from '@apollo/cache-control-types';

builder.queryField('someField', (t) => {
  return t.field({
    subGraphs: ['subgraph1'],
    type: MyResponse,
    args: {
      id: t.arg({
        type: 'ID',
      }),
    },
    resolve: (parent, { id }, context, info) => {

        //  does not work
        // @ts-expect-error
      	info.cacheControl.setCacheHint({ maxAge: 123 });

        // does not work either
        cacheControlFromInfo(info).setCacheHint({ maxAge: 123 });

        return value;
    },
  });
});

If I remove pothos as the schema generator it works.
#782 didn't solve it for me.

Could this be an issue?

@hayes
Copy link
Owner

hayes commented Oct 5, 2023

Tried to reproduce the issue, but this seems to be working as expected for me: https://github.com/hayes/apollo-cache-debug

@thomaswr
Copy link
Author

thomaswr commented Oct 5, 2023

The problem occured when queries returned more than one level of object types.
So Apollo wants me to set the cacheControl directive with the inheritMaxAge on child types.
(See: https://www.apollographql.com/docs/apollo-server/performance/caching/#recommended-starting-usage)

Solved that with the directives plugin:

  Directives: {
    cacheControl: {
      locations: 'FIELD_DEFINITION' | 'OBJECT' | 'INTERFACE' | 'UNION';
      args: { inheritMaxAge?: boolean; maxAge?: number; scope?: CacheScope };
    };
  };

And on all affected schema types I added:

  directives: {
    cacheControl: {
      inheritMaxAge: true,
    },
  },

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