Package Exports
- @apollo/generate-persisted-query-manifest
- @apollo/generate-persisted-query-manifest/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@apollo/generate-persisted-query-manifest) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@apollo/generate-persisted-query-manifest
Setup
First, install the @apollo/generate-persisted-query-manifest package as a dev dependency:
npm install --save-dev @apollo/generate-persisted-query-manifestUse its CLI to extract queries from your app:
npx generate-persisted-query-manifestCLI configuration
To override the default options, you can provide a config file. Create a persisted-query-manifest.config.json file in the root of your project.
{
"documents": [
"src/**/*.{graphql,gql,js,jsx,ts,tsx}",
"!**/*.d.ts",
"!**/*.spec.{js,jsx,ts,tsx}",
"!**/*.story.{js,jsx,ts,tsx}",
"!**/*.test.{js,jsx,ts,tsx}"
],
"output": "persisted-query-manifest.json"
}NOTE: The config file is optional. Defaults for each option are displayed above.
If you would like to define the config file in a directory other than the root, you can tell the CLI the location of the config file using the --config option.
npx generate-persisted-query-manifest --config path/to/persisted-query-manifest.config.jsonSupported config file formats
The config file can be provided in a variety of formats and optionally prefixed with a dot (.). The CLI will search for a config file in these locations:
.persisted-query-manifest.config.jsonpersisted-query-manifest.config.json.persisted-query-manifest.config.ymlpersisted-query-manifest.config.yml.persisted-query-manifest.config.yamlpersisted-query-manifest.config.yaml.persisted-query-manifest.config.jspersisted-query-manifest.config.js.persisted-query-manifest.config.tspersisted-query-manifest.config.ts.persisted-query-manifest.config.cjspersisted-query-manifest.config.cjs- A
persisted-query-manifestkey inpackage.json
Configuration
To customize the CLI, create a config file using one of the formats described above in the root of your directory. If using a .js , .cjs, or .ts config file, your config object must be the default export.
TypeScript
If you define your config file as a TypeScript file, you can get autocompletion and documentation on the options by importing the PersistedQueryManifestConfig type from the package.
// persisted-query-manifest.config.ts
import { PersistedQueryManifestConfig } from "@apollo/generate-persisted-query-manifest";
const config: PersistedQueryManifestConfig = {
// options
};
export default config;Options
documents-string | string[]
Tell the CLI where to look for your documents. You can use glob patterns to determine where to look. Prefix the pattern with ! to ignore the path which is useful to ignore queries that might be defined in your tests or storybook stories not used in your production application.
Paths are interpreted relative to the current working directory, not the config file's directory. We recommend always running generate-persisted-query-manifest via an npm run script, which runs commands with the directory containing package.json as the current directory.
Default:
[
"src/**/*.{graphql,gql,js,jsx,ts,tsx}",
"!**/*.d.ts",
"!**/*.spec.{js,jsx,ts,tsx}",
"!**/*.story.{js,jsx,ts,tsx}",
"!**/*.test.{js,jsx,ts,tsx}"
]Usage with GraphQL Codegen persisted documents
GraphQL Codegen is a popular code
generation utility used with GraphQL. You can use GraphQL Codegen's persisted
documents feature with generate-persisted-query-manifest by providing the documents option with the fromGraphQLCodegenPersistedDocuments utility exported by this package. This is useful to prevent traversing the file system to parse GraphQL documents since GraphQL Codegen has already done the hard work for you.
import type { PersistedQueryManifestConfig } from "@apollo/generate-persisted-query-manifest";
import { fromGraphQLCodegenPersistedDocuments } from "@apollo/generate-persisted-query-manifest";
const config: PersistedQueryManifestConfig = {
documents: fromGraphQLCodegenPersistedDocuments(
"./src/gql/persisted-documents.json",
),
};
export default config;NOTE: Running these documents through this package is necessary to ensure the GraphQL document sent to the server by Apollo Client matches that in the manifest. While GraphQL Codegen's persisted documents transforms are intended to be similar to Apollo Client's, there are subtle differences that may prevent correct usage of Apollo's persisted queries. This also ensures forward compatibility with any future transforms introduced by Apollo Client that may alter the GraphQL document output.
output-string
Tell the CLI the location of where to write your manifest file. Paths are interpreted relative to the current working directory.
Default: persisted-query-manifest.json
createOperationId- Function(query: string, options: CreateOperationIdOptions) => string
A custom function that allows you to customize the id for a query operation. By default, a SHA 256 hash of the query string will be used to generate the id. This option can only be used if your config file is defined using a .js, .cjs or .ts extension.
When you use this option, you cannot use the generatePersistedQueryIdsAtRuntime function from @apollo/persisted-query-lists in your client, because that function assumes that you are using the default ID generation (SHA256 hash of the body). It is compatible with generatePersistedQueryIdsFromManifest.
interface CreateOperationIdOptions {
/**
* The name of the operation.
*/
operationName: string;
/**
* The type of operation.
*/
type: "query" | "mutation" | "subscription";
/**
* Helper function that returns the default generated ID for the operation.
*/
createDefaultId: () => string;
}Here is an example that uses a Base 64 encoded string for an operation named TestOperation, but uses the default generated ID for all others.
import { Buffer } from "node:buffer";
const config = {
createOperationId(query, { operationName, type, createDefaultId }) {
switch (operationName) {
case "TestOperation":
return Buffer.from(query).toString("base64");
default:
return createDefaultId();
}
},
};documentTransform-DocumentTransform
An @apollo/client DocumentTransform instance used to transform GraphQL documents before they are saved to the manifest. Use this option if you pass a documentTransform option to your Apollo Client instance.
For more information, see the Apollo Client Document transforms documentation.
NOTE: This feature is only available if you use Apollo Client 3.8.0 or greater.
import { DocumentTransform } from "@apollo/client/core";
const config = {
// Inlined for this example, but ideally this should use the same instance
// that is passed to your Apollo Client instance
documentTransform: new DocumentTransform((document) => {
// ... transform the document
return transformedDocument;
}),
};