GraphQL Codegen Types Map
Description
This package is a plugin for the graphql-code-generator which lists all known operations and fragment entries in:
- TypeScript interface, linking operation's
name
with:- kind (
subscription
,mutation
,query
,fragment
) - variables type
- return data type
- kind (
- [optionally] constant object (which can be used at runtime), linking operation's
name
with:- kind (
subscription
,mutation
,query
,fragment
), - reference to corresponding DocumentNode.
- kind (
where these constructs can be used as a building blocks to link all the context about a single operation/fragment just by its name.
codegen.yml
see working example of codegen.yml
at packages/graphql-codegen-types-map-test/codegen.yml
overwrite: trueschema: - './githunt/schema.gql'documents: - './githunt/**/*.graphql' generates: ... generated/operationsMap.ts: hooks: afterOneFileWrite: - prettier --write config: operationsMap: operationTypeTemplate: "{OperationName}{OperationKind}" variablesTypeTemplate: '{OperationName}{OperationKind}Variables' operationKindTemplate: '{operationKind}' operationDocumentTemplate: '{OperationName}' importTypesFrom: './gqlTypes' plugins: - add: '/* eslint-disable */' - 'graphql-codegen-types-map'
Config
You can see more about each of the config value at config.ts
config | description |
---|---|
withOperationsMap |
configures whether to generate object OperationsMap interfaces. |
operationTypeTemplate |
controls which name will be used to reference operations return data type (can also be imported from another file) |
variablesTypeTemplate |
controls which name will be used to reference variables type (can also be imported from another file) |
operationKindTemplate |
controls what string literal value will be given for the operation's kind type or what actual string value will be given for the documentsMap entry. |
operationDocumentTemplate |
controls which name will be used to reference document object in the documentsMap entry |
withDocumentsMap |
configures whether to generate object documentsMap constant which can be used on the runtime. |
importTypesFrom |
If value is present (non undefined ), then all types will be imported from this given location. |
importedTypesAlias |
When values are imported from another file, importedTypesAlias describes what alias will be given for the namespace. |
defaults of these values are:
withOperationsMap: trueoperationTypeTemplate: '{OperationName}{OperationKind}'variablesTypeTemplate: '{OperationName}{OperationKind}Variables'operationKindTemplate: '{operationKind}'operationDocumentTemplate: '{OperationName}{OperationKind === "Fragment" ? "FragmentDoc" : "Document"}'withDocumentsMap: trueimportTypesFrom: undefinedimportedTypesAlias: 'Types'
Templates
Templates (all the config values which end with suffix Template) are JavaScript literals (just ${
is replaced with {
for compatibility with other file types, such as .json
, .yml
) and a few extra values are in the context, such as:
- variables:
operationKind
- kind (subscription
,mutation
,query
,fragment
) of the operationoperationName
- name of the operationOperationKind
- pascalCase of the operationKindOperationName
- pascalCase of the operationName
- casing functions provided by the change-case NPM package.
Example generated code
Example Operations map types
/* eslint-disable */;
Example documents map constant
Building example code yourself
run these commands:
yarn
yarn build
yarn test
and then observe packages/graphql-codegen-types-map-test/generated/operationsMap.ts
you'll see real example of generated code.