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

Not working with Apollo persistedQueryLink? #398

Open
david-vendel opened this issue Oct 15, 2022 · 3 comments
Open

Not working with Apollo persistedQueryLink? #398

david-vendel opened this issue Oct 15, 2022 · 3 comments

Comments

@david-vendel
Copy link

TLDR:
The persisted query link requires using the HttpLink. (see link to their doc bellow).
Is it possible to set up MultiAPILink using HttpLink? (instead of createHttpLink)

Longer explanation:

Hello, in apollo-client.ts I had implemented PersistendQueryLink, based on https://www.apollographql.com/docs/react/api/link/persisted-queries/

I have const links = [] and then push there errorLink, authorizationLink, (retry link), following by:

if (process.env.NODE_ENV === 'production') {
  const persistedQueryLink = createPersistedQueryLink({ sha256 })
  links.push(persistedQueryLink)
}

const retryLink = new RetryLink()
links.push(retryLink)

const httpLink = new MultiAPILink({
  createHttpLink: () => createHttpLink(),
  endpoints,
  httpSuffix: '',
  wsSuffix: '',
})

links.push(httpLink)

export const client = new ApolloClient({
  cache,
  link: ApolloLink.from(links),
})

I get error from BE:

"PersistedQueryNotFoundError: PersistedQueryNotFound"
 at processGraphQLRequest (/Users/david/Documents/github/datacatalog-be/node_modules/apollo-server-core/src/requestPipeline.ts:181:40)"
 at processTicksAndRejections (node:internal/process/task_queues:96:5)"
 at processHTTPRequest (/Users/david/Documents/github/datacatalog-be/node_modules/apollo-server-core/src/runHttpQuery.ts:434:24)"
message
: 
"PersistedQueryNotFound"

Before using this library, I connected to 1 endpoint like so:

const httpLink = new HttpLink({
  uri: process.env.NEXT_PUBLIC_API_URL,
})

Thus it is clear my error comes from connecting to multiple endpoints using this library.

I think answer to my problem is in sentence: The persisted query link requires using the HttpLink. (see link to their doc above).
Thus my question is, is it possible to set up MultiAPILink using HttpLink? (instead of createHttpLink). If yes, how?

@jean9696
Copy link
Member

Hi @justdvl, we never tried to use our link with PersistendQueryLink but it seems like it won't work out of the box... It will need additional development. I can have a look soon but PRs are welcome!

@joelmukuthu
Copy link
Contributor

I've ran into this issue as well. I think the issue is that the operations get persisted containing the @api directive and when the server matches the operation's hash in to a persisted operation, which contains the @api directive, it's then not able to handle the directive.
I can't really think of a way to get this library to work with persisted operations but if there's a way, I'd like to work on a PR. Any pointers @jean9696?

@joelmukuthu
Copy link
Contributor

Just some feedback.

TLDR: you can still use this (great!) library with persisted operations, without requiring any changes to your code (or the library).

Longer version: I fixed this by writing some code that alters the persisted operations to remove the @api directive. When the persisted operations are revived on the backend, they don't contain the directive and can therefore be handled without issue. The graphql operations themselves still need to contain the directive as it'll be used by apollo to route the operation to the correct endpoint. This presents a nice "feature": that operation hashes change if an operation's endpoint changes (e.g. from @api(name: "foo") to @api(name: "bar")), since hashes are generated from the operation's contents.

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

3 participants