Skip to content

Commit

Permalink
Implement GraphQL subscription (#115)
Browse files Browse the repository at this point in the history
* Implement subscription

* Add original subscription test

* Refactor implementation

* Update original subscription test to use resolver instead of rootValue

* Fix TS errors and format

* Update README
  • Loading branch information
hoangvvo committed Sep 29, 2021
1 parent dfdde4a commit f522b86
Show file tree
Hide file tree
Showing 4 changed files with 1,329 additions and 18 deletions.
22 changes: 17 additions & 5 deletions README.md
Expand Up @@ -29,8 +29,7 @@ Done in 141.94s.

### Support for GraphQL spec

The goal is to support the [June 2018 version of the GraphQL spec](https://facebook.github.io/graphql/June2018/). At this moment,
the only missing feature is support for Subscriptions.
The goal is to support the [June 2018 version of the GraphQL spec](https://facebook.github.io/graphql/June2018/).

#### Differences to `graphql-js`

Expand Down Expand Up @@ -91,10 +90,19 @@ if (!isCompiledQuery(compiledQuery)) {
#### Execute the Query

```js
const executionResult = await compiledQuery.query();
const executionResult = await compiledQuery.query(root, context, variables);
console.log(executionResult);
```

#### Subscribe to the Query

```js
const result = await compiledQuery.subscribe(root, context, variables);
for await (const value of result) {
console.log(value);
}
```

## API

### compiledQuery = compileQuery(schema, document, operationName, compilerOptions)
Expand All @@ -112,14 +120,18 @@ Compiles the `document` AST, using an optional operationName and compiler option
for overly expensive serializers
- `customJSONSerializer` {boolean, default: false} - Whether to produce also a JSON serializer function using `fast-json-stringify`. The default stringifier function is `JSON.stringify`

#### compiledQuery.compiled(root: any, context: any, variables: Maybe<{ [key: string]: any }>)
#### compiledQuery.query(root: any, context: any, variables: Maybe<{ [key: string]: any }>)

the compiled function that can be called with a root value, a context and the required variables.

#### compiledQuery.subscribe(root: any, context: any, variables: Maybe<{ [key: string]: any }>)

(available for GraphQL Subscription only) the compiled function that can be called with a root value, a context and the required variables to produce either an AsyncIterator (if successful) or an ExecutionResult (error).

#### compiledQuery.stringify(value: any)

the compiled function for producing a JSON string. It will be `JSON.stringify` unless `compilerOptions.customJSONSerializer` is true.
The value argument should the return of the compiled GraphQL function.
The value argument should be the return of the compiled GraphQL function.

## LICENSE

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/execution.test.ts
Expand Up @@ -2,6 +2,7 @@
* Based on https://github.com/graphql/graphql-js/blob/master/src/execution/__tests__/execution-test.js
*/

import { makeExecutableSchema } from "@graphql-tools/schema";
import {
DocumentNode,
ExecutableDefinitionNode,
Expand All @@ -16,7 +17,6 @@ import {
parse
} from "graphql";
import { CompiledQuery, compileQuery, isCompiledQuery } from "../execution";
import { makeExecutableSchema } from "@graphql-tools/schema";

function executeArgs(args: any) {
const {
Expand Down Expand Up @@ -67,7 +67,7 @@ describe("Execute: Handles basic execution tasks", () => {
})
});

expect(() => executeQuery(schema)).toThrow("Must provide document");
expect(() => executeQuery(schema)).toThrow("Must provide document.");
});
test("throws if no resolve info enricher is not a function", async () => {
const schema = new GraphQLSchema({
Expand Down

0 comments on commit f522b86

Please sign in to comment.