Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Fixed bugs around invariants #457

Merged
merged 1 commit into from
Feb 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
Expand All @@ -50,31 +50,27 @@ 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;

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 || [];
Expand Down
16 changes: 10 additions & 6 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/react-web/client/graphql/fragments.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('fragments', () => {
renderer.create(<ApolloProvider client={client}><Container /></ApolloProvider>);
throw new Error();
} catch (e) {
expect(e.name).toMatch(/TypeError/);
expect(e.name).toMatch(/Invariant Violation/);
}
});

Expand Down