|  | 
|  | 1 | +import { CodegenConfig } from '@graphql-codegen/cli' | 
|  | 2 | + | 
|  | 3 | +const PUBLIC_API_URL = process.env.PUBLIC_API_URL || 'http://localhost:8000' | 
|  | 4 | + | 
|  | 5 | +export default (async (): Promise<CodegenConfig> => { | 
|  | 6 | +  let response | 
|  | 7 | + | 
|  | 8 | +  try { | 
|  | 9 | +    response = await fetch(`${PUBLIC_API_URL}/csrf/`, { | 
|  | 10 | +      method: 'GET', | 
|  | 11 | +    }) | 
|  | 12 | +  } catch { | 
|  | 13 | +    /* eslint-disable no-console */ | 
|  | 14 | +    console.log('Failed to fetch CSRF token: make sure the backend is running.') | 
|  | 15 | +    return | 
|  | 16 | +  } | 
|  | 17 | + | 
|  | 18 | +  if (!response.ok) { | 
|  | 19 | +    throw new Error(`Failed to fetch CSRF token: ${response.status} ${response.statusText}`) | 
|  | 20 | +  } | 
|  | 21 | +  const csrfToken = (await response.json()).csrftoken | 
|  | 22 | + | 
|  | 23 | +  return { | 
|  | 24 | +    documents: ['src/**/*.{ts,tsx}', '!src/types/__generated__/**'], | 
|  | 25 | +    generates: { | 
|  | 26 | +      './src/': { | 
|  | 27 | +        config: { | 
|  | 28 | +          avoidOptionals: { | 
|  | 29 | +            // Use `null` for nullable fields instead of optionals | 
|  | 30 | +            field: true, | 
|  | 31 | +            // Allow nullable input fields to remain unspecified | 
|  | 32 | +            inputValue: false, | 
|  | 33 | +          }, | 
|  | 34 | +          // Use `unknown` instead of `any` for unconfigured scalars | 
|  | 35 | +          defaultScalarType: 'unknown', | 
|  | 36 | +          // Apollo Client always includes `__typename` fields | 
|  | 37 | +          nonOptionalTypename: true, | 
|  | 38 | +          // Apollo Client doesn't add the `__typename` field to root types so | 
|  | 39 | +          // don't generate a type for the `__typename` for root operation types. | 
|  | 40 | +          skipTypeNameForRoot: true, | 
|  | 41 | +        }, | 
|  | 42 | +        // Order of plugins matter | 
|  | 43 | +        plugins: ['typescript-operations', 'typed-document-node'], | 
|  | 44 | +        preset: 'near-operation-file', | 
|  | 45 | +        presetConfig: { | 
|  | 46 | +          // This should be the file generated by the "typescript" plugin above, | 
|  | 47 | +          // relative to the directory specified for this configuration | 
|  | 48 | +          baseTypesPath: './types/__generated__/graphql.ts', | 
|  | 49 | +          // Relative to the source files | 
|  | 50 | +          folder: '../../types/__generated__', | 
|  | 51 | +        }, | 
|  | 52 | +      }, | 
|  | 53 | +      './src/types/__generated__/graphql.ts': { | 
|  | 54 | +        plugins: ['typescript'], | 
|  | 55 | +      }, | 
|  | 56 | +    }, | 
|  | 57 | +    // Don't exit with non-zero status when there are no documents | 
|  | 58 | +    ignoreNoDocuments: true, | 
|  | 59 | +    overwrite: true, | 
|  | 60 | +    schema: { | 
|  | 61 | +      [`${PUBLIC_API_URL}/graphql/`]: { | 
|  | 62 | +        headers: { | 
|  | 63 | +          Cookie: `csrftoken=${csrfToken}`, | 
|  | 64 | +          'X-CSRFToken': csrfToken, | 
|  | 65 | +        }, | 
|  | 66 | +      }, | 
|  | 67 | +    }, | 
|  | 68 | +  } | 
|  | 69 | +})() | 
0 commit comments