website/src/pages/plugins/typescript/typescript-apollo-next.mdx
import { PluginHeader } from '@/components/plugin' import { pluginGetStaticProps } from '@/lib/plugin-get-static-props' export const getStaticProps = pluginGetStaticProps(__filename, { hasOperationsNote: true })
<PluginHeader />This plugin generates:
getServerSideProps or getStaticPropsIt extends the basic TypeScript plugins: @graphql-codegen/typescript, @graphql-codegen/typescript-operations - and thus shares a similar configuration.
Next.js recently introduced getServerSideProps and getStaticProps which doesn't allow to use the HOC pattern adopted by the official apollo graphql plugin (based on getInitialProps). At the same time the SSR method offered by apollo client (getDataFromTree) enforces the React app to render multiple times in order to collect and fetch all the relevant queries.
By declaring a top level query we can save rendering time and provide a simpler pattern which works with getServerSideProps. Note that this pattern requires each SSR query to run at top level. Example
apolloReactCommonImportFromtype: string
default: "
Customize the package where apollo-react common lib is loaded from.
apolloImportFromtype: string
default: "
Customize the package where apollo-client lib is loaded from.
apolloCacheImportFromtype: string
default: "
Customize the package where apollo-cache-inmemory lib is loaded from.
apolloReactHooksImportFromtype: string
default: "
Customize the package where apollo-react hooks lib is loaded from.
reactApolloVersiontype: number (values: 2, 3)
default: 2
Sets the version of react-apollo.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
reactApolloVersion: 3
}
}
}
}
export default config
withHOCtype: boolean
default: true
Customized the output by enabling/disabling the HOC.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
withHOC: 'I'
}
}
}
}
export default config
withHookstype: boolean
default: false
Customized the output by enabling/disabling the generated React Hooks.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
withHooks: 'I'
}
}
}
}
export default config
excludePatternstype: string
default: ''
Regexp to exclude a certain operation name.
excludePatternsOptionstype: string
default: ''
Regexp options to exclude a certain operation name.
pretype: string
default: ''
Add custom code before each operation.
posttype: string
default: ''
Add custom code after each operation.
customImportstype: string
default: ''
Add custom imports needed by pre/post.
gqlImporttype: string
default: gql#graphql-tag
Customize from which module will gql be imported from.
This is useful if you want to use modules other than graphql-tag, e.g. graphql.macro.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
gqlImport: 'graphql.macro#gql'
}
}
}
}
export default config
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
gqlImport: 'gatsby#graphql'
}
}
}
}
export default config
noExporttype: boolean
default: false
Set this configuration to true if you wish to tell codegen to generate code with no export identifier.
dedupeOperationSuffixtype: boolean
default: false
Set this configuration to true if you wish to make sure to remove duplicate operation name suffix.
omitOperationSuffixtype: boolean
default: false
Set this configuration to true if you wish to disable auto add suffix of operation name, like Query, Mutation, Subscription, Fragment.
operationResultSuffixtype: string
default: ``
Adds a suffix to generated operation result type names
documentVariablePrefixtype: string
default: ``
Changes the GraphQL operations variables prefix.
documentVariableSuffixtype: string
default: Document
Changes the GraphQL operations variables suffix.
fragmentVariablePrefixtype: string
default: ``
Changes the GraphQL fragments variables prefix.
fragmentVariableSuffixtype: string
default: FragmentDoc
Changes the GraphQL fragments variables suffix.
documentModetype: DocumentMode
default: graphQLTag
Declares how DocumentNode are created:
graphQLTag: graphql-tag or other modules (check gqlImport) will be used to generate document nodes. If this is used, document nodes are generated on client side i.e. the module used to generate this will be shipped to the clientdocumentNode: document nodes will be generated as objects when we generate the templates.documentNodeImportFragments: Similar to documentNode except it imports external fragments instead of embedding them.external: document nodes are imported from an external file. To be used with importDocumentNodeExternallyFromimportOperationTypesFromtype: string
default: ``
This config is used internally by presets, but you can use it manually to tell codegen to prefix all base types that it's using.
This is useful if you wish to generate base types from typescript-operations plugin into a different file, and import it from there.
importDocumentNodeExternallyFromtype: string
default: ``
This config should be used if documentMode is external. This has 2 usage:
graphql-tag in a separate file and export the generated documentnear-operation-file preset to import document nodes from those files. If these files are .graphql files, we make use of webpack loader.import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
documentMode: 'external',
importDocumentNodeExternallyFrom: 'path/to/document-node-file'
}
}
}
}
export default config
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
documentMode: 'external',
importDocumentNodeExternallyFrom: 'near-operation-file'
}
}
}
}
export default config
scalarstype: ScalarsMap
Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
scalars: {
DateTime: 'Date',
JSON: '{ [key: string]: any }'
}
}
}
}
}
export default config
namingConventiontype: 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.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
namingConvention: 'change-case-all#lowerCase'
}
}
}
}
export default config
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
namingConvention: {
typeNames: 'change-case-all#pascalCase',
enumValues: 'change-case-all#upperCase'
}
}
}
}
}
export default config
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
namingConvention: 'keep'
}
}
}
}
export default config
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file': {
// plugins...
config: {
namingConvention: {
typeNames: 'change-case-all#pascalCase',
transformUnderscore: true
}
}
}
}
}
export default config
typesPrefixtype: string
default: ``
Prefixes all the generated types.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
typesPrefix: 'I'
}
}
}
}
export default config
skipTypenametype: boolean
default: false
Does not add __typename to the generated types, unless it was specified in the selection set.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
skipTypename: true
}
}
}
}
export default config
nonOptionalTypenametype: 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.
import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
// ...
generates: {
'path/to/file.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
nonOptionalTypename: true
}
}
}
}
export default config