-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgraphql-tools.js
53 lines (46 loc) · 1009 Bytes
/
graphql-tools.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
// @flow
import { ApolloServer } from 'apollo-server';
import { makeExecutableSchema } from 'graphql-tools';
import { authors, articles } from './data';
const typeDefs = `
"Author data"
type Author {
id: Int
name: String
}
"Article data with related Author data"
type Article {
title: String!
text: String
"Record id from Author table"
authorId: Int!
author: Author
}
type Query {
articles(limit: Int = 10): [Article]
authors: [Author]
}
`;
const resolvers = {
Article: {
author: source => {
const { authorId } = source;
return authors.find(o => o.id === authorId);
},
},
Query: {
articles: (_, args) => {
const { limit } = args;
return articles.slice(0, limit);
},
authors: () => authors,
},
};
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
const server = new ApolloServer({ schema });
server.listen(5000).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});