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

(core) - error for invalid operation passed to query/mutation/subscription #1829

Merged
merged 6 commits into from
Jul 30, 2021
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
5 changes: 5 additions & 0 deletions .changeset/weak-doors-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': minor
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
---

Warn for invalid operation passed to query/subscription/mutation
77 changes: 54 additions & 23 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ const query = {
variables: { example: 1234 },
};

const mutation = {
key: 1,
query: gql`
mutation {
todos {
id
}
}
`,
variables: { example: 1234 },
};

const subscription = {
key: 1,
query: gql`
subscription {
todos {
id
}
}
`,
variables: { example: 1234 },
};

let receivedOps: Operation[] = [];
let client = createClient({ url: '1234' });
const receiveMock = jest.fn((s: Source<Operation>) =>
Expand Down Expand Up @@ -119,21 +143,17 @@ describe('promisified methods', () => {
});

it('mutation', () => {
const mutationResult = client
.mutation(
gql`
{
todos {
id
}
}
`,
{ example: 1234 }
)
.toPromise();
const mut = gql`
mutation {
todos {
id
}
}
`;
const mutationResult = client.mutation(mut, { example: 1234 }).toPromise();

const received = receivedOps[0];
expect(print(received.query)).toEqual(print(query.query));
expect(print(received.query)).toEqual(print(mut));
expect(received.key).toBeDefined();
expect(received.variables).toEqual({ example: 1234 });
expect(received.kind).toEqual('mutation');
Expand Down Expand Up @@ -187,6 +207,17 @@ describe('executeQuery', () => {
expect(print(receivedQuery)).toBe(print(query.query));
});

it('should throw when passing in a mutation', () => {
try {
client.executeQuery(mutation);
expect(true).toBeFalsy();
} catch (e) {
expect(e.message).toMatchInlineSnapshot(
`"Expected operation of type query but found mutation"`
);
}
});

it('passes variables type to exchange', () => {
pipe(
client.executeQuery(query),
Expand Down Expand Up @@ -242,17 +273,17 @@ describe('executeQuery', () => {
describe('executeMutation', () => {
it('passes query string exchange', async () => {
pipe(
client.executeMutation(query),
client.executeMutation(mutation),
subscribe(x => x)
);

const receivedQuery = receivedOps[0].query;
expect(print(receivedQuery)).toBe(print(query.query));
expect(print(receivedQuery)).toBe(print(mutation.query));
});

it('passes variables type to exchange', () => {
pipe(
client.executeMutation(query),
client.executeMutation(mutation),
subscribe(x => x)
);

Expand All @@ -261,7 +292,7 @@ describe('executeMutation', () => {

it('passes kind type to exchange', () => {
pipe(
client.executeMutation(query),
client.executeMutation(mutation),
subscribe(x => x)
);

Expand All @@ -270,7 +301,7 @@ describe('executeMutation', () => {

it('passes url (from context) to exchange', () => {
pipe(
client.executeMutation(query),
client.executeMutation(mutation),
subscribe(x => x)
);

Expand All @@ -281,26 +312,26 @@ describe('executeMutation', () => {
describe('executeSubscription', () => {
it('passes query string exchange', async () => {
pipe(
client.executeSubscription(query),
client.executeSubscription(subscription),
subscribe(x => x)
);

const receivedQuery = receivedOps[0].query;
expect(print(receivedQuery)).toBe(print(query.query));
expect(print(receivedQuery)).toBe(print(subscription.query));
});

it('passes variables type to exchange', () => {
pipe(
client.executeSubscription(query),
client.executeSubscription(subscription),
subscribe(x => x)
);

expect(receivedOps[0]).toHaveProperty('variables', query.variables);
expect(receivedOps[0]).toHaveProperty('variables', subscription.variables);
});

it('passes kind type to exchange', () => {
pipe(
client.executeSubscription(query),
client.executeSubscription(subscription),
subscribe(x => x)
);

Expand Down
21 changes: 20 additions & 1 deletion packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from 'wonka';

import { TypedDocumentNode } from '@graphql-typed-document-node/core';
import { DocumentNode } from 'graphql';
import { DocumentNode, Kind } from 'graphql';

import { composeExchanges, defaultExchanges } from './exchanges';
import { fallbackExchange } from './exchanges/fallback';
Expand Down Expand Up @@ -289,6 +289,25 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
},

createRequestOperation(kind, request, opts) {
if (
kind !== 'teardown' &&
!request.query.definitions.some(
x => x.kind === Kind.OPERATION_DEFINITION && x.operation === kind
) &&
process.env.NODE_ENV !== 'production'
) {
for (let i = 0; i < request.query.definitions.length; i++) {
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
const definition = request.query.definitions[i];
if (
definition.kind === Kind.OPERATION_DEFINITION &&
definition.operation !== kind
) {
throw new Error(
`Expected operation of type ${kind} but found ${definition.operation}`
);
}
}
}
return makeOperation(kind, request, client.createOperationContext(opts));
},

Expand Down