Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbahl/063507f4f9d9afb1dbd97083b99d7ae7 to your computer and use it in GitHub Desktop.
Save jasonbahl/063507f4f9d9afb1dbd97083b99d7ae7 to your computer and use it in GitHub Desktop.
Faust.js plugin for adding Automated Persisted Query support. Plays nice with https://github.com/wp-graphql/wp-graphql-smart-cache
import {createPersistedQueryLink} from '@apollo/client/link/persisted-queries';
import {HttpLink} from "@apollo/client";
import {sha256} from 'crypto-hash';
const linkChain = createPersistedQueryLink({ sha256 }).concat(
new HttpLink({ uri: process.env.WPGRAPHQL_URL }),
);
class PersistedQueriesPlugin {
apply({ addFilter }) {
addFilter('apolloClientOptions', 'faust', (apolloClientOptions) => {
const existingLink = apolloClientOptions?.link;
return {
...apolloClientOptions,
link: existingLink ? linkChain.concat(existingLink) : linkChain
}
});
}
}
export default PersistedQueriesPlugin;
@jasonbahl
Copy link
Author

This is a plugin that adds support for Apollo's Automated Persisted Queries.

Plays nice with https://github.com/wp-graphql/wp-graphql-smart-cache

To add, you need to update the faust.config.js like so:

import PersistedQueriesPlugin from 'path/to/the/file/above.js'

...

import { setConfig } from "@faustwp/core"
import templates from "./src/wp-templates"
import possibleTypes from "./possibleTypes.json"
import PersistedQueriesPlugin from './src/plugins/PersistedQueriesPlugin' # wherever you put the file for the gist above

/**
 * @type {import('@faustwp/core').FaustConfig}
 **/
export default setConfig({
  templates,
  experimentalPlugins: [
    new PersistedQueriesPlugin(), 
  ],
  possibleTypes,
})

@blakewilson
Copy link

💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment