From e8d6800fd9369208c7b188c39aaf278e6bf5903b Mon Sep 17 00:00:00 2001 From: Jimmy Miller Date: Thu, 9 Feb 2017 14:24:26 -0500 Subject: [PATCH] Fixed bugs around invariants --- src/parser.ts | 32 ++++++++----------- test/parser.test.ts | 16 ++++++---- .../client/graphql/fragments.test.tsx | 2 +- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 716932652f..9510c7020f 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -29,7 +29,7 @@ export function parser(document: DocumentNode): IDocumentDefinition { Saftey checks for proper usage of react-apollo */ - invariant((!!document || !!document.kind), + invariant((!!document && !!document.kind), // tslint:disable-line `Argument of ${document} passed to parser was not a valid GraphQL DocumentNode. You may need to use 'graphql-tag' or another method to convert your operation into a document` ); @@ -50,18 +50,16 @@ export function parser(document: DocumentNode): IDocumentDefinition { (x: DefinitionNode) => x.kind === 'OperationDefinition' && x.operation === 'subscription' ); - if (fragments.length && (!queries.length || !mutations.length || !subscriptions.length)) { - invariant(true, - `Passing only a fragment to 'graphql' is not yet supported. You must include a query, subscription or mutation as well` - ); - } + invariant(!fragments.length || (queries.length || mutations.length || subscriptions.length), + `Passing only a fragment to 'graphql' is not yet supported. You must include a query, subscription or mutation as well` + ); + + + invariant(((queries.length + mutations.length + subscriptions.length) <= 1), + // tslint:disable-line + `react-apollo only supports a query, subscription, or a mutation per HOC. ${document} had ${queries.length} queries, ${subscriptions.length} subscriptions and ${mutations.length} muations. You can use 'compose' to join multiple operation types to a component` + ); - if (queries.length && mutations.length && mutations.length) { - invariant(((queries.length + mutations.length + mutations.length) > 1), - // tslint:disable-line - `react-apollo only supports a query, subscription, or a mutation per HOC. ${document} had ${queries.length} queries, ${subscriptions.length} subscriptions and ${mutations.length} muations. You can use 'compose' to join multiple operation types to a component` - ); - } type = queries.length ? DocumentType.Query : DocumentType.Mutation; if (!queries.length && !mutations.length) type = DocumentType.Subscription; @@ -69,12 +67,10 @@ export function parser(document: DocumentNode): IDocumentDefinition { const definitions = queries.length ? queries : (mutations.length ? mutations : subscriptions); - if (definitions.length !== 1) { - invariant((definitions.length !== 1), - // tslint:disable-line - `react-apollo only supports one defintion per HOC. ${document} had ${definitions.length} definitions. You can use 'compose' to join multiple operation types to a component` - ); - } + invariant(definitions.length === 1, + // tslint:disable-line + `react-apollo only supports one defintion per HOC. ${document} had ${definitions.length} definitions. You can use 'compose' to join multiple operation types to a component` + ); const definition = definitions[0] as OperationDefinitionNode; variables = definition.variableDefinitions || []; diff --git a/test/parser.test.ts b/test/parser.test.ts index d75d5457ec..3373f83a27 100644 --- a/test/parser.test.ts +++ b/test/parser.test.ts @@ -18,9 +18,7 @@ describe('parser', () => { mutation ($t: String) { addT(t: $t) { user { name } } } `; - try { parser(query); } catch (e) { - expect(e).toMatch(/Invariant Violation/); - } + expect(parser.bind(null, query)).toThrowError(/react-apollo only supports/); }); it('should error if multiple operations are present', () => { @@ -29,9 +27,15 @@ describe('parser', () => { query Two { user { name } } `; - try { parser(query); } catch (e) { - expect(e).toMatch(/Invariant Violation/); - } + expect(parser.bind(null, query)).toThrowError(/react-apollo only supports/); + }); + + it('should error if not a DocumentNode', () => { + const query = ` + query One { user { name } } + `; + + expect(parser.bind(null, query)).toThrowError(/not a valid GraphQL DocumentNode/); }); it('should return the name of the operation', () => { diff --git a/test/react-web/client/graphql/fragments.test.tsx b/test/react-web/client/graphql/fragments.test.tsx index cf0d36db99..b01621ae06 100644 --- a/test/react-web/client/graphql/fragments.test.tsx +++ b/test/react-web/client/graphql/fragments.test.tsx @@ -38,7 +38,7 @@ describe('fragments', () => { renderer.create(); throw new Error(); } catch (e) { - expect(e.name).toMatch(/TypeError/); + expect(e.name).toMatch(/Invariant Violation/); } });