diff --git a/src/index.js b/src/index.js index 0d148a5..2ef1067 100644 --- a/src/index.js +++ b/src/index.js @@ -31,9 +31,13 @@ export default () => { const queryDocument = gql(source); - for (const definition of queryDocument.definitions) { - if (!definition.name) { - throw new Error('GraphQL query must have name.'); + // If a document contains only one operation, that operation may be unnamed: + // https://facebook.github.io/graphql/#sec-Language.Query-Document + if (queryDocument.definitions.length > 1) { + for (const definition of queryDocument.definitions) { + if (!definition.name) { + throw new Error('GraphQL query must have name.'); + } } } diff --git a/test/fixtures/graphql-tag/transpiles a single unnamed query/actual.js b/test/fixtures/graphql-tag/transpiles a single unnamed query/actual.js new file mode 100644 index 0000000..c798142 --- /dev/null +++ b/test/fixtures/graphql-tag/transpiles a single unnamed query/actual.js @@ -0,0 +1,3 @@ +import gql from 'graphql-tag'; + +const foo = gql`{foo}`; diff --git a/test/fixtures/graphql-tag/transpiles a single unnamed query/expected.js b/test/fixtures/graphql-tag/transpiles a single unnamed query/expected.js new file mode 100644 index 0000000..f7dd375 --- /dev/null +++ b/test/fixtures/graphql-tag/transpiles a single unnamed query/expected.js @@ -0,0 +1,36 @@ +const foo = { + 'kind': 'Document', + 'definitions': [{ + 'kind': 'OperationDefinition', + 'operation': 'query', + 'name': null, + 'variableDefinitions': null, + 'directives': [], + 'selectionSet': { + 'kind': 'SelectionSet', + 'selections': [{ + 'kind': 'Field', + 'alias': null, + 'name': { + 'kind': 'Name', + 'value': 'foo' + }, + 'arguments': [], + 'directives': [], + 'selectionSet': null + }] + } + }], + 'loc': { + 'start': 0, + 'end': 5, + 'source': { + 'body': '{foo}', + 'name': 'GraphQL request', + 'locationOffset': { + 'line': 1, + 'column': 1 + } + } + } +}; diff --git a/test/fixtures/graphql-tag/transpiles a single unnamed query/options.json b/test/fixtures/graphql-tag/transpiles a single unnamed query/options.json new file mode 100644 index 0000000..5acbd80 --- /dev/null +++ b/test/fixtures/graphql-tag/transpiles a single unnamed query/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + [ + "../../../../src" + ] + ] +} diff --git a/test/unnamed-query-test.js b/test/unnamed-query-test.js new file mode 100644 index 0000000..742ddb5 --- /dev/null +++ b/test/unnamed-query-test.js @@ -0,0 +1,29 @@ +import { transform } from 'babel-core'; +import assert from 'assert'; + +const fixture = "gql`type Widget { name: String } query {widget}`"; + +describe("When given an unnamed query", () => { + let originalError; + + before(function() { + originalError = console.error; + }); + + after(function() { + console.error = originalError; + }); + + it('fails when there are other definitions', () => { + const calls = []; + + console.error = (...args) => calls.push(args.join(' ')); + + transform(fixture, { + plugins: [['./src']] + }); + + assert.equal(calls.length, 1); + assert.equal(calls[0], 'error Error: GraphQL query must have name.'); + }); +});