-
-
Save boopathi/885f41720f258b50a2ba67bed76581af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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