Skip to content

Commit

Permalink
doc: migrate config examples to TS (#8481)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlypoly committed Oct 13, 2022
1 parent c525a4c commit 8473682
Show file tree
Hide file tree
Showing 62 changed files with 3,982 additions and 1,761 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-pants-remember.md
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': patch
---

ensure to generate `codegen.ts` in a typescript setup
6 changes: 5 additions & 1 deletion packages/graphql-codegen-cli/src/init/questions.ts
Expand Up @@ -71,7 +71,11 @@ export function getQuestions(possibleTargets: Record<Tags, boolean>): inquirer.Q
name: 'config',
message: 'How to name the config file?',
default: answers =>
answers.targets.includes(Tags.client) || answers.targets.includes(Tags.angular) ? 'codegen.ts' : 'codegen.yml',
answers.targets.includes(Tags.client) ||
answers.targets.includes(Tags.typescript) ||
answers.targets.includes(Tags.angular)
? 'codegen.ts'
: 'codegen.yml',
validate: (str: string) => {
const isNotEmpty = str.length > 0;
const hasCorrectExtension = ['json', 'yml', 'yaml', 'js', 'ts'].some(ext =>
Expand Down
37 changes: 37 additions & 0 deletions packages/graphql-codegen-cli/tests/__snapshots__/init.spec.ts.snap
Expand Up @@ -62,6 +62,43 @@ export default config;
"
`;

exports[`init plugins suggestions for client-side setup should use stencil related plugins when @stencil/core is found 1`] = `
"
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
overwrite: true,
schema: "http://localhost:4000",
documents: "src/**/*.graphql",
generates: {
"src/generated/graphql.tsx": {
plugins: ["typescript", "typescript-operations", "typescript-stencil-apollo"]
}
}
};
export default config;
"
`;

exports[`init plugins suggestions non client-side setup should use typescript related plugins when typescript is found (node) 1`] = `
"
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
overwrite: true,
schema: "http://localhost:4000",
generates: {
"src/generated/graphql.ts": {
plugins: ["typescript", "typescript-resolvers"]
}
}
};
export default config;
"
`;

exports[`init should have few default values 1`] = `
"
import type { CodegenConfig } from '@graphql-codegen/cli';
Expand Down
22 changes: 4 additions & 18 deletions packages/graphql-codegen-cli/tests/init.spec.ts
Expand Up @@ -10,7 +10,6 @@ import { guessTargets } from '../src/init/targets.js';
import { plugins } from '../src/init/plugins.js';
import { bold } from '../src/init/helpers.js';
import { getApplicationTypeChoices, getPluginChoices } from '../src/init/questions.js';
import { load } from 'js-yaml';

jest.mock('fs');
const { version } = require('../package.json');
Expand Down Expand Up @@ -225,16 +224,9 @@ describe('init', () => {
expect(writeFileSpy).toHaveBeenCalledTimes(2);

const pkg = JSON.parse(writeFileSpy.mock.calls[1][1] as string);
const config = load(writeFileSpy.mock.calls[0][1] as string) as Record<string, any>;

// should use default output path
expect(config.generates['src/generated/graphql.tsx']).toBeDefined();
const config = writeFileSpy.mock.calls[0][1] as string;

const output: any = config.generates['src/generated/graphql.tsx'];
expect(output.plugins).toContainEqual('typescript');
expect(output.plugins).toContainEqual('typescript-operations');
expect(output.plugins).toContainEqual('typescript-stencil-apollo');
expect(output.plugins).toHaveLength(3);
expect(config).toMatchSnapshot();

// expected plugins
expect(pkg.devDependencies).toHaveProperty('@graphql-codegen/typescript');
Expand Down Expand Up @@ -269,15 +261,9 @@ describe('init', () => {
expect(writeFileSpy).toHaveBeenCalledTimes(2);

const pkg = JSON.parse(writeFileSpy.mock.calls[1][1] as string);
const config = load(writeFileSpy.mock.calls[0][1] as string) as Record<string, any>;

// should use default output path
expect(config.generates['src/generated/graphql.ts']).toBeDefined();
const config = writeFileSpy.mock.calls[0][1] as string;

const output: any = config.generates['src/generated/graphql.ts'];
expect(output.plugins).toContainEqual('typescript');
expect(output.plugins).toContainEqual('typescript-resolvers');
expect(output.plugins).toHaveLength(2);
expect(config).toMatchSnapshot();

// expected plugins
expect(pkg.devDependencies).toHaveProperty('@graphql-codegen/typescript');
Expand Down
69 changes: 48 additions & 21 deletions packages/plugins/other/fragment-matcher/src/index.ts
Expand Up @@ -42,13 +42,22 @@ export interface FragmentMatcherConfig {
* @default es2015
*
* @exampleMarkdown
* ```yaml {6}
* generates:
* path/to/file.json:
* plugins:
* - fragment-matcher
* config:
* module: commonjs
* ```tsx {10} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: 'https://localhost:4000/graphql',
* documents: ['src/**\/*.tsx'],
* generates: {
* 'path/to/file.json': {
* plugins: ['fragment-matcher'],
* config: {
* module: 'commonjs',
* },
* },
* },
* };
* export default config;
* ```
*/
module?: 'commonjs' | 'es2015';
Expand All @@ -57,13 +66,22 @@ export interface FragmentMatcherConfig {
* @default 3
*
* @exampleMarkdown
* ```yaml {6}
* generates:
* path/to/file.ts:
* plugins:
* - fragment-matcher
* config:
* apolloClientVersion: 3
* ```tsx {10} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: 'https://localhost:4000/graphql',
* documents: ['src/**\/*.tsx'],
* generates: {
* 'path/to/file.json': {
* plugins: ['fragment-matcher'],
* config: {
* apolloClientVersion: 3,
* },
* },
* },
* };
* export default config;
* ```
*/
apolloClientVersion?: 2 | 3;
Expand All @@ -72,13 +90,22 @@ export interface FragmentMatcherConfig {
* @default false
*
* @exampleMarkdown
* ```yaml {6}
* generates:
* path/to/file.ts:
* plugins:
* - fragment-matcher
* config:
* useExplicitTyping: true
* ```tsx {10} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: 'https://localhost:4000/graphql',
* documents: ['src/**\/*.tsx'],
* generates: {
* 'path/to/file.json': {
* plugins: ['fragment-matcher'],
* config: {
* useExplicitTyping: true
* },
* },
* },
* };
* export default config;
* ```
*/
useExplicitTyping?: boolean;
Expand Down
23 changes: 16 additions & 7 deletions packages/plugins/other/introspection/src/index.ts
Expand Up @@ -12,13 +12,22 @@ export interface IntrospectionPluginConfig {
* @default false
*
* @exampleMarkdown
* ```yaml {6}
* generates:
* introspection.json:
* plugins:
* - introspection
* config:
* minify: true
* ```tsx {10} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: 'https://localhost:4000/graphql',
* documents: ['src/**\/*.tsx'],
* generates: {
* 'introspection.json': {
* plugins: ['introspection'],
* config: {
* minify: true
* },
* },
* },
* };
* export default config;
* ```
*/
minify?: boolean;
Expand Down
71 changes: 45 additions & 26 deletions packages/plugins/other/schema-ast/src/index.ts
Expand Up @@ -26,15 +26,21 @@ export interface SchemaASTConfig {
* @default false
*
* @exampleMarkdown
* ```yaml {8}
* schema:
* - './src/schema.graphql'
* generates:
* path/to/file.graphql:
* plugins:
* - schema-ast
* config:
* includeDirectives: true
* ```tsx {9} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: './src/schema.graphql',
* generates: {
* 'path/to/file.graphql': {
* plugins: ['schema-ast'],
* config: {
* includeDirectives: true
* },
* },
* },
* };
* export default config;
* ```
*/
includeDirectives?: boolean;
Expand All @@ -43,15 +49,21 @@ export interface SchemaASTConfig {
* @default false
*
* @exampleMarkdown
* ```yaml {8}
* schema:
* - './src/schema.graphql'
* generates:
* path/to/file.graphql:
* plugins:
* - schema-ast
* config:
* includeIntrospectionTypes: true
* ```tsx {9} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: './src/schema.graphql',
* generates: {
* 'path/to/file.graphql': {
* plugins: ['schema-ast'],
* config: {
* includeIntrospectionTypes: true
* },
* },
* },
* };
* export default config;
* ```
*/
includeIntrospectionTypes?: boolean;
Expand All @@ -60,14 +72,21 @@ export interface SchemaASTConfig {
* @default false
*
* @exampleMarkdown
* ```yaml {7}
* schema: http://localhost:3000/graphql
* generates:
* schema.graphql:
* plugins:
* - schema-ast
* config:
* commentDescriptions: true
* ```tsx {9} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: './src/schema.graphql',
* generates: {
* 'path/to/file.graphql': {
* plugins: ['schema-ast'],
* config: {
* commentDescriptions: true
* },
* },
* },
* };
* export default config;
* ```
*/
commentDescriptions?: boolean;
Expand Down
44 changes: 32 additions & 12 deletions packages/plugins/other/time/src/config.ts
Expand Up @@ -4,12 +4,22 @@ export interface TimePluginConfig {
* @default YYYY-MM-DDTHH:mm:ssZ
*
* @exampleMarkdown
* ```yaml {4-5}
* generates:
* path/to/file.ts:
* plugins:
* - time:
* format: DD.MM.YY
* ```tsx {10} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: 'https://localhost:4000/graphql',
* documents: ['src/**\/*.tsx'],
* generates: {
* 'path/to/file.ts': {
* plugins: ['time'],
* config: {
* format: 'DD.MM.YY'
* },
* },
* },
* };
* export default config;
* ```
*/
format?: string;
Expand All @@ -18,12 +28,22 @@ export interface TimePluginConfig {
* @default 'Generated on'
*
* @exampleMarkdown
* ```yaml {4-5}
* generates:
* path/to/file.ts:
* plugins:
* - time:
* message: 'The file generated on: '
* ```tsx {10} filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* schema: 'https://localhost:4000/graphql',
* documents: ['src/**\/*.tsx'],
* generates: {
* 'path/to/file.ts': {
* plugins: ['time'],
* config: {
* message: 'The file generated on: '
* },
* },
* },
* };
* export default config;
* ```
*/
message?: string;
Expand Down

0 comments on commit 8473682

Please sign in to comment.