I’ve upgraded my Apollo Server 2 app to Apollo Server 4 standalone and setup redis for cache. I know my KeyV is working because I tested that separately. No query data is written to my cache. My server.ts has this:
let keyV: any;
if (graphqlCacheEnabled === true) {
keyV = new Keyv(`redis://${redisHost}:${redisPort}`);
}
const server = new ApolloServer<MyContext>({
typeDefs,
resolvers,
csrfPrevention: true,
cache: graphqlCacheEnabled === true ? new KeyvAdapter(keyV) : undefined,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
And I annotated a type like this:
type CodeCampYear @cacheControl(maxAge: 240) {
id: ID!
codeCampYearId: Int
codeCampYearTypeId: Int
name: String
}
I’ve responded on the related GH issue for anyone who ends up here looking for an answer.
opened 02:09AM - 03 Mar 23 UTC
closed 05:14PM - 03 Mar 23 UTC
### Issue Description
Taking the basic codesandbox example from the AS4 docs … getting started and making very minor changes to support Redis, no redis key/values get inserted when a very simple cache control is added. Below is the updated index.ts.
I created a github repo with the main branch being the unchanged codesandbox demo. I then made very few changes to add Redis cache, assuming there is a localhost:6379 redis server responding. The app works, I verified that the cache hint is set, yet no key is added to the redis db.
Here is the GitHub repo. https://github.com/pkellner/as4-redis-cache-broken
The main branch is exactly what is in AS4 docs with no change.
the branch named bug-add-redis-fail has the changed code that should add key/value to Redis and it does not.
https://github.com/pkellner/as4-redis-cache-broken/tree/bug-add-redis-fail
``` JavaScript
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import Keyv from "keyv";
import { KeyvAdapter } from "@apollo/utils.keyvadapter";
// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = `#graphql
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
# This "Book" type defines the queryable fields for every book in our data source.
type Book {
title: String
author: String
}
# The "Query" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each. In this
# case, the "books" query returns an array of zero or more Books (defined above).
type Query {
books: [Book]
}
`;
const books = [
{
title: "The Awakening",
author: "Kate Chopin",
},
{
title: "City of Glass",
author: "Paul Auster",
},
];
// Resolvers define the technique for fetching the types defined in the
// schema. This resolver retrieves books from the "books" array above.
const resolvers = {
Query: {
// @ts-ignore
books: (parent, args, contextValue, info) => {
console.log("books");
info.cacheControl.setCacheHint({ maxAge: 60, scope: "PRIVATE" });
return books;
},
},
};
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({
typeDefs,
resolvers,
});
// Passing an ApolloServer instance to the `startStandaloneServer` function:
// 1. creates an Express app
// 2. installs your ApolloServer instance as middleware
// 3. prepares your app to handle incoming requests
const keyV = new Keyv(`redis://localhost:6379`);
const { url } = await startStandaloneServer(server, {
// @ts-ignore
cache: new KeyvAdapter(keyV),
cacheControl: {
defaultMaxAge: 5,
},
listen: { port: 4000 },
});
console.log(`🚀 Server listening at: ${url}`);
```
### Link to Reproduction
https://github.com/pkellner/as4-redis-cache-broken/tree/bug-add-redis-fail
### Reproduction Steps
execute this query:
```
{
books {
author
title
}
}
```
and no values are added to the Redis store.