The Guild LogoThe Guild Monogram

Search docs

Search icon

Products by The Guild


Hive logoHive blurred logo


Schema Registry for your GraphQL Workflows


GraphQL Code Generator

Generate anything from GraphQL schema / operations!

Contact Us

Fragment Matcher

yarn add @graphql-codegen/fragment-matcher

This plugin generates an introspection file but only with Interfaces and Unions, based on your GraphQLSchema.

If you are using apollo-client and your schema contains interface or union declaration, it's recommended to use Apollo's Fragment Matcher and the result generated by the plugin.

You can read more about it in apollo-client documentation:

Fragment Matcher plugin accepts a TypeScript / JavaScript or a JSON file as an output (.ts, .tsx, .js, .jsx, .json).

Both in TypeScript and JavaScript a default export is being used.

The output is based on the output you choose for the output file name.

Config API Reference#


type: string (values: commonjs, es2015) default: es2015

Compatible only with JSON extension, allow you to choose the export type, either module.exports or export default. Allowed values are: commonjs, es2015.

Usage Examples#

generates: path/to/file.json: plugins: - fragment-matcher config: module: commonjs

type: number (values: 2, 3) default: 3

Compatible only with TS/TSX/JS/JSX extensions, allow you to generate output based on your Apollo-Client version. Valid values are: 2, 3.

Usage Examples#

generates: path/to/file.ts: plugins: - fragment-matcher config: apolloClientVersion: 3

type: boolean default: false

Create an explicit type based on your schema. This can help IDEs autofill your fragment matcher. This is mostly useful if you do more with your fragment matcher than just pass it to an Apollo-Client.

Usage Examples#

generates: path/to/file.ts: plugins: - fragment-matcher config: useExplicitTyping: true

type: boolean

How to use?#

Usage with Apollo Client 3#

If you are using Apollo Client 3, update your codegen config accordingly, and then use it in your Apollo cache instance:

import { InMemoryCache } from '@apollo/client' // generated by Fragment Matcher plugin import generatedIntrospection from '../introspection-result' const cache = new InMemoryCache({ possibleTypes: generatedIntrospection.possibleTypes })

Usage with Apollo Client 2#

If you are using version 2 of Apollo-Client, you need to specify the apolloClientVersion: 2 configuration, and then use it like that:

import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory' // generated by Fragment Matcher plugin import introspectionQueryResultData from '../introspection-result' const fragmentMatcher = new IntrospectionFragmentMatcher({ introspectionQueryResultData }) const cache = new InMemoryCache({ fragmentMatcher })

Package Details