-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathgraphql.js
More file actions
74 lines (68 loc) · 1.54 KB
/
graphql.js
File metadata and controls
74 lines (68 loc) · 1.54 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// @flow
import { ApolloServer } from 'apollo-server';
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
} from 'graphql';
import { authors, articles } from './data';
const AuthorType = new GraphQLObjectType({
name: 'Author',
description: 'Author data',
fields: () => ({
id: { type: GraphQLInt },
name: { type: GraphQLString },
}),
});
const ArticleType = new GraphQLObjectType({
name: 'Article',
description: 'Article data with related Author data',
fields: () => ({
title: {
type: new GraphQLNonNull(GraphQLString),
},
text: {
type: GraphQLString,
},
authorId: {
type: new GraphQLNonNull(GraphQLInt),
description: 'Record id from Author table',
},
author: {
type: AuthorType,
resolve: source => {
const { authorId } = source;
return authors.find(o => o.id === authorId);
},
},
}),
});
const Query = new GraphQLObjectType({
name: 'Query',
fields: {
articles: {
args: {
limit: { type: GraphQLInt, defaultValue: 3 },
},
type: new GraphQLList(ArticleType),
resolve: (_, args) => {
const { limit } = args;
return articles.slice(0, limit);
},
},
authors: {
type: new GraphQLList(AuthorType),
resolve: () => authors,
},
},
});
const schema = new GraphQLSchema({
query: Query,
});
const server = new ApolloServer({ schema });
server.listen(5000).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});