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

feat(server): More callbacks, clearer differences and higher extensibility #40

Merged
merged 29 commits into from
Oct 23, 2020
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1aff2a6
fix: message type has to be a string always
enisdenjo Oct 23, 2020
10bf3ef
feat: add areGraphQLErrors and drop unused
enisdenjo Oct 23, 2020
3a9c54a
docs: why ignore
enisdenjo Oct 23, 2020
bad4172
feat: cleanup and begin with more callbacks and extensibility
enisdenjo Oct 23, 2020
6019b00
docs(protocol): refine and more explanations
enisdenjo Oct 23, 2020
7bcd100
feat: add `onOperation` and introduce operation result type
enisdenjo Oct 23, 2020
f721238
feat: all callbacks can return promises
enisdenjo Oct 23, 2020
9d1c3c0
feat: why not pass args along too
enisdenjo Oct 23, 2020
69b2aca
docs: generate
enisdenjo Oct 23, 2020
61c9d24
fix: onSubscribe test
enisdenjo Oct 23, 2020
eccc991
style: drop unused import
enisdenjo Oct 23, 2020
73c10c7
refactor: unnecessary export
enisdenjo Oct 23, 2020
75b9bf3
test: onNext behaviour
enisdenjo Oct 23, 2020
4e81b7c
test: onError behaviour
enisdenjo Oct 23, 2020
a54c11f
test: onComplete behaviour
enisdenjo Oct 23, 2020
f40eb85
test: onSubscribe returned error behaviour
enisdenjo Oct 23, 2020
143912a
test: throwing errors from callbacks
enisdenjo Oct 23, 2020
21a72b9
fix: dont forget to await
enisdenjo Oct 23, 2020
862a6dc
style: drop unused import
enisdenjo Oct 23, 2020
4fc183d
docs: custom graphql arguments recipe
enisdenjo Oct 23, 2020
62a4594
docs: persisted queries recipe (and test to make sure)
enisdenjo Oct 23, 2020
7b9199e
fix: correct tag close
enisdenjo Oct 23, 2020
385caf8
docs: remind to validate with onSubscribe
enisdenjo Oct 23, 2020
8a3c7d4
docs: static and dynamic graphql arguments
enisdenjo Oct 23, 2020
732be2b
fix: from msg payload
enisdenjo Oct 23, 2020
eef812f
style: simplifty
enisdenjo Oct 23, 2020
f683819
feat: onConnect can return nothing
enisdenjo Oct 23, 2020
2cc2032
docs: refine logging recipe
enisdenjo Oct 23, 2020
7a0414f
docs: generate [skip ci]
enisdenjo Oct 23, 2020
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
Prev Previous commit
Next Next commit
test: throwing errors from callbacks
  • Loading branch information
enisdenjo committed Oct 23, 2020
commit 143912aa05c59176193049403611093fc223f59c
123 changes: 122 additions & 1 deletion src/tests/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import WebSocket from 'ws';
import { parse, buildSchema, execute, subscribe, GraphQLError } from 'graphql';
import { GRAPHQL_TRANSPORT_WS_PROTOCOL } from '../protocol';
import { MessageType, parseMessage, stringifyMessage } from '../message';
import {
MessageType,
parseMessage,
stringifyMessage,
SubscribeMessage,
SubscribePayload,
} from '../message';
import { startServer, url, schema, pong } from './fixtures/simple';

let forgottenDispose: (() => Promise<void>) | undefined;
Expand Down Expand Up @@ -575,6 +581,121 @@ describe('Subscribe', () => {
});
});

it('should close the socket with errors thrown from any callback', async () => {
const error = new Error('Stop');

// onConnect
let [, dispose] = await makeServer({
onConnect: () => {
throw error;
},
});
const client = await createTClient();
client.ws.send(
stringifyMessage<MessageType.ConnectionInit>({
type: MessageType.ConnectionInit,
}),
);
await client.waitForClose((event) => {
expect(event.code).toBe(4400);
expect(event.reason).toBe(error.message);
expect(event.wasClean).toBeTruthy();
});
await dispose();

async function test(
payload: SubscribePayload = {
query: `query { getValue }`,
},
) {
const client = await createTClient();
client.ws.send(
stringifyMessage<MessageType.ConnectionInit>({
type: MessageType.ConnectionInit,
}),
);

await client.waitForMessage(({ data }) => {
expect(parseMessage(data).type).toBe(MessageType.ConnectionAck);
client.ws.send(
stringifyMessage<MessageType.Subscribe>({
id: '1',
type: MessageType.Subscribe,
payload,
}),
);
});

await client.waitForClose((event) => {
expect(event.code).toBe(4400);
expect(event.reason).toBe(error.message);
expect(event.wasClean).toBeTruthy();
});
}

// onSubscribe
[, dispose] = await makeServer({
onSubscribe: () => {
throw error;
},
});
await test();
await dispose();

[, dispose] = await makeServer({
onOperation: () => {
throw error;
},
});
await test();
await dispose();

// execute
[, dispose] = await makeServer({
execute: () => {
throw error;
},
});
await test();
await dispose();

// subscribe
[, dispose] = await makeServer({
subscribe: () => {
throw error;
},
});
await test({ query: 'subscription { greetings }' });
await dispose();

// onNext
[, dispose] = await makeServer({
onNext: () => {
throw error;
},
});
await test();
await dispose();

// onError
[, dispose] = await makeServer({
onError: () => {
throw error;
},
});
await test({ query: 'query { noExisto }' });
await dispose();

// onComplete
[, dispose] = await makeServer({
onComplete: () => {
throw error;
},
});
await test();
await dispose();
});

it('should directly use the execution arguments returned from `onSubscribe`', async () => {
const nopeArgs = {
schema,
Expand Down