diff --git a/frontend/graphql-codegen.ts b/frontend/graphql-codegen.ts index 41bd209fb7..39f3762cbc 100644 --- a/frontend/graphql-codegen.ts +++ b/frontend/graphql-codegen.ts @@ -2,76 +2,77 @@ import { CodegenConfig } from '@graphql-codegen/cli' const PUBLIC_API_URL = process.env.PUBLIC_API_URL || 'http://localhost:8000' -export default (async (): Promise => { - let response +let response: Response - try { - response = await fetch(`${PUBLIC_API_URL}/csrf/`, { - method: 'GET', - }) - } catch { - /* eslint-disable no-console */ - console.log('Failed to fetch CSRF token: make sure the backend is running.') - return - } +try { + response = await fetch(`${PUBLIC_API_URL}/csrf/`, { + method: 'GET', + }) +} catch { + /* eslint-disable no-console */ + console.log('Failed to fetch CSRF token: make sure the backend is running.') + process.exit(1) +} - if (!response.ok) { - throw new Error(`Failed to fetch CSRF token: ${response.status} ${response.statusText}`) - } - const csrfToken = (await response.json()).csrftoken +if (!response.ok) { + console.log(`Failed to fetch CSRF token: ${response.status} ${response.statusText}`) + process.exit(1) +} +const csrfToken = (await response.json()).csrftoken - return { - documents: ['src/**/*.{ts,tsx}', '!src/types/__generated__/**'], - generates: { - './src/': { - config: { - avoidOptionals: { - // Use `null` for nullable fields instead of optionals - field: true, - // Allow nullable input fields to remain unspecified - inputValue: false, - }, - defaultScalarType: 'any', - // Apollo Client always includes `__typename` fields - nonOptionalTypename: true, - // Apollo Client doesn't add the `__typename` field to root types so - // don't generate a type for the `__typename` for root operation types. - skipTypeNameForRoot: true, - }, - // Order of plugins matter - plugins: ['typescript-operations', 'typed-document-node'], - preset: 'near-operation-file', - presetConfig: { - // This should be the file generated by the "typescript" plugin above, - // relative to the directory specified for this configuration - baseTypesPath: './types/__generated__/graphql.ts', - // Relative to the source files - folder: '../../types/__generated__', +const config: CodegenConfig = { + documents: ['src/**/*.{ts,tsx}', '!src/types/__generated__/**'], + generates: { + './src/': { + config: { + avoidOptionals: { + // Use `null` for nullable fields instead of optionals + field: true, + // Allow nullable input fields to remain unspecified + inputValue: false, }, + defaultScalarType: 'any', + // Apollo Client always includes `__typename` fields + nonOptionalTypename: true, + // Apollo Client doesn't add the `__typename` field to root types so + // don't generate a type for the `__typename` for root operation types. + skipTypeNameForRoot: true, }, - './src/types/__generated__/graphql.ts': { - config: { - scalars: { - // eslint-disable-next-line @typescript-eslint/naming-convention - Date: 'string | number', - // eslint-disable-next-line @typescript-eslint/naming-convention - DateTime: 'string | number', - JSON: 'Record', - }, - }, - plugins: ['typescript'], + // Order of plugins matter + plugins: ['typescript-operations', 'typed-document-node'], + preset: 'near-operation-file', + presetConfig: { + // This should be the file generated by the "typescript" plugin above, + // relative to the directory specified for this configuration + baseTypesPath: './types/__generated__/graphql.ts', + // Relative to the source files + folder: '../../types/__generated__', }, }, - // Don't exit with non-zero status when there are no documents - ignoreNoDocuments: true, - overwrite: true, - schema: { - [`${PUBLIC_API_URL}/graphql/`]: { - headers: { - Cookie: `csrftoken=${csrfToken}`, - 'X-CSRFToken': csrfToken, + './src/types/__generated__/graphql.ts': { + config: { + scalars: { + // eslint-disable-next-line @typescript-eslint/naming-convention + Date: 'string | number', + // eslint-disable-next-line @typescript-eslint/naming-convention + DateTime: 'string | number', + JSON: 'Record', }, }, + plugins: ['typescript'], }, - } -})() + }, + // Don't exit with non-zero status when there are no documents + ignoreNoDocuments: true, + overwrite: true, + schema: { + [`${PUBLIC_API_URL}/graphql/`]: { + headers: { + Cookie: `csrftoken=${csrfToken}`, + 'X-CSRFToken': csrfToken, + }, + }, + }, +} + +export default config