[v1]: info missing cacheControl - TypeScript graphql-modules

I am finalising a migration from 0.x to 1.x (1.21 currently), but ran into an issue with a resolver that programmatically adds cache hints.

  Query: {
    mostPopularArticles: async (_, { count }, { canCache, injector, user }, { cacheControl }) => {
      cacheControl.setCacheHint({
        maxAge: canCache ? 300 : 0,
        scope: user && user.id ? 'PRIVATE' : 'PUBLIC',
      });
      return await injector.get(MyRESTDataSource).mostPopularArticles(user, count);
    },
  }

and cacheControl is now undefined, whereas this works fine in 0.x.

The stack trace is

{
  "name": "my-apollo-express",
  "hostname": "MacBook-Pro-2.local",
  "pid": 52885,
  "level": 50,
  "err": {
    "message": "Cannot read property 'setCacheHint' of undefined",
    "locations": [],
    "path": ["mostPopularArticles"],
    "extensions": {
      "code": "INTERNAL_SERVER_ERROR",
      "exception": {
        "stacktrace": [
          "TypeError: Cannot read property 'setCacheHint' of undefined",
          "    at mostPopularArticles (/Users/mark/dev/my-apollo-express/dist/main.js:6554:25)",
          "    at /Users/mark/dev/my-apollo-express/node_modules/graphql-modules/index.cjs.js:1574:38",
          "    at dispatch (/Users/mark/dev/my-apollo-express/node_modules/graphql-modules/index.cjs.js:1408:40)",
          "    at composed (/Users/mark/dev/my-apollo-express/node_modules/graphql-modules/index.cjs.js:1414:16)",
          "    at wrappedResolver (/Users/mark/dev/my-apollo-express/node_modules/graphql-modules/index.cjs.js:1574:16)",
          "    at resolveField (/Users/mark/dev/my-apollo-express/node_modules/graphql/execution/execute.js:466:18)",
          "    at executeFields (/Users/mark/dev/my-apollo-express/node_modules/graphql/execution/execute.js:294:18)",
          "    at executeOperation (/Users/mark/dev/my-apollo-express/node_modules/graphql/execution/execute.js:238:122)",
          "    at executeImpl (/Users/mark/dev/my-apollo-express/node_modules/graphql/execution/execute.js:118:14)",
          "    at Object.execute (/Users/mark/dev/my-apollo-express/node_modules/graphql/execution/execute.js:62:35)"
        ]
      }
    }
  },
  "msg": "Cannot read property 'setCacheHint' of undefined",
  "time": "2021-01-15T02:25:41.261Z",
  "v": 0
}

I have confirmed that the info object in the resolveField function of the graphql module is the same whether executing with 0.x or 1.x graphql-modules - cacheControl has yet to be added. In 0.x it is then made available during resolverComposition.

Is there something I have missed in the migration that would provide the cacheControl in info in resolver execution in 1.x?

Asked Oct 04 '21 06:10
avatar markw351
markw351

6 Answer:

I need to understand how do you create and use GraphQL Modules with Apollo Server. Could you share a code snippet?

1
Answered Mar 02 '21 at 12:05
avatar  of kamilkisiela
kamilkisiela

Hi @kamilkisiela, I encountered a similar problem when I tried to add cacheControl directives to our schema. Here's a very simple example application that reproduces the problem. https://github.com/ftatzky/apollo-cache-test Adding static cache hints via directives or setting dynamic cache hints is currently not possible.

1
Answered Mar 04 '21 at 10:52
avatar  of ftatzky
ftatzky

Yes, it's createSchemaForApollo that is broken. It's something experimental to enable Subscriptions support in Apollo.

If you don't need GraphQL Subscriptions, you can use createApolloExecutor and pass the executor to Apollo Server.

1
Answered Mar 04 '21 at 10:54
avatar  of kamilkisiela
kamilkisiela

Our project depends on subscriptions, but using APPLICATION.typeDefs/.resolvers yields the same error. So it is not exclusive to createSchemaForApollo

1
Answered Mar 04 '21 at 10:59
avatar  of ftatzky
ftatzky

@kamilkisiela so I guess there's no alternative to make cache hints work right now?

1
Answered Mar 05 '21 at 07:56
avatar  of ftatzky
ftatzky

I think it happens because Apollo Server defines a non-enumerable property on contextValue and it just gets lost. I will try to fix it.

1
Answered Mar 05 '21 at 09:46
avatar  of kamilkisiela
kamilkisiela