@toptal/davinci-graphql-codegen 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +6 -5
- package/package.json +1 -1
- package/src/commands/generate-operations.js +31 -10
- package/src/commands/generate-schema.js +19 -8
- package/src/config/config.js +1 -0
- package/src/generate/operations.js +60 -43
- package/src/services/get-configutation-type.js +7 -0
- package/src/services/get-configutation-type.test.js +20 -0
- package/src/services/get-monorepo-schema-path.js +1 -1
- package/src/services/get-monorepo-schema-path.test.js +12 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @toptal/davinci-graphql-codegen
|
|
2
2
|
|
|
3
|
+
## 3.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#2085](https://github.com/toptal/davinci/pull/2085) [`eb0f3e46`](https://github.com/toptal/davinci/commit/eb0f3e463533ef5ae8e2391c17b0b2d2956918bf) Thanks [@sashuk](https://github.com/sashuk)!
|
|
8
|
+
- extend configuration of operations generation
|
|
9
|
+
|
|
3
10
|
## 3.2.0
|
|
4
11
|
|
|
5
12
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -86,7 +86,7 @@ The above script will search for `codegen-schema.json` within `./graphql`.
|
|
|
86
86
|
|
|
87
87
|
## Using custom config
|
|
88
88
|
|
|
89
|
-
To override default
|
|
89
|
+
To override default configuration, `preset`, `presetConfig`, `plugins`, and `config` options can be added to `codegen.json`.
|
|
90
90
|
|
|
91
91
|
```json
|
|
92
92
|
// codegen.json
|
|
@@ -94,14 +94,15 @@ To override default config, a config option could be added in `codegen.json`.
|
|
|
94
94
|
{
|
|
95
95
|
"schema": "@toptal/modularity-template-graphql/talent",
|
|
96
96
|
"documents": "src/**/*.gql",
|
|
97
|
-
"
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
"preset": "...",
|
|
98
|
+
"presetConfig": { ... },
|
|
99
|
+
"plugins": [ ... ],
|
|
100
|
+
"config": { ... }
|
|
100
101
|
}
|
|
101
102
|
]
|
|
102
103
|
```
|
|
103
104
|
|
|
104
|
-
>
|
|
105
|
+
> Please see the https://the-guild.dev/graphql/codegen/docs/config-reference/codegen-config for more information about these options.
|
|
105
106
|
|
|
106
107
|
## Customizing the extension of generated files
|
|
107
108
|
|
package/package.json
CHANGED
|
@@ -3,6 +3,7 @@ import { createOption } from '@toptal/davinci-cli-shared'
|
|
|
3
3
|
|
|
4
4
|
import { generateOperations } from '../generate/index.js'
|
|
5
5
|
import readConfig from '../read-config/index.js'
|
|
6
|
+
import { getConfigurationType } from '../services/get-configutation-type.js'
|
|
6
7
|
|
|
7
8
|
const codegenGenerateOperations = async ({
|
|
8
9
|
config,
|
|
@@ -13,17 +14,37 @@ const codegenGenerateOperations = async ({
|
|
|
13
14
|
}) => {
|
|
14
15
|
process.env.VERBOSE = verbose
|
|
15
16
|
|
|
16
|
-
const
|
|
17
|
+
const codegenConfigurations = await readConfig(config)
|
|
17
18
|
|
|
18
|
-
for (const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
for (const codegenConfiguration of codegenConfigurations) {
|
|
20
|
+
if (getConfigurationType(codegenConfiguration) === 'operations') {
|
|
21
|
+
const {
|
|
22
|
+
schema,
|
|
23
|
+
documents,
|
|
24
|
+
preset,
|
|
25
|
+
presetConfig,
|
|
26
|
+
plugins,
|
|
27
|
+
config: customConfig = {},
|
|
28
|
+
} = codegenConfiguration
|
|
29
|
+
|
|
30
|
+
await generateOperations({
|
|
31
|
+
schema,
|
|
32
|
+
documents,
|
|
33
|
+
preset,
|
|
34
|
+
presetConfig,
|
|
35
|
+
plugins,
|
|
36
|
+
extension,
|
|
37
|
+
experiments,
|
|
38
|
+
schemaName,
|
|
39
|
+
config: customConfig,
|
|
40
|
+
})
|
|
41
|
+
} else {
|
|
42
|
+
console.log(
|
|
43
|
+
`ℹ️ Operations generation configuration was skipped when reading [${chalk.yellow(
|
|
44
|
+
config
|
|
45
|
+
)}]`
|
|
46
|
+
)
|
|
47
|
+
}
|
|
27
48
|
}
|
|
28
49
|
}
|
|
29
50
|
|
|
@@ -2,17 +2,28 @@ import chalk from 'chalk'
|
|
|
2
2
|
|
|
3
3
|
import { generateSchema } from '../generate/index.js'
|
|
4
4
|
import readConfig from '../read-config/index.js'
|
|
5
|
+
import { getConfigurationType } from '../services/get-configutation-type.js'
|
|
5
6
|
|
|
6
7
|
const codegenGenerateSchema = async ({ projectId, config }) => {
|
|
7
|
-
const
|
|
8
|
+
const codegenConfigurations = await readConfig(config)
|
|
8
9
|
|
|
9
|
-
for (const
|
|
10
|
-
|
|
11
|
-
schema,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
for (const codegenConfiguration of codegenConfigurations) {
|
|
11
|
+
if (getConfigurationType(codegenConfiguration) === 'schema') {
|
|
12
|
+
const { schema, documents, target } = codegenConfiguration
|
|
13
|
+
|
|
14
|
+
await generateSchema({
|
|
15
|
+
schema,
|
|
16
|
+
documents,
|
|
17
|
+
target,
|
|
18
|
+
projectId,
|
|
19
|
+
})
|
|
20
|
+
} else {
|
|
21
|
+
console.log(
|
|
22
|
+
`ℹ️ Schema generation configuration was skipped when reading [${chalk.yellow(
|
|
23
|
+
config
|
|
24
|
+
)}]`
|
|
25
|
+
)
|
|
26
|
+
}
|
|
16
27
|
}
|
|
17
28
|
}
|
|
18
29
|
|
package/src/config/config.js
CHANGED
|
@@ -18,9 +18,13 @@ const { readFileSync, writeFileSync } = fsExtra
|
|
|
18
18
|
const generateOperations = async ({
|
|
19
19
|
schema,
|
|
20
20
|
documents,
|
|
21
|
+
preset,
|
|
22
|
+
presetConfig,
|
|
23
|
+
plugins,
|
|
21
24
|
extension,
|
|
22
25
|
experiments,
|
|
23
26
|
schemaName,
|
|
27
|
+
config,
|
|
24
28
|
}) => {
|
|
25
29
|
const enabledExperiments = experiments ? experiments.split(',') : []
|
|
26
30
|
|
|
@@ -36,59 +40,72 @@ const generateOperations = async ({
|
|
|
36
40
|
enabledExperiments.includes('fragment-resolver')
|
|
37
41
|
|
|
38
42
|
const docsPath = castArray(documents).map(getRelativeFilePath)
|
|
43
|
+
const schemas = castArray(schema)
|
|
39
44
|
|
|
40
|
-
|
|
45
|
+
// Picking the primary schema for the operation generation
|
|
46
|
+
const primarySchema = schemas[0]
|
|
47
|
+
const { target, schemaPath } = getSchemaPath(primarySchema)
|
|
48
|
+
|
|
49
|
+
const schemasPathResoltuions = schemas.map(
|
|
50
|
+
schema => getSchemaPath(schema).schemaPathResolution
|
|
51
|
+
)
|
|
41
52
|
|
|
42
53
|
console.log(`ℹ️ Generating [${chalk.green(target)}] operations`)
|
|
43
54
|
console.log(`ℹ️ Documents pattern: [${chalk.green(documents)}]`)
|
|
44
55
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
baseTypesPath: `~${schema}/${schemaName}`,
|
|
60
|
-
},
|
|
61
|
-
plugins: [
|
|
62
|
-
'typescript-operations',
|
|
63
|
-
'typed-document-node',
|
|
64
|
-
{
|
|
65
|
-
add: {
|
|
66
|
-
content: autoGenerationComments,
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
],
|
|
56
|
+
const gqlConfiguration = {
|
|
57
|
+
documents: isFragmentResolverExperimentEnabled
|
|
58
|
+
? docsPath.flatMap(fragmentImportResolverGraphQL)
|
|
59
|
+
: docsPath,
|
|
60
|
+
config: {
|
|
61
|
+
...commonOperationTypesConfig,
|
|
62
|
+
},
|
|
63
|
+
generates: {
|
|
64
|
+
[schemaPath]: {
|
|
65
|
+
schema: schemasPathResoltuions,
|
|
66
|
+
preset: preset || 'near-operation-file',
|
|
67
|
+
presetConfig: presetConfig || {
|
|
68
|
+
extension,
|
|
69
|
+
baseTypesPath: `~${primarySchema}/${schemaName}`,
|
|
70
70
|
},
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const formattedText = prettier.format(rawText, {
|
|
80
|
-
parser: 'typescript',
|
|
81
|
-
...options,
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
writeFileSync(path, formattedText)
|
|
85
|
-
}
|
|
71
|
+
plugins: plugins || [
|
|
72
|
+
'typescript-operations',
|
|
73
|
+
'typed-document-node',
|
|
74
|
+
{
|
|
75
|
+
add: {
|
|
76
|
+
content: autoGenerationComments,
|
|
77
|
+
},
|
|
86
78
|
},
|
|
87
79
|
],
|
|
80
|
+
config: config || undefined,
|
|
88
81
|
},
|
|
89
82
|
},
|
|
90
|
-
|
|
91
|
-
|
|
83
|
+
hooks: {
|
|
84
|
+
afterAllFileWrite: [
|
|
85
|
+
async (...filePaths) => {
|
|
86
|
+
for (const path of filePaths) {
|
|
87
|
+
const rawText = readFileSync(path, 'utf8')
|
|
88
|
+
const options = await prettier.resolveConfig(dirname(path))
|
|
89
|
+
|
|
90
|
+
const formattedText = prettier.format(rawText, {
|
|
91
|
+
parser: 'typescript',
|
|
92
|
+
...options,
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
writeFileSync(path, formattedText)
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (process.env.VERBOSE) {
|
|
103
|
+
console.log(`ℹ️ GQL configuration:
|
|
104
|
+
${JSON.stringify(gqlConfiguration, null, 2)}
|
|
105
|
+
`)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
await generate(gqlConfiguration, true)
|
|
92
109
|
}
|
|
93
110
|
|
|
94
111
|
export default generateOperations
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { getConfigurationType } from './get-configutation-type.js'
|
|
2
|
+
|
|
3
|
+
describe('getConfigutationType', () => {
|
|
4
|
+
describe('when configuration contains "documents" field', () => {
|
|
5
|
+
it('identifies configuration as a configuration used for generating operations', () => {
|
|
6
|
+
expect(
|
|
7
|
+
getConfigurationType({
|
|
8
|
+
schema: ['./abc.graphql'],
|
|
9
|
+
documents: ['**/*.ts'],
|
|
10
|
+
})
|
|
11
|
+
).toBe('operations')
|
|
12
|
+
})
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
describe('when configuration does not contain "documents" field', () => {
|
|
16
|
+
it('identifies configuration as a configuration used for generating schema', () => {
|
|
17
|
+
expect(getConfigurationType({ schema: ['./abc.graphql'] })).toBe('schema')
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
})
|
|
@@ -4,7 +4,7 @@ const localRequire = createRequire(import.meta.url)
|
|
|
4
4
|
|
|
5
5
|
export const createGetMonorepoSchemaPathResolver = pathResolver => schema => {
|
|
6
6
|
// @toptal/modularity-template-graphql/talent -> talent
|
|
7
|
-
const target = schema.split('/').
|
|
7
|
+
const target = schema.split('/').slice(2).join('/')
|
|
8
8
|
// @toptal/modularity-template-graphql/talent -> @toptal/modularity-template-graphql
|
|
9
9
|
const schemaPath = schema.split('/').slice(0, 2).join('/')
|
|
10
10
|
|
|
@@ -10,13 +10,22 @@ const getMonorepoSchemaPath =
|
|
|
10
10
|
|
|
11
11
|
describe('getMonorepoSchemaPath', () => {
|
|
12
12
|
it('returns target, schemaPath and schemaPathResolution', () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
expect(
|
|
14
|
+
getMonorepoSchemaPath('@toptal/modularity-template-graphql/talent')
|
|
15
|
+
).toEqual({
|
|
16
16
|
target: 'talent',
|
|
17
17
|
schemaPath: '@toptal/modularity-template-graphql',
|
|
18
18
|
schemaPathResolution:
|
|
19
19
|
'home/users/johndoe/toptal/libs/graphql/schema.graphql',
|
|
20
20
|
})
|
|
21
|
+
|
|
22
|
+
expect(
|
|
23
|
+
getMonorepoSchemaPath('@toptal/modularity-template-graphql/talent/schema')
|
|
24
|
+
).toEqual({
|
|
25
|
+
target: 'talent/schema',
|
|
26
|
+
schemaPath: '@toptal/modularity-template-graphql',
|
|
27
|
+
schemaPathResolution:
|
|
28
|
+
'home/users/johndoe/toptal/libs/graphql/schema.graphql',
|
|
29
|
+
})
|
|
21
30
|
})
|
|
22
31
|
})
|