Skip to content

Commit

Permalink
Update original subscription test to use resolver instead of rootValue
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangvvo committed May 23, 2021
1 parent 29b7241 commit 6c8cfc6
Showing 1 changed file with 43 additions and 47 deletions.
90 changes: 43 additions & 47 deletions src/__tests__/subscription.test.ts
@@ -1,5 +1,6 @@
/**
* Based on https://github.com/graphql/graphql-js/blob/main/src/subscription/__tests__/subscribe-test.js
* (commit 39b69da863fca34f0e921bb9ac6a1b99797e17cf)
* This test suite includes certain deviations from the original:
* graphql-jit does not support the root resolver pattern that this test uses
* so the part must be rewritten to include that root resolver in `subscribe` of
Expand All @@ -17,7 +18,7 @@ import {
parse,
SubscriptionArgs
} from "graphql";
import { compileQuery, isCompiledQuery } from "../execution";
import { compileQuery, isAsyncIterable, isCompiledQuery } from "../execution";

type Email = {
from: string,
Expand Down Expand Up @@ -51,13 +52,6 @@ const InboxType = new GraphQLObjectType({
},
});

const QueryType = new GraphQLObjectType({
name: "Query",
fields: {
inbox: { type: InboxType },
},
});

const EmailEventType = new GraphQLObjectType({
name: "EmailEvent",
fields: {
Expand All @@ -66,21 +60,6 @@ const EmailEventType = new GraphQLObjectType({
},
});

const emailSchema = new GraphQLSchema({
query: QueryType,
subscription: new GraphQLObjectType({
name: "Subscription",
fields: {
importantEmail: {
type: EmailEventType,
args: {
priority: { type: GraphQLInt },
},
},
},
}),
});

async function subscribe({
schema,
document,
Expand Down Expand Up @@ -121,26 +100,48 @@ function createSubscription(pubsub: SimplePubSub<Email>) {
},
];

const data = {
inbox: { emails },
// FIXME: we shouldn't use mapAsyncIterator here since it makes tests way more complex
importantEmail: pubsub.getSubscriber((newEmail) => {
emails.push(newEmail);
const inbox = { emails };

const QueryType = new GraphQLObjectType({
name: "Query",
fields: {
inbox: { type: InboxType, resolve: () => emails },
},
});

return {
const emailSchema = new GraphQLSchema({
query: QueryType,
subscription: new GraphQLObjectType({
name: "Subscription",
fields: {
importantEmail: {
email: newEmail,
inbox: data.inbox,
type: EmailEventType,
args: {
priority: { type: GraphQLInt },
},
// FIXME: we shouldn't use mapAsyncIterator here since it makes tests way more complex
subscribe() {
return pubsub.getSubscriber((newEmail) => {
emails.push(newEmail);

return {
importantEmail: {
email: newEmail,
inbox,
},
};
})
}
},
};
},
}),
};
});

return subscribe({ schema: emailSchema, document, rootValue: data });
return subscribe({ schema: emailSchema, document });
}

async function expectPromise(promise: Promise<any>) {
let caughtError;
let caughtError: any;

try {
await promise;
Expand All @@ -153,7 +154,7 @@ async function expectPromise(promise: Promise<any>) {
toReject() {
expect(caughtError).toBeInstanceOf(Error);
},
toRejectWith(message) {
toRejectWith(message: string) {
expect(caughtError).toBeInstanceOf(Error);
expect(caughtError).toHaveProperty("message", message);
},
Expand All @@ -170,25 +171,24 @@ const DummyQueryType = new GraphQLObjectType({
// Check all error cases when initializing the subscription.
describe("Subscription Initialization Phase", () => {
it("accepts multiple subscription fields defined in schema", async () => {
async function* fooGenerator() {
yield { foo: "FooValue" };
}

const schema = new GraphQLSchema({
query: DummyQueryType,
subscription: new GraphQLObjectType({
name: "Subscription",
fields: {
foo: { type: GraphQLString },
foo: { type: GraphQLString, subscribe: fooGenerator },
bar: { type: GraphQLString },
},
}),
});

async function* fooGenerator() {
yield { foo: "FooValue" };
}

const subscription = await subscribe({
schema,
document: parse("subscription { foo }"),
rootValue: { foo: fooGenerator },
}) as AsyncIterableIterator<ExecutionResult>;
expect(isAsyncIterable(subscription)).toBeTruthy();

Expand Down Expand Up @@ -486,7 +486,7 @@ describe("Subscription Initialization Phase", () => {
errors: [
{
message:
'Variable "$arg" got invalid value "meow"; Int cannot represent non-integer value: "meow"',
'Variable "$arg" got invalid value "meow"; Expected type Int; Int cannot represent non-integer value: "meow"',
locations: [{ line: 2, column: 21 }],
},
],
Expand Down Expand Up @@ -1034,10 +1034,6 @@ class SimplePubSub<T = any> {
}
}

function isAsyncIterable(maybeAsyncIterable) {
return typeof maybeAsyncIterable?.[Symbol.asyncIterator] === 'function';
}

function resolveOnNextTick(): Promise<void> {
return Promise.resolve(undefined);
}

0 comments on commit 6c8cfc6

Please sign in to comment.