Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement GraphQL subscription #115

Merged
merged 6 commits into from Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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