0

I tried to use highly advertised Automatic persisted query for performance benefits in Graphql based system but after spending three days I couldn't fix below problem. Apollo documents has lots of 404 pages for this topic if you try to look up -

On Client:

I used

import { createPersistedQueryLink } from "apollo-link-persisted-queries";


const httpLink = createHttpLink({uri: API_URL })


const apqSwitch = createPersistedQueryLink({useGETForHashedQueries: true}).concat(httpLink);

  let links = [errorlink, stateLink, setSiteIdHeaderLink, apqLinkSwitch]

  const link = ApolloLink.from(links)

  const client = new ApolloClient({
    defaultOptions: {
      watchQuery: {
        errorPolicy: 'all'
      },
      query: {
        errorPolicy: 'all'
      },
      mutate: {
        errorPolicy: 'all'
      }
    },
    link,
    cache,
    connectToDevTools: true,
    credentials: 'include',
  })

  return { client, persistor }
}

client dependencies:

"apollo-boost": "^0.1.22",
"apollo-cache-inmemory": "^1.3.11",
"apollo-cache-persist": "^0.1.1",
"apollo-client": "^2.4.7",
"apollo-link": "^1.2.3",
"apollo-link-batch-http": "^1.2.8",
"apollo-link-http": "^1.5.9",
"apollo-link-persisted-queries": "^0.2.2",
"apollo-link-retry": "^2.2.5",
"apollo-link-schema": "^1.1.1",

On Server:

import { InMemoryLRUCache } from 'apollo-server-caching';
const { RedisCache } = require('apollo-server-cache-redis');

const server = new ApolloServer({
  ...root,
  resolverValidationOptions: {
    requireResolversForResolveType: false,
  },
  persistedQueries: {
    /*
    cache: new CustomRedis(),
    */
    cache: new RedisCache({
      host: 'localhost',
      port: xxxx,
    }),
  },
  formatError,
  formatResponse: (response, query) => formatResponse({ response, query }),
  dataSources
});

Server dependencies:

"apollo-datasource-rest": "^0.3.2",
"apollo-errors": "^1.9.0",
"apollo-server-cache-redis": "^0.3.1",
"apollo-server-caching": "^0.3.1",
"apollo-server-express": "^2.4.8",
"cors": "^2.8.5",
"dataloader": "^1.4.0",
"express": "^4.16.3",
"glob": "^7.1.3",
"graphql": "^14.0.2",
"graphql-import": "^0.7.1",
"graphql-resolvers": "^0.2.2",

Observations:

  1. From client side I see sha1 has is being send to server.

variables=%7B%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%22fd4c5f1ae3sfsf6ee0f0710b8d303a9703db7a6708b401278e2fd664f56f4e91762f%22%7D%7D

  1. Server is looking for sha1 in the redis cache but it couldn't find
  2. Server returns PersistedQueryNotFound","MsgCode":"PERSISTED_QUERY_NOT_FOUND error to client

After that nothing happens there is no set operation is performed in redis cache and continuously getting PERSISTED_QUERY_NOT_FOUND error

Issue: APQ is not working, continuously getting PersistedQueryNotFound error

WitVault
  • 23,445
  • 19
  • 103
  • 133

1 Answers1

0

I have also configured APQs, and they are working as expected.

Can you please try below code at client side? Server side is pretty much same as I have.

import { createPersistedQueryLink } from "apollo-link-persisted-queries";
import { createHttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import ApolloClient from "apollo-client";

const link = createPersistedQueryLink({useGETForHashedQueries: true}).concat(createHttpLink({ uri: "/graphql" }));

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: link,
});

This is documented here!

Rishikesh Darandale
  • 3,222
  • 4
  • 17
  • 35