Skip to content

Instantly share code, notes, and snippets.

@boopathi
Created March 9, 2021 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boopathi/885f41720f258b50a2ba67bed76581af to your computer and use it in GitHub Desktop.
Save boopathi/885f41720f258b50a2ba67bed76581af to your computer and use it in GitHub Desktop.
import { makeExecutableSchema } from "@graphql-tools/schema";
import { parse } from "graphql";
import { compileQuery, isCompiledQuery } from "../execution";
function isAsyncIterable<T>(val: any): val is AsyncIterableIterator<T> {
return val != null && typeof val[Symbol.asyncIterator] === "function";
}
const schema = makeExecutableSchema({
typeDefs: `
type Subscription {
nodes: Node
}
type Node {
id: ID
}
`,
resolvers: {
Subscription: {
nodes: {
async *subscribe() {
for (let i = 0; i < 3; i++) {
yield {
id: i.toString()
};
}
},
/**
* Without this resolver, the tests below would fail
*/
resolve(source) {
return source;
}
}
}
}
});
async function subscribe(query: string) {
const document = parse(query);
const prepared = compileQuery(schema, document);
if (!isCompiledQuery(prepared)) return prepared;
return prepared.subscribe!({}, {}, {});
}
describe("subscription happy case", () => {
test("nodes and ids", async () => {
const sub = await subscribe(`
subscription {
nodes {
id
}
}
`);
if (isAsyncIterable(sub)) {
let i = 0;
for await (const val of sub) {
expect(val.data?.nodes?.id).toBe((i++).toString());
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment