diff --git a/README.md b/README.md index 31991ab3..c04cbc48 100644 --- a/README.md +++ b/README.md @@ -44,10 +44,6 @@ Options: schema definition will be read from STDIN instead of specified file - --comment-descriptions - - use old way of defining descriptions in GraphQL SDL - -c, --config-direction path to begin searching for config files @@ -56,6 +52,14 @@ Options: path to additional custom rules to be loaded. Example: rules/*.js + --comment-descriptions + + use old way of defining descriptions in GraphQL SDL + + --old-implements-syntax + + use old way of defining implemented interfaces in GraphQL SDL + --version output the version number diff --git a/src/configuration.js b/src/configuration.js index bd6d5098..f2f0efc6 100644 --- a/src/configuration.js +++ b/src/configuration.js @@ -17,12 +17,14 @@ export class Configuration { - customRulePaths: [string array] path to additional custom rules to be loaded - stdin: [boolean] pass schema via stdin? - commentDescriptions: [boolean] use old way of defining descriptions in GraphQL SDL + - oldImplementsSyntax: [boolean] use old way of defining implemented interfaces in GraphQL SDL */ constructor(options = {}, stdinFd = null) { const defaultOptions = { format: 'text', customRulePaths: [], commentDescriptions: false, + oldImplementsSyntax: false, }; const configOptions = loadOptionsFromConfig(options.configDirectory); @@ -42,6 +44,10 @@ export class Configuration { return this.options.commentDescriptions; } + getOldImplementsSyntax() { + return this.options.oldImplementsSyntax; + } + getSchema() { if (this.schema) { return this.schema; diff --git a/src/runner.js b/src/runner.js index 84ae01c6..63508990 100644 --- a/src/runner.js +++ b/src/runner.js @@ -32,6 +32,10 @@ export function run(stdout, stdin, stderr, argv) { '--comment-descriptions', 'use old way of defining descriptions in GraphQL SDL' ) + .option( + '--old-implements-syntax', + 'use old way of defining implemented interfaces in GraphQL SDL' + ) // DEPRECATED - This code should be removed in v1.0.0. .option( '-o, --only ', @@ -144,6 +148,10 @@ function getOptionsFromCommander(commander) { options.commentDescriptions = commander.commentDescriptions; } + if (commander.oldImplementsSyntax) { + options.oldImplementsSyntax = commander.oldImplementsSyntax; + } + if (commander.args && commander.args.length) { options.schemaPaths = commander.args; } diff --git a/src/validator.js b/src/validator.js index afcc9ccc..298506b4 100644 --- a/src/validator.js +++ b/src/validator.js @@ -9,8 +9,14 @@ export function validateSchemaDefinition( configuration ) { let ast; + + let parseOptions = {}; + if (configuration.getOldImplementsSyntax()) { + parseOptions.allowLegacySDLImplementsInterfaces = true; + } + try { - ast = parse(schemaDefinition); + ast = parse(schemaDefinition, parseOptions); } catch (e) { if (e instanceof GraphQLError) { return [e]; diff --git a/test/configuration.js b/test/configuration.js index c65407f1..7828e66c 100644 --- a/test/configuration.js +++ b/test/configuration.js @@ -274,4 +274,16 @@ extend type Query { assert.equal(configuration.getCommentDescriptions(), true); }); }); + + describe('getOldImplementsSyntax', () => { + it('defaults to false', () => { + const configuration = new Configuration({}); + assert.equal(configuration.getOldImplementsSyntax(), false); + }); + + it('returns specified value', () => { + const configuration = new Configuration({ oldImplementsSyntax: true }); + assert.equal(configuration.getOldImplementsSyntax(), true); + }); + }); }); diff --git a/test/fixtures/schema.new-implements.graphql b/test/fixtures/schema.new-implements.graphql new file mode 100644 index 00000000..ecf6b349 --- /dev/null +++ b/test/fixtures/schema.new-implements.graphql @@ -0,0 +1,29 @@ +""" +Query +""" +type Query { + node(id: ID!): Node + posts: [Post!]! +} + +""" +Post +""" +type Post implements Node & Commentable { + id: ID! + comments: [String!]! +} + +""" +Node +""" +interface Node { + id: ID! +} + +""" +Commentable +""" +interface Commentable { + comments: [String!]! +} diff --git a/test/fixtures/schema.old-implements.graphql b/test/fixtures/schema.old-implements.graphql new file mode 100644 index 00000000..62ea6859 --- /dev/null +++ b/test/fixtures/schema.old-implements.graphql @@ -0,0 +1,29 @@ +""" +Query +""" +type Query { + node(id: ID!): Node + posts: [Post!]! +} + +""" +Post +""" +type Post implements Node, Commentable { + id: ID! + comments: [String!]! +} + +""" +Node +""" +interface Node { + id: ID! +} + +""" +Commentable +""" +interface Commentable { + comments: [String!]! +} diff --git a/test/runner.js b/test/runner.js index 345e1541..dcc42916 100644 --- a/test/runner.js +++ b/test/runner.js @@ -93,6 +93,39 @@ describe('Runner', () => { assert.equal(expected, stripAnsi(stdout)); }); + it('allows using old `implements` syntax in GraphQL SDL', () => { + const argv = [ + 'node', + 'lib/cli.js', + '--format', + 'json', + '--old-implements-syntax', + '--rules', + 'types-have-descriptions', + `${__dirname}/fixtures/schema.old-implements.graphql`, + ]; + + run(mockStdout, mockStdin, mockStderr, argv); + + assert.deepEqual([], JSON.parse(stdout)['errors']); + }); + + it('validates using new `implements` syntax in GraphQL SDL', () => { + const argv = [ + 'node', + 'lib/cli.js', + '--format', + 'json', + '--rules', + 'types-have-descriptions', + `${__dirname}/fixtures/schema.new-implements.graphql`, + ]; + + run(mockStdout, mockStdin, mockStderr, argv); + + assert.deepEqual([], JSON.parse(stdout)['errors']); + }); + it('validates a single schema file and outputs in text', () => { const argv = [ 'node',