Named Operations Object

This plugin generates an object containing a list of all your GraphQL operations and fragments. This is useful if you are using Apollo-Client and wish to have type validation when you are using refetchQueries.

All operations and fragments are being exported by their name (so unnamed operations are being ignored), in the following structure:

export const namedOperations = {
Query: [...],
Mutation: [...],
Subscription: [...],
Fragment: [...],
}

Installation

Using yarn
$ yarn add -D @graphql-codegen/named-operations-object

API Reference

identifierName

type: string default: namedOperations

Allow you to customize the name of the exported identifier

Usage Examples

generates:
path/to/file.ts:
plugins:
- typescript
- typescript-named-operations-object
config:
identifierName: ListAllOperations

How to use?

Include the plugin within your output (into an existing JS/TS file, or a new file), for example:

schema: YOUR_SCHEMA
documents: YOUR_OPERATIONS
generates:
./types.ts:
plugins:
- typescript
- typescript-operations
- named-operations-object

Now, you should be able to import namedOperations from that file, and use the names within your code. For example, with Apollo Client, you can do:

client
.mutate(
{ ... },
// No more typos, and you get auto-completion and build time validation
{ refetchQueries: [namedOperations.Query.myQuery] }
);