-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add graphql-codegen support * Add `PACKAGE_JSON_PATH` to graphql-codegen * cleenup tests * cleenup logs --------- Co-authored-by: Lars Kappert <[email protected]>
- Loading branch information
Showing
11 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module.exports = { | ||
schema: 'schema.graphql', | ||
overwrite: true, | ||
generates: { | ||
'./graphql.schema.json': { | ||
plugins: ['introspection'], | ||
}, | ||
'./graphql.schema.graphql': { | ||
'schema-ast': {}, | ||
}, | ||
'./src/generated/graphql.ts': { | ||
documents: ['./src/**/*.tsx'], | ||
plugins: ['typescript', 'typescript-operations', 'typescript-urql'], | ||
}, | ||
'./lib/': { | ||
documents: ['./lib/**/*.tsx'], | ||
preset: 'near-operation-file-preset', | ||
plugins: ['typescript-operations', 'typescript-msw'], | ||
}, | ||
}, | ||
}; |
3 changes: 3 additions & 0 deletions
3
fixtures/plugins/graphql-codegen/node_modules/@graphql-codegen/cli/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "@fixtures/_template", | ||
"version": "*", | ||
"devDependencies": { | ||
"@graphql-codegen/cli": "*" | ||
}, | ||
"codegen": { | ||
"schema": "schema.graphql", | ||
"documents": ["src/**/*.tsx", "!src/gql/**/*"], | ||
"generates": { | ||
"./src/gql/": { | ||
"preset": "client" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Graphql Codegen | ||
|
||
## Enabled | ||
|
||
This plugin is enabled when any of the following package names and/or regular expressions has a match in `dependencies` | ||
or `devDependencies`: | ||
|
||
- `/^@graphql-codegen\//` | ||
|
||
## Default configuration | ||
|
||
```json | ||
{ | ||
"graphql-codegen": { | ||
"config": ["codegen.{ts,js,json,yml,mjs,cts}", "package.json"] | ||
} | ||
} | ||
``` | ||
|
||
Also see [Knip plugins][1] for more information about plugins. | ||
|
||
[1]: https://github.com/webpro/knip/blob/main/README.md#plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { timerify } from '../../util/Performance.js'; | ||
import { hasDependency, load } from '../../util/plugin.js'; | ||
import { isConfigurationOutput } from './types.js'; | ||
import type { ConfiguredPlugin, GraphqlCodegenTypes, PresetNames } from './types.js'; | ||
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js'; | ||
|
||
export const NAME = 'Graphql Codegen'; | ||
|
||
/** @public */ | ||
export const ENABLERS = [/^@graphql-codegen\//]; | ||
|
||
export const PACKAGE_JSON_PATH = 'codegen'; | ||
|
||
export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS); | ||
|
||
export const CONFIG_FILE_PATTERNS = ['codegen.{ts,js,json,yml,mjs,cts}', 'package.json']; | ||
|
||
const findPluginDependencies: GenericPluginCallback = async (configFilePath, options) => { | ||
const { manifest, isProduction } = options; | ||
|
||
if (isProduction) return []; | ||
|
||
// load configuration file from `configFilePath` (or grab `manifest` for package.json) | ||
// load(FAKE_PATH) will return `undefined` | ||
const localConfig: GraphqlCodegenTypes | undefined = configFilePath.endsWith('package.json') | ||
? manifest[PACKAGE_JSON_PATH] | ||
: await load(configFilePath); | ||
|
||
if (!localConfig) return []; | ||
|
||
const generateSet = Object.values(localConfig.generates); | ||
|
||
const configurationOutput = generateSet.filter(isConfigurationOutput); | ||
|
||
const presets = configurationOutput | ||
.map(configOutput => (configOutput.preset ? configOutput.preset : undefined)) | ||
.filter((preset): preset is PresetNames => typeof preset === 'string') | ||
.map(presetName => `@graphql-codegen/${presetName}${presetName.endsWith('-preset') ? '' : '-preset'}`); | ||
|
||
const flatPlugins = generateSet | ||
.filter((config): config is ConfiguredPlugin => !isConfigurationOutput(config)) | ||
.map(item => Object.keys(item)) | ||
.flat() | ||
.map(plugin => `@graphql-codegen/${plugin}`); | ||
|
||
const nestedPlugins = configurationOutput | ||
.map(configOutput => (configOutput.plugins ? configOutput.plugins : [])) | ||
.flat() | ||
.map(plugin => `@graphql-codegen/${plugin}`); | ||
|
||
return [...presets, ...flatPlugins, ...nestedPlugins]; | ||
}; | ||
|
||
export const findDependencies = timerify(findPluginDependencies); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
type PluginConfig<T = unknown> = { [key: string]: T }; | ||
export interface ConfiguredPlugin { | ||
[name: string]: PluginConfig; | ||
} | ||
type NamedPlugin = string; | ||
|
||
type OutputConfig = NamedPlugin | ConfiguredPlugin; | ||
type PresetNamesBase = 'client' | 'near-operation-file' | 'gql-tag-operations' | 'graphql-modules' | 'import-types'; | ||
export type PresetNames = `${PresetNamesBase}-preset` | PresetNamesBase; | ||
|
||
type OutputPreset = { | ||
buildGeneratesSection: (options: unknown) => Promise<unknown>; | ||
prepareDocuments?: (outputFilePath: string, outputSpecificDocuments: unknown) => Promise<unknown>; | ||
}; | ||
|
||
export function isConfigurationOutput(config: ConfiguredOutput | ConfiguredPlugin[]): config is ConfiguredOutput { | ||
return 'preset' in config || 'plugins' in config; | ||
} | ||
|
||
interface ConfiguredOutput { | ||
/** | ||
* @type array | ||
* @items { "$ref": "#/definitions/GeneratedPluginsMap" } | ||
* @description List of plugins to apply to this current output file. | ||
* | ||
* You can either specify plugins from the community using the NPM package name (after you installed it in your project), or you can use a path to a local file for custom plugins. | ||
* | ||
* You can find a list of available plugins here: https://the-guild.dev/graphql/codegen/docs/plugins/index | ||
* Need a custom plugin? read this: https://the-guild.dev/graphql/codegen/docs/custom-codegen/index | ||
*/ | ||
plugins?: OutputConfig[]; | ||
/** | ||
* @description If your setup uses Preset to have a more dynamic setup and output, set the name of your preset here. | ||
* | ||
* Presets are a way to have more than one file output, for example: https://the-guild.dev/graphql/codegen/docs/presets/near-operation-file | ||
* | ||
* You can either specify a preset from the community using the NPM package name (after you installed it in your project), or you can use a path to a local file for a custom preset. | ||
* | ||
* List of available presets: https://graphql-code-generator.com/docs/presets/presets-index | ||
*/ | ||
preset?: PresetNames | OutputPreset; | ||
} | ||
// Extracted from https://github.com/dotansimha/graphql-code-generator/blob/master/packages/utils/plugins-helpers/src/types.ts | ||
export interface GraphqlCodegenTypes { | ||
/** | ||
* @description A map where the key represents an output path for the generated code and the value represents a set of options which are relevant for that specific file. | ||
* | ||
* For more details: https://graphql-code-generator.com/docs/config-reference/codegen-config | ||
*/ | ||
generates: { | ||
[outputPath: string]: ConfiguredOutput | ConfiguredPlugin[]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import assert from 'node:assert/strict'; | ||
import test from 'node:test'; | ||
import { main } from '../../src/index.js'; | ||
import * as graphqlCodegen from '../../src/plugins/graphql-codegen/index.js'; | ||
import { resolve, join } from '../../src/util/path.js'; | ||
import baseArguments from '../helpers/baseArguments.js'; | ||
import baseCounters from '../helpers/baseCounters.js'; | ||
import { getManifest, pluginConfig as config } from '../helpers/index.js'; | ||
|
||
const cwd = resolve('fixtures/plugins/graphql-codegen'); | ||
const manifest = getManifest(cwd); | ||
|
||
test('Find dependencies in graphql-codegen configuration (json)', async () => { | ||
const configFilePath = join(cwd, 'package.json'); | ||
const dependencies = await graphqlCodegen.findDependencies(configFilePath, { manifest, config }); | ||
assert.deepEqual(dependencies, ['@graphql-codegen/client-preset']); | ||
}); | ||
|
||
test('Find dependencies in graphql-codegen configuration (codegen.ts)', async () => { | ||
const configFilePath = join(cwd, 'codegen.ts'); | ||
const dependencies = await graphqlCodegen.findDependencies(configFilePath, { manifest, config }); | ||
assert.deepEqual(dependencies, [ | ||
'@graphql-codegen/near-operation-file-preset', | ||
'@graphql-codegen/schema-ast', | ||
'@graphql-codegen/introspection', | ||
'@graphql-codegen/typescript', | ||
'@graphql-codegen/typescript-operations', | ||
'@graphql-codegen/typescript-urql', | ||
'@graphql-codegen/typescript-operations', | ||
'@graphql-codegen/typescript-msw', | ||
]); | ||
}); | ||
|
||
test('Find dependencies in graphql-codegen configuration (codegen.ts function)', async () => { | ||
const { issues, counters } = await main({ | ||
...baseArguments, | ||
cwd, | ||
}); | ||
|
||
assert(issues.unlisted['codegen.ts']['@graphql-codegen/near-operation-file-preset']); | ||
assert(issues.unlisted['codegen.ts']['@graphql-codegen/schema-ast']); | ||
assert(issues.unlisted['codegen.ts']['@graphql-codegen/introspection']); | ||
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript']); | ||
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-operations']); | ||
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-urql']); | ||
assert(issues.unlisted['codegen.ts']['@graphql-codegen/typescript-msw']); | ||
assert(issues.unlisted['package.json']['@graphql-codegen/client-preset']); | ||
|
||
assert.deepEqual(counters, { | ||
...baseCounters, | ||
unlisted: 8, | ||
devDependencies: 1, | ||
processed: 1, | ||
total: 1, | ||
}); | ||
}); |