@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 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 config, a config option could be added in `codegen.json`.
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
- "config": {
98
- "skipTypename": false
99
- }
97
+ "preset": "...",
98
+ "presetConfig": { ... },
99
+ "plugins": [ ... ],
100
+ "config": { ... }
100
101
  }
101
102
  ]
102
103
  ```
103
104
 
104
- > List of options could be find in https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toptal/davinci-graphql-codegen",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "Codegen",
5
5
  "author": "Toptal",
6
6
  "license": "SEE LICENSE IN LICENSE.MD",
@@ -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 codegen = await readConfig(config)
17
+ const codegenConfigurations = await readConfig(config)
17
18
 
18
- for (const { schema, documents, config: customConfig = {} } of codegen) {
19
- await generateOperations({
20
- schema,
21
- documents,
22
- extension,
23
- experiments,
24
- schemaName,
25
- config: customConfig,
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 codegen = await readConfig(config)
8
+ const codegenConfigurations = await readConfig(config)
8
9
 
9
- for (const { schema, documents, target } of codegen) {
10
- await generateSchema({
11
- schema,
12
- documents,
13
- target,
14
- projectId,
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
 
@@ -20,6 +20,7 @@ const commonTypesConfig = {
20
20
  Time: 'string',
21
21
  TimeOfDay: 'string',
22
22
  Upload: 'unknown',
23
+ HTML: 'string',
23
24
  },
24
25
  }
25
26
 
@@ -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
- const { target, schemaPath, schemaPathResolution } = getSchemaPath(schema)
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
- await generate(
46
- {
47
- documents: isFragmentResolverExperimentEnabled
48
- ? docsPath.flatMap(fragmentImportResolverGraphQL)
49
- : docsPath,
50
- config: {
51
- ...commonOperationTypesConfig,
52
- },
53
- generates: {
54
- [schemaPath]: {
55
- schema: schemaPathResolution,
56
- preset: 'near-operation-file',
57
- presetConfig: {
58
- extension,
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
- hooks: {
73
- afterAllFileWrite: [
74
- async (...filePaths) => {
75
- for (const path of filePaths) {
76
- const rawText = readFileSync(path, 'utf8')
77
- const options = await prettier.resolveConfig(dirname(path))
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
- true
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,7 @@
1
+ export const getConfigurationType = codegenConfiguration => {
2
+ if (codegenConfiguration && 'documents' in codegenConfiguration) {
3
+ return 'operations'
4
+ }
5
+
6
+ return 'schema'
7
+ }
@@ -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('/').pop()
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
- const schemaPath = '@toptal/modularity-template-graphql/talent'
14
-
15
- expect(getMonorepoSchemaPath(schemaPath)).toEqual({
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
  })