-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (42 loc) · 1.21 KB
/
index.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
const PORT = 5000;
// ----------------------------------------------------------------
const express = require("express");
const { graphqlHTTP } = require("express-graphql");
const { GraphQLSchema, GraphQLObjectType, GraphQLList } = require("graphql");
const { books, authors } = require("./data");
const { AuthorModel, BookModel } = require("./models");
const { BookServices } = require("./services");
const app = express();
const RootQueryType = new GraphQLObjectType({
name: "Query",
description: "Root Query",
fields: () => ({
books: {
type: new GraphQLList(BookModel),
description: "",
resolve: () => books, // return all books
},
authors: {
type: new GraphQLList(AuthorModel),
resolve: () => authors, // return all authors
},
}),
});
const RootMutationType = new GraphQLObjectType({
name: "Mutation",
description: "Root Mutation",
fields: () => BookServices,
});
const schema = new GraphQLSchema({
query: RootQueryType,
mutation: RootMutationType,
});
// ----------------------------------------------------------------
app.use(
"/graphql",
graphqlHTTP({
graphiql: true,
schema: schema,
})
);
app.listen(PORT, () => console.log("Running at " + PORT));