Skip to content

Commit 55f86cc

Browse files
remove fetchCsrfTokenServer and use a config constructor
1 parent 5c8b6e1 commit 55f86cc

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

frontend/graphql-codegen.ts

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { CodegenConfig } from '@graphql-codegen/cli'
22

33
const PUBLIC_API_URL = process.env.PUBLIC_API_URL || 'http://localhost:8000'
4-
let csrfToken = null
54

6-
const fetchCsrfTokenServer = async (): Promise<string> => {
7-
if (csrfToken != null) {
8-
return csrfToken
9-
}
5+
const createCodegenConfig = async (): Promise<CodegenConfig> => {
106
const response = await fetch(`${PUBLIC_API_URL}/csrf/`, {
117
credentials: 'include',
128
method: 'GET',
@@ -16,44 +12,41 @@ const fetchCsrfTokenServer = async (): Promise<string> => {
1612
throw new Error(`Failed to fetch CSRF token: ${response.status} ${response.statusText}`)
1713
}
1814
const data = await response.json()
19-
csrfToken = data.csrftoken
20-
21-
return data.csrftoken
22-
}
15+
const csrfToken = data.csrftoken
2316

24-
const config: CodegenConfig = {
25-
documents: ['src/**/*.{ts,tsx}'],
26-
generates: {
27-
'./src/types/__generated__/graphql.ts': {
28-
config: {
29-
avoidOptionals: {
30-
// Use `null` for nullable fields instead of optionals
31-
field: true,
32-
// Allow nullable input fields to remain unspecified
33-
inputValue: false,
17+
return {
18+
documents: ['src/**/*.{ts,tsx}'],
19+
generates: {
20+
'./src/types/__generated__/graphql.ts': {
21+
config: {
22+
avoidOptionals: {
23+
// Use `null` for nullable fields instead of optionals
24+
field: true,
25+
// Allow nullable input fields to remain unspecified
26+
inputValue: false,
27+
},
28+
// Use `unknown` instead of `any` for unconfigured scalars
29+
defaultScalarType: 'unknown',
30+
// Apollo Client always includes `__typename` fields
31+
nonOptionalTypename: true,
32+
// Apollo Client doesn't add the `__typename` field to root types so
33+
// don't generate a type for the `__typename` for root operation types.
34+
skipTypeNameForRoot: true,
3435
},
35-
// Use `unknown` instead of `any` for unconfigured scalars
36-
defaultScalarType: 'unknown',
37-
// Apollo Client always includes `__typename` fields
38-
nonOptionalTypename: true,
39-
// Apollo Client doesn't add the `__typename` field to root types so
40-
// don't generate a type for the `__typename` for root operation types.
41-
skipTypeNameForRoot: true,
36+
plugins: ['typescript', 'typescript-operations'],
4237
},
43-
plugins: ['typescript', 'typescript-operations'],
4438
},
45-
},
46-
// Don't exit with non-zero status when there are no documents
47-
ignoreNoDocuments: true,
48-
overwrite: true,
49-
schema: {
50-
[`${PUBLIC_API_URL}/graphql/`]: {
51-
headers: {
52-
Cookie: `csrftoken=${await fetchCsrfTokenServer()}`,
53-
'x-csrftoken': `${await fetchCsrfTokenServer()}`,
39+
// Don't exit with non-zero status when there are no documents
40+
ignoreNoDocuments: true,
41+
overwrite: true,
42+
schema: {
43+
[`${PUBLIC_API_URL}/graphql/`]: {
44+
headers: {
45+
Cookie: `csrftoken=${csrfToken}`,
46+
'x-csrftoken': `${csrfToken}`,
47+
},
5448
},
5549
},
56-
},
50+
}
5751
}
58-
59-
export default config
52+
export default await createCodegenConfig()

0 commit comments

Comments
 (0)