Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 590f897

Browse files
author
Gabriel Saliev
committed
add example GraphQL server and client that use @defer experimental feature
1 parent 9acd264 commit 590f897

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

examples/defer-example/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Example usage
2+
3+
This is the bare minimum that one needs in order to setup GraphQL server that supports @defer and a client that consumes it.
4+
5+
## Server setup
6+
7+
1. cd to /server
8+
2. use ``npm install``
9+
3. start the server with `` node server.js ``
10+
11+
## Client setup
12+
13+
1. cd to /client
14+
2. use ``node client.js`` to execute the example deferred query
15+
16+
Have fun!
17+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import http from 'http';
2+
3+
const data = `query defer_test {
4+
deferTest {
5+
text
6+
... on GraphQLDeferTest @defer {
7+
defferedText
8+
}
9+
}
10+
}`
11+
12+
const options = {
13+
hostname: 'localhost',
14+
port: 4040,
15+
path: '/graphql',
16+
method: 'POST',
17+
headers: {
18+
'Content-Type': 'application/graphql',
19+
'Content-Length': Buffer.byteLength(data)
20+
}
21+
};
22+
23+
const req = http.request(options, (res) => {
24+
console.log(`STATUS: ${res.statusCode}`);
25+
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
26+
27+
res.setEncoding('utf8');
28+
29+
res.on('data', (chunk) => {
30+
console.log(`BODY: ${chunk}`);
31+
});
32+
33+
res.on('end', () => {
34+
console.log('No more data in response.');
35+
});
36+
});
37+
38+
req.on('error', (e) => {
39+
console.error(`problem with request: ${e.message}`);
40+
});
41+
42+
// Write data to request body
43+
req.write(data);
44+
req.end();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"express": "^4.17.1",
4+
"express-graphql": "0.12.0-experimental-stream-defer.1",
5+
"graphql": "15.4.0-experimental-stream-defer.1"
6+
}
7+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const express = require('express');
2+
const { graphqlHTTP } = require('express-graphql');
3+
const { buildSchema } = require('graphql');
4+
5+
// Construct a schema, using GraphQL schema language
6+
var schema = buildSchema(`
7+
type Query {
8+
hello: String
9+
deferTest: GraphQLDeferTest
10+
}
11+
12+
type GraphQLDeferTest {
13+
text: String
14+
defferedText: String
15+
}
16+
`);
17+
18+
const sleep = (t = 1000) => new Promise((res) => setTimeout(res, t));
19+
20+
// Model
21+
class GraphQLDeferTest {
22+
constructor() {}
23+
24+
async text() {
25+
return "Peter Parker"
26+
}
27+
28+
async defferedText() {
29+
await sleep(5000)
30+
31+
return 'Took a long time, he?'
32+
}
33+
}
34+
35+
// Query resolvers
36+
var root = {
37+
hello: () => 'Hello World',
38+
deferTest: async () => new GraphQLDeferTest(),
39+
};
40+
41+
var app = express();
42+
43+
app.use('/graphql', graphqlHTTP({
44+
schema: schema,
45+
rootValue: root,
46+
graphiql: true,
47+
}));
48+
49+
app.listen(4040)
50+
console.log('Running a GraphQL API server at http://localhost:4040/graphql');

examples/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,40 @@ import { buildSchema } from 'graphql';
33

44
import { graphqlHTTP } from '../src';
55

6+
// Model
7+
class GraphQLDeferTest {
8+
constructor() {}
9+
10+
// Child resolvers
11+
async text() {
12+
return "Peter Parker"
13+
}
14+
15+
async defferedText() {
16+
await sleep(5000)
17+
18+
return 'Took a long time, he?'
19+
}
20+
}
21+
22+
623
// Construct a schema, using GraphQL schema language
724
const schema = buildSchema(`
825
type Query {
926
hello: String
27+
deferTest: GraphQLDeferTest
28+
}
29+
30+
type GraphQLDeferTest {
31+
text: String
32+
defferedText: String
1033
}
1134
`);
1235

1336
// The root provides a resolver function for each API endpoint
1437
const rootValue = {
1538
hello: () => 'Hello world!',
39+
deferTest: async () => new GraphQLDeferTest(),
1640
};
1741

1842
const app = express();

0 commit comments

Comments
 (0)