Flow Resolvers
Package name | Weekly Downloads | Version | License | Updated |
---|---|---|---|---|
@graphql-codegen/flow-resolvers | Nov 1st, 2022 |
Installation
yarn add -D @graphql-codegen/flow-resolvers
Quick Start with flow-resolvers
You can find a blog post we wrote about using and customizing this plugin here, it refers to typescript-resolvers
but everything there is relevant to flow-resolvers
as well.
Enum Resolvers
Apollo-Server and schemas built with graphql-tools
supports creating resolvers for GraphQL enum
s.
This is helpful because you can have internal values that are different from the public enum values, and you can use the internal values in your resolvers.
Codegen allows you to specify either mappers
or enumValues
to map enums in your resolvers, and if you are using it for enums, you'll get a resolver signature for the enum resolvers as well.
Usage Example
With the following schema:
type Query {
favoriteColor: Color!
}
enum Color {
RED
BLUE
}
schema: schema.graphql
generates:
./resolvers-types.js:
config:
enumValues:
Color: ./enums#ColorsCode
plugins:
- flow
- flow-resolvers
export enum ColorsCode {
MY_RED = '#FF0000',
MY_BLUE = '#0000FF'
}
import type { Resolvers } from './resolvers-types'
import { ColorsCode } from './enums'
const resolvers: Resolvers = {
Color: {
RED: ColorsCode.MY_RED,
BLUE: ColorsCode.MY_BLUE
},
Query: {
favoriteColor: () => ColorsCode.MY_RED // Now you cn return this, and it will be mapped to your actual GraphQL enum
}
}
You can also define the same with explicit enum values:
schema: schema.graphql
generates:
./resolvers-types.js:
config:
enumValues:
Color:
RED: '#FF0000'
BLUE: '#0000FF'
plugins:
- flow
- flow-resolvers
Or, with mappers
:
schema: schema.graphql
generates:
./resolvers-types.js:
config:
mappers:
Color: ./enums#ColorsCode
plugins:
- flow
- flow-resolvers
This plugin generates resolvers signature based on your GraphQLSchema
.
It generates types for your entire schema: types, input types, enum, interface, scalar and union.
This plugin requires you to use @graphql-codegen/flow
as well, because it depends on it's types.
Config API Reference
addUnderscoreToArgsType
type: boolean
Adds _
to generated Args
types in order to avoid duplicate identifiers.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
addUnderscoreToArgsType: true
},
},
},
};
export default config;
contextType
type: string
Use this configuration to set a custom type for your context
, and it will
affect all the resolvers, without the need to override it using generics each time.
If you wish to use an external type and import it from another file, you can use add
plugin
and add the required import
statement, or you can use a module#type
syntax.
Usage Examples
Custom Context Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
contextType: 'MyContext'
},
},
},
};
export default config;
Custom Context Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
contextType: './my-types#MyContext'
},
},
},
};
export default config;
fieldContextTypes
type: Array_1
Use this to set a custom type for a specific field context
.
It will only affect the targeted resolvers.
You can either use Field.Path#ContextTypeName
or Field.Path#ExternalFileName#ContextTypeName
Usage Examples
Custom Field Context Types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
fieldContextTypes: ['MyType.foo#CustomContextType', 'MyType.bar#./my-file#ContextTypeOne']
},
},
},
};
export default config;
rootValueType
type: string
Use this configuration to set a custom type for the rootValue
, and it will
affect resolvers of all root types (Query, Mutation and Subscription), without the need to override it using generics each time.
If you wish to use an external type and import it from another file, you can use add
plugin
and add the required import
statement, or you can use both module#type
or module#namespace#type
syntax.
Usage Examples
Custom RootValue Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
rootValueType: 'MyRootValue'
},
},
},
};
export default config;
Custom RootValue Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
rootValueType: './my-types#MyRootValue'
},
},
},
};
export default config;
directiveContextTypes
type: Array_1
Use this to set a custom type for a specific field context
decorated by a directive.
It will only affect the targeted resolvers.
You can either use Field.Path#ContextTypeName
or Field.Path#ExternalFileName#ContextTypeName
ContextTypeName should by a generic Type that take the context or field context type as only type parameter.
Usage Examples
Directive Context Extender
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
directiveContextTypes: ['myCustomDirectiveName#./my-file#CustomContextExtender']
},
},
},
};
export default config;
mapperTypeSuffix
type: string
Adds a suffix to the imported names to prevent name clashes.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
mapperTypeSuffix: 'Model'
},
},
},
};
export default config;
mappers
type: object
Replaces a GraphQL type usage with a custom type, allowing you to return custom object from
your resolvers.
You can use both module#type
and module#namespace#type
syntax.
Usage Examples
Custom Context Type
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
mappers: {
User: './my-models#UserDbObject',
Book: './my-models#Collections',
}
},
},
},
};
export default config;
defaultMapper
type: string
Allow you to set the default mapper when it's not being override by mappers
or generics.
You can specify a type name, or specify a string in module#type
or module#namespace#type
format.
The default value of mappers is the TypeScript type generated by typescript
package.
Usage Examples
Replace with any
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultMapper: 'any',
},
},
},
};
export default config;
Custom Base Object
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultMapper: './my-file#BaseObject',
},
},
},
};
export default config;
Wrap default types with Partial
You can also specify a custom wrapper for the original type, without overriding the original generated types, use {T}
to specify the identifier. (for flow, use $Shape<{T}>
)
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultMapper: 'Partial<{T}>',
},
},
},
};
export default config;
Allow deep partial with utility-types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver', { add: { content: "import { DeepPartial } from 'utility-types';" } }],
config: {
defaultMapper: 'DeepPartial<{T}>',
},
},
},
};
export default config;
avoidOptionals
type: AvoidOptionalsConfig_1 | boolean
default: false
This will cause the generator to avoid using optionals (?
),
so all field resolvers must be implemented in order to avoid compilation errors.
Usage Examples
Override all definition types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
avoidOptionals: true
},
},
},
};
export default config;
Override only specific definition types
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
avoidOptionals: {
field: true,
inputValue: true,
object: true,
defaultValue: true,
}
},
},
},
};
export default config;
showUnusedMappers
type: boolean
default: true
Warns about unused mappers.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
showUnusedMappers: true,
},
},
},
};
export default config;
enumValues
type: EnumValuesMap_1
Overrides the default value of enum values declared in your GraphQL schema, supported
in this plugin because of the need for integration with typescript
package.
See documentation under typescript
plugin for more information and examples.
resolverTypeWrapperSignature
type: string
default: Promise<T> | T
Allow you to override resolverTypeWrapper
definition.
federation
type: boolean
default: false
Supports Apollo Federation
enumPrefix
type: boolean
default: true
Allow you to disable prefixing for generated enums, works in combination with typesPrefix
.
Usage Examples
Disable enum prefixes
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
plugins: ['typescript', 'typescript-resolver'],
config: {
typesPrefix: 'I',
enumPrefix: false
},
},
},
};
export default config;
optionalResolveType
type: boolean
default: false
Sets the __resolveType
field as optional field.
immutableTypes
type: boolean
default: false
Generates immutable types by adding readonly
to properties and uses ReadonlyArray
.
namespacedImportName
type: string
default: ''
Prefixes all GraphQL related generated types with that value, as namespaces import. You can use this feature to allow separation of plugins to different files.
resolverTypeSuffix
type: string
default: Resolvers
Suffix we add to each generated type resolver.
allResolversTypeName
type: string
default: Resolvers
The type name to use when exporting all resolvers signature as unified type.
internalResolversPrefix
type: string
default: '__'
Defines the prefix value used for __resolveType
and __isTypeOf
resolvers.
If you are using mercurius-js
, please set this field to empty string for better compatibility.
onlyResolveTypeForInterfaces
type: boolean
default: false
Turning this flag to true
will generate resolver signature that has only resolveType
for interfaces, forcing developers to write inherited type resolvers in the type itself.
strictScalars
type: boolean
default: false
Makes scalars strict.
If scalars are found in the schema that are not defined in scalars
an error will be thrown during codegen.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
strictScalars: true,
},
},
},
};
export default config;
defaultScalarType
type: string
default: any
Allows you to override the type that unknown scalars will have.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
defaultScalarType: 'unknown'
},
},
},
};
export default config;
scalars
type: ScalarsMap
Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
namingConvention
type: NamingConvention
default: change-case-all#pascalCase
Allow you to override the naming convention of the output.
You can either override all namings, or specify an object with specific custom naming convention per output.
The format of the converter must be a valid module#method
.
Allowed values for specific output are: typeNames
, enumValues
.
You can also use "keep" to keep all GraphQL names as-is.
Additionally, you can set transformUnderscore
to true
if you want to override the default behavior,
which is to preserve underscores.
Available case functions in change-case-all
are camelCase
, capitalCase
, constantCase
, dotCase
, headerCase
, noCase
, paramCase
, pascalCase
, pathCase
, sentenceCase
, snakeCase
, lowerCase
, localeLowerCase
, lowerCaseFirst
, spongeCase
, titleCase
, upperCase
, localeUpperCase
and upperCaseFirst
See more
typesPrefix
typesSuffix
skipTypename
type: boolean
default: false
Does not add __typename
to the generated types, unless it was specified in the selection set.
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
skipTypename: true
},
},
},
};
export default config;
nonOptionalTypename
type: boolean
default: false
Automatically adds __typename
field to the generated types, even when they are not specified
in the selection set, and makes it non-optional
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
nonOptionalTypename: true
},
},
},
};
export default config;
useTypeImports
type: boolean
default: false
Will use import type {}
rather than import {}
when importing only types. This gives
compatibility with TypeScript's "importsNotUsedAsValues": "error" option
Usage Examples
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
useTypeImports: true
},
},
},
};
export default config;
dedupeFragments
type: boolean
default: false
Removes fragment duplicates for reducing data transfer. It is done by removing sub-fragments imports from fragment definition Instead - all of them are imported to the Operation node.
inlineFragmentTypes
type: InlineFragmentTypeOptions
default: inline
Whether fragment types should be inlined into other operations. "inline" is the default behavior and will perform deep inlining fragment types within operation type definitions. "combine" is the previous behavior that uses fragment type references without inlining the types (and might cause issues with deeply nested fragment that uses list types).
emitLegacyCommonJSImports
type: boolean
default: true
Emit legacy common js imports.
Default it will be true
this way it ensure that generated code works with non-compliant bundlers.