This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.ts
71 lines (60 loc) · 1.68 KB
/
server.ts
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
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import { makeExecutableSchema, IResolvers } from 'graphql-tools';
import { GraphQLSchema } from 'graphql';
import cors from 'cors';
import casual from 'casual';
import shortid from 'shortid';
import http from 'http';
import { logger } from '../utils';
import { config } from '../config';
interface IBook {
id: string;
title: string;
author: string;
}
const books: IBook[] = [
{ id: shortid.generate(), title: casual.title, author: casual.name },
{ id: shortid.generate(), title: casual.title, author: casual.name }
];
const typeDefs: string = `
type Book {
id: ID!
title: String
author: String
}
type Query {
books: [Book]!
}
`;
const resolvers: IResolvers = {
Book: {
title: (book: IBook): string => {
return book.title.toUpperCase();
}
},
Query: {
books: (): Promise<IBook[]> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(books);
}, 2000);
});
}
}
};
const schema: GraphQLSchema = makeExecutableSchema({ typeDefs, resolvers });
async function start(): Promise<http.Server> {
const app: express.Application = express();
app.use(cors());
app.use(config.GRAPHQL_ROUTE, bodyParser.json(), graphqlExpress({ schema }));
app.use(config.GRAPHIQL_ROUTE, graphiqlExpress({ endpointURL: config.GRAPHQL_ROUTE }));
return app.listen(config.PORT, () => {
logger.info(`Go to ${config.GRAPHQL_ENDPOINT} to run queries!`);
});
}
if (process.env.NODE_ENV !== 'test') {
start();
}
export { start, schema, resolvers, IBook, books };