-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgraphql-compose.js
54 lines (48 loc) · 1.14 KB
/
graphql-compose.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @flow
import { ApolloServer } from 'apollo-server';
import { schemaComposer } from 'graphql-compose';
import { authors, articles } from './data';
const AuthorType = schemaComposer.createObjectTC(`
"Author data"
type Author {
id: Int
name: String
}
`);
const ArticleType = schemaComposer.createObjectTC({
name: 'Article',
description: 'Article data with related Author data',
fields: {
title: 'String!',
text: 'String',
authorId: 'Int!',
author: {
type: () => AuthorType,
resolve: source => {
const { authorId } = source;
return authors.find(o => o.id === authorId);
},
},
},
});
schemaComposer.Query.addFields({
articles: {
args: {
limit: { type: 'Int', defaultValue: 3 },
},
type: [ArticleType],
resolve: (_, args) => {
const { limit } = args;
return articles.slice(0, limit);
},
},
authors: {
type: [AuthorType],
resolve: () => authors,
},
});
const schema = schemaComposer.buildSchema();
const server = new ApolloServer({ schema });
server.listen(5000).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});