javascriptnode.jscachingapollo-serverkeystonejs

Cache problem when adding data sources to the apollo config option in Keystone 5


When executing a request to the server for the second time, the result that returns is always the cached one, even if the first request mutated the entity.

here is the dataSources.js file that imports two instances of each class of dataSource:

  1. legacyUserApi = new LegacyUserApi();
  2. legacyMailApi = new LegacyMailApi();
import legacyUserApi from './LegacyUserApi';
import legacyMailApi from './LegacyMailApi';

export default () => ({
  legacyUserApi,
  legacyMailApi
});

and in the keystone.js file I import it:

import dataSources from './dataSources';

const apps = [
  new GraphQLApp({
    apiPath: API_PATH,
    apollo: {
      dataSources,
      introspection: isDev
    }
  })
]

Solution

  • Keystone js uses Apollo Server so I've looked at their documentation:

    Apollo Server calls this function for every incoming operation. Also as shown, the function should create a new instance of each data source for each operation.

    so dataSources.js file should import the class and not the instance so that Apollo Server will create a new instance with every request.

    import LegacyUserApi from './LegacyUserApi';
    import LegacyMailApi from './LegacyMailApi';
    
    export default () => ({
      legacyUserApi: new LegacyUserApi(),
      legacyMailApi: new LegacyMailApi()
    });