Skip to content

Instantly share code, notes, and snippets.

@christopher-caldwell
Created September 28, 2023 01:59
Show Gist options
  • Save christopher-caldwell/ccb223dfc6119abb40c0e637067b78eb to your computer and use it in GitHub Desktop.
Save christopher-caldwell/ccb223dfc6119abb40c0e637067b78eb to your computer and use it in GitHub Desktop.
Codegen with mappers to models
import { CodegenConfig } from '@graphql-codegen/cli'
const modelNames = [
'User',
'Contractor',
'JobSite',
'Project',
'PunchListItem',
'ScheduleItem',
'Section',
'Settings',
'Vendor',
]
const generateMappers = (modelNames: string[]) => {
const mappers: Record<string, string> = {}
modelNames.forEach((name) => {
mappers[name] = `@tenacity/models#${name}`
})
return mappers
}
const isGeneratingUi = false
const uiGenConfig = isGeneratingUi
? {
'packages/schema/src/ui.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-query'],
config: {
fetcher: './client#runQuery',
exposeQueryKeys: true,
legacyMode: false,
},
},
}
: undefined
// 'dist/packages/schema/src/schema/index.js'
const config: CodegenConfig = {
overwrite: true,
schema: ['http://localhost:5001/graphql'],
documents: isGeneratingUi ? ['**/*.graphql'] : undefined,
config: {
sort: false,
// skipTypename: true,
scalars: {
Date: 'string',
DateTime: 'Date',
},
},
generates: {
'src/types/autoGenerated.ts': {
plugins: [
'typescript',
'typescript-resolvers',
{
add: {
content:
'export type AllowOptionalRelationship<Resolver> = Resolver extends { __typename?: string } ? Resolver | undefined : Resolver',
},
},
],
config: {
enumsAsConst: true,
allowParentTypeOverride: true,
contextType: './context#ResolverContext',
mappers: {
...generateMappers(modelNames),
},
mapperTypeSuffix: 'Model',
},
},
...uiGenConfig,
},
}
export default config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment