Skip to content

Commit

Permalink
fixes #2128 (#2129)
Browse files Browse the repository at this point in the history
  • Loading branch information
alx13 authored and dotansimha committed Jul 9, 2019
1 parent 2ee0881 commit e49b75a
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 4 deletions.
18 changes: 18 additions & 0 deletions docs/generated-config/schema-ast.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

### includeDirectives (`boolean`, default value: `false`)

Include directives to Schema output.


#### Usage Example

```yml
schema:
- './src/schema.graphql'
generates:
path/to/file.graphql:
plugins:
- schema-ast
config:
includeDirectives: true
```
4 changes: 4 additions & 0 deletions docs/plugins/schema-ast.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ generates:
plugins:
- schema-ast
```

## Configuration

{@import: ../docs/generated-config/schema-ast.md}
41 changes: 38 additions & 3 deletions packages/plugins/other/schema-ast/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import { GraphQLSchema, printSchema } from 'graphql';
import { GraphQLSchema, printSchema, print } from 'graphql';
import { PluginFunction, PluginValidateFn, Types } from '@graphql-codegen/plugin-helpers';
import { extname } from 'path';

export const plugin: PluginFunction = async (schema: GraphQLSchema): Promise<string> => {
// Actually this should go to ardatan/graphql-toolkit
export function printSchemaWithDirectives(schema: GraphQLSchema): string {
const allTypes = schema.getTypeMap();
const allTypesAst = Object.keys(allTypes).map(key => allTypes[key].astNode);

const allDirectivesAst = schema.getDirectives().map(dir => dir.astNode);

return [...allDirectivesAst, ...allTypesAst].map(ast => print(ast)).join('\n');
}

export interface SchemaASTConfig {
/**
* @name includeDirectives
* @type boolean
* @description Include directives to Schema output.
* @default false
*
* @example
* ```yml
* schema:
* - './src/schema.graphql'
* generates:
* path/to/file.graphql:
* plugins:
* - schema-ast
* config:
* includeDirectives: true
* ```
*/
includeDirectives?: boolean;
}
export const plugin: PluginFunction = async (schema: GraphQLSchema, _documents, { includeDirectives = false }: SchemaASTConfig): Promise<string> => {
if (includeDirectives) {
return printSchemaWithDirectives(schema);
}

return printSchema(schema, { commentDescriptions: false });
};

export const validate: PluginValidateFn<any> = async (schema: GraphQLSchema, documents: Types.DocumentFile[], config: any, outputFile: string, allPlugins: Types.ConfiguredPlugin[]) => {
export const validate: PluginValidateFn<any> = async (_schema: GraphQLSchema, _documents: Types.DocumentFile[], _config: SchemaASTConfig, outputFile: string, allPlugins: Types.ConfiguredPlugin[]) => {
const singlePlugin = allPlugins.length === 1;

if (singlePlugin && extname(outputFile) !== '.graphql') {
Expand Down
40 changes: 39 additions & 1 deletion packages/plugins/other/schema-ast/tests/schema-ast.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { validate } from '../src/index';
import { validate, plugin } from '../src/index';
import { buildSchema } from 'graphql';
import '@graphql-codegen/testing';
import { Types } from '@graphql-codegen/plugin-helpers';

const SHOULD_THROW_ERROR = 'SHOULD_THROW_ERROR';
Expand Down Expand Up @@ -56,4 +58,40 @@ describe('Schema AST', () => {
}
});
});
describe('Output', () => {
const typeDefs = /* GraphQL */ `
directive @modify(limit: Int) on FIELD_DEFINITION
type Query {
fieldTest: String @modify(limit: 1)
}
schema {
query: Query
}
`;
const schema = buildSchema(typeDefs);

it('Should print schema without directives when "includeDirectives" is unset', async () => {
const content = await plugin(schema, [], { includeDirectives: false });

expect(content).toBeSimilarStringTo(`
type Query {
fieldTest: String
}
`);
});
it('Should print schema with directives when "includeDirectives" is set', async () => {
const content = await plugin(schema, [], { includeDirectives: true });

expect(content).toBeSimilarStringTo(`
directive @modify(limit: Int) on FIELD_DEFINITION
`);
expect(content).toBeSimilarStringTo(`
type Query {
fieldTest: String @modify(limit: 1)
}
`);
});
});
});
1 change: 1 addition & 0 deletions website/generate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const mapping = {
// Presets
'../packages/presets/near-operation-file/src/index.ts': BASE_DIR + '/near-operation-file.md',
// Plugins
'../packages/plugins/other/schema-ast/src/index.ts': BASE_DIR + '/schema-ast.md',
'../packages/plugins/other/time/src/index.ts': BASE_DIR + '/time.md',
'../packages/plugins/typescript/typescript/src/index.ts': BASE_DIR + '/typescript.md',
'../packages/plugins/typescript/compatibility/src/index.ts': BASE_DIR + '/typescript-compatibility.md',
Expand Down
3 changes: 3 additions & 0 deletions website/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
"generated-config/near-operation-file": {
"title": "generated-config/near-operation-file"
},
"generated-config/schema-ast": {
"title": "generated-config/schema-ast"
},
"generated-config/time": {
"title": "generated-config/time"
},
Expand Down

0 comments on commit e49b75a

Please sign in to comment.