npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@apollo/generate-persisted-query-manifest

v1.2.0

Published

Creates a Persisted Query Manifest from an Apollo Client Web project

Downloads

5,403

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-manifest

Use its CLI to extract queries from your app:

npx generate-persisted-query-manifest

CLI 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.json

Supported 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.json
  • persisted-query-manifest.config.json
  • .persisted-query-manifest.config.yml
  • persisted-query-manifest.config.yml
  • .persisted-query-manifest.config.yaml
  • persisted-query-manifest.config.yaml
  • .persisted-query-manifest.config.js
  • persisted-query-manifest.config.js
  • .persisted-query-manifest.config.ts
  • persisted-query-manifest.config.ts
  • .persisted-query-manifest.config.cjs
  • persisted-query-manifest.config.cjs
  • A persisted-query-manifest key in package.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;
  }),
};