From a419252a514e9c8cd94017d953e2491db79726ed Mon Sep 17 00:00:00 2001 From: Blake Wilson Date: Thu, 24 Aug 2023 15:25:07 -0500 Subject: [PATCH] Disable `createHttpLink` `useGETForQueries` option if persisted queries is enabled (#1548) * Disable `createHttpLink` `useGETForQueries` option if persisted queries is enabled * Testing commit * Revert "Testing commit" This reverts commit 2e1f9d49ed32ab867890cc36337194f11f1de0b5. * Add changeset --- .changeset/calm-buckets-perform.md | 5 +++++ packages/faustwp-core/src/client.ts | 7 ++++++- packages/faustwp-core/tests/client.test.ts | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .changeset/calm-buckets-perform.md diff --git a/.changeset/calm-buckets-perform.md b/.changeset/calm-buckets-perform.md new file mode 100644 index 000000000..f9d1a8a96 --- /dev/null +++ b/.changeset/calm-buckets-perform.md @@ -0,0 +1,5 @@ +--- +'@faustwp/core': patch +--- + +Fixed an issue where persisted queries were not being enabled properly when setting the `usePersistedQueries` flag. diff --git a/packages/faustwp-core/src/client.ts b/packages/faustwp-core/src/client.ts index c73400e36..c334adf1b 100644 --- a/packages/faustwp-core/src/client.ts +++ b/packages/faustwp-core/src/client.ts @@ -58,7 +58,12 @@ export function createApolloClient(authenticated = false) { let linkChain = createHttpLink({ uri: getGraphqlEndpoint(), - useGETForQueries, + /** + * Only add this option if usePersistedQueries is not set/false. + * When persisted queries is enabled and this flag and useGETForHashedQueries + * are both set, there is a conflict and persisted queries does not work. + */ + useGETForQueries: useGETForQueries && !usePersistedQueries, }); // If the user requested to use persisted queries, apply the link. diff --git a/packages/faustwp-core/tests/client.test.ts b/packages/faustwp-core/tests/client.test.ts index 81fd76f24..9a16c40ff 100644 --- a/packages/faustwp-core/tests/client.test.ts +++ b/packages/faustwp-core/tests/client.test.ts @@ -81,6 +81,25 @@ describe('createApolloClient', () => { expect(persistedQueriesLinkSpy).toHaveBeenCalledTimes(1); }); + it('Does not set useGETForQueries in HttpLink if persisted queries is enabled', () => { + process.env.NEXT_PUBLIC_WORDPRESS_URL = 'http://headless.local'; + + setConfig({ + usePersistedQueries: true, + } as any as FaustConfig); + + client.createApolloClient(); + + expect(httpLinkSpy).toHaveBeenCalledTimes(1); + + expect(httpLinkSpy).toHaveBeenCalledWith({ + uri: 'http://headless.local/index.php?graphql', + useGETForQueries: false, + }); + + expect(persistedQueriesLinkSpy).toHaveBeenCalledTimes(1); + }); + it('invokes setContext to set the proper auth headers if its an auth client', () => { client.createApolloClient(true);