Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: display correct name for operations with fragments #411

Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export function extractDefinition(
// We know we always have a single definition, because Apollo validates this before we get here.
// With more then one query defined, an error like this is thrown and the query is never sent:
// "react-apollo only supports a query, subscription, or a mutation per HOC. [object Object] had 2 queries, 0 subscriptions and 0 mutations. You can use 'compose' to join multiple operation types to a component"
return operation.query.definitions[0] as OperationDefinitionNode;
return operation.query.definitions.find(
(q) => q.kind === 'OperationDefinition',
) as OperationDefinitionNode;
}
33 changes: 33 additions & 0 deletions tests/operation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,37 @@ describe('extractDefinition', () => {

expect(definition.kind).toBe('OperationDefinition');
});

it('should get the single operation definition if it includes a fragment', () => {
const definition = extractDefinition(
makeOperation({
query: parse(`
fragment CoreCommentFields on Comment {
id
}
query Comments {
...CoreCommentFields
}
`),
}),
);

expect(definition.name?.value).toBe('Comments');
expect(definition.kind).toBe('OperationDefinition');
});

it('should get a mutation operation', () => {
const definition = extractDefinition(
makeOperation({
query: parse(`
mutation Test {
test
}
`),
}),
);

expect(definition.name?.value).toBe('Test');
expect(definition.kind).toBe('OperationDefinition');
});
});