-
-
Notifications
You must be signed in to change notification settings - Fork 537
fix(frontend): refactor graphql-codegen config to async function export #3283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8b49c4e
31edc1b
892c04f
c707b81
6cae246
452af4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,68 @@ | ||
| import { CodegenConfig } from '@graphql-codegen/cli' | ||
|
|
||
| const PUBLIC_API_URL = process.env.PUBLIC_API_URL || 'http://localhost:8000' | ||
| type CsrfResponse = { | ||
| csrftoken?: string | ||
| } | ||
|
|
||
| export default (async (): Promise<CodegenConfig> => { | ||
| let response | ||
|
|
||
| export default async function graphqlCodegenConfig(): Promise<CodegenConfig> { | ||
| 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 | ||
| } catch (err) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Failed to fetch CSRF token: make sure the backend is running.', err) | ||
| throw new Error('Failed to fetch CSRF token', { cause: err }) | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch CSRF token: ${response.status} ${response.statusText}`) | ||
| } | ||
| const csrfToken = (await response.json()).csrftoken | ||
|
|
||
| let body: unknown | ||
|
|
||
| try { | ||
| body = await response.json() | ||
| } catch (err) { | ||
| throw new Error( | ||
| `Failed to parse CSRF token response: ${response.status} ${response.statusText}`, | ||
| { cause: err } | ||
| ) | ||
| } | ||
|
|
||
| const data = body as CsrfResponse | ||
|
|
||
| const csrfToken = | ||
| typeof data.csrftoken === 'string' && data.csrftoken.trim() !== '' | ||
| ? data.csrftoken | ||
| : null | ||
|
|
||
|
|
||
| if (!csrfToken) { | ||
| throw new Error("Failed to fetch CSRF token: missing or invalid 'csrftoken' in response body") | ||
| } | ||
|
|
||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please do not remove these comments.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing this out.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, can you also restore the original pull request description format and fill in the details?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, thanks for letting me know . Let me know if there’s anything else you’d like me to update. |
||
| baseTypesPath: './types/__generated__/graphql.ts', | ||
| // Relative to the source files | ||
| folder: '../../types/__generated__', | ||
| }, | ||
| }, | ||
|
|
@@ -62,7 +79,6 @@ export default (async (): Promise<CodegenConfig> => { | |
| plugins: ['typescript'], | ||
| }, | ||
| }, | ||
| // Don't exit with non-zero status when there are no documents | ||
| ignoreNoDocuments: true, | ||
| overwrite: true, | ||
| schema: { | ||
|
|
@@ -74,4 +90,4 @@ export default (async (): Promise<CodegenConfig> => { | |
| }, | ||
| }, | ||
| } | ||
| })() | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.