graphql/yoga-server/docs/features/subscriptions #2020
Replies: 3 comments 4 replies
-
Sample test using export async function testSubscription<TVariables>({
yoga,
document,
variables,
}: {
yoga: Server;
document: string;
variables: TVariables;
}) {
const uri = encodeURI(`http://localhost:3000/graphql?query=${document}${variables ? `&variables=${JSON.stringify(variables)}` : ''}`)
const response = server.fetch(uri, {
method: 'GET',
headers: {
accept: 'text/event-stream',
},
});
return response;
}
const response = await testSubscription({ yoga, document: 'subscription Health { health }' });
if (!response.body) {
return;
}
for await (const chunk of response.body) {
const event = JSON.parse(chunk.toString().slice(6));
expect(event).toHaveProperty("data.health", eventTest());
} |
Beta Was this translation helpful? Give feedback.
-
In the current release (3.7.3) and I suspect earlier releases, the library's import { createPubSub, PubSub } from 'graphql-yoga'
describe('pubsub', () => {
let pubsub: PubSub<{ test: [string] }>;
beforeAll(() => {
pubsub = createPubSub();
});
it('a message published before a subscriber creates its promise is lost', async () => {
expect.assertions(1);
const subscriber = pubsub.subscribe('test');
pubsub.publish('test', 'message');
let messageReceived = false;
subscriber.next().then(() => {
messageReceived = true;
});
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(messageReceived).toBe(false);
});
it('a subscriber obtains a message published after its promise has been created', async () => {
expect.assertions(1);
const subscriber = pubsub.subscribe('test');
pubsub.publish('test', 'message 1');
const messageP = subscriber.next();
pubsub.publish('test', 'message 2');
expect(await messageP).toHaveProperty('value', 'message 2');
});
it('subscribers\' iterates are resolved in order as long as their respective promises have been pending before being published', async () => {
expect.assertions(4);
const subscriber = pubsub.subscribe('test');
pubsub.publish('test', 'message 1');
const message2P = subscriber.next();
const message3P = subscriber.next();
const message4P = subscriber.next();
const message5P = subscriber.next();
pubsub.publish('test', 'message 2');
pubsub.publish('test', 'message 3');
pubsub.publish('test', 'message 4');
pubsub.publish('test', 'message 5');
expect(await message2P).toHaveProperty('value', 'message 2');
expect(await message3P).toHaveProperty('value', 'message 3');
expect(await message4P).toHaveProperty('value', 'message 4');
expect(await message5P).toHaveProperty('value', 'message 5');
});
it('a message published after the first .next() call but before the next .next() call is buffered and not lost', async () => {
expect.assertions(4);
const subscriber = pubsub.subscribe('test');
pubsub.publish('test', 'message 1');
const message2P = subscriber.next();
pubsub.publish('test', 'message 2');
pubsub.publish('test', 'message 3');
pubsub.publish('test', 'message 4');
pubsub.publish('test', 'message 5');
const message3P = subscriber.next();
const message4P = subscriber.next();
const message5P = subscriber.next();
expect(await message2P).toHaveProperty('value', 'message 2');
expect(await message3P).toHaveProperty('value', 'message 3');
expect(await message4P).toHaveProperty('value', 'message 4');
expect(await message5P).toHaveProperty('value', 'message 5');
});
}); |
Beta Was this translation helpful? Give feedback.
-
Sorry posted my comment as reply accidentally. I am trying an example and one part isn't working:
I have tried this example and it does seem to work, I had to modify this to something along those line.
Does that sounds right? |
Beta Was this translation helpful? Give feedback.
-
graphql/yoga-server/docs/features/subscriptions
GraphQL Yoga Documentation
https://www.the-guild.dev/graphql/yoga-server/docs/features/subscriptions
Beta Was this translation helpful? Give feedback.
All reactions