-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (76 loc) · 1.86 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const express = require('express');
const cors = require("cors");
const graphql = require('graphql');
const { graphqlHTTP } = require("express-graphql")
const { Mongoose, default: mongoose } = require('mongoose');
const product = require('./Models/Product')
const user = require('./Models/User')
require("dotenv").config();
const app = express();
const port = process.env.PORT || 8000;
app.use(cors());
app.use(express.json());
const { GraphQLObjectType, GraphQLString, GraphQLSchema, buildSchema } = graphql
app.use('/graphql', graphqlHTTP({
schema: buildSchema(`
type products
{
_id : String!
name : String!
family : String!
scientificName : String!
}
type user{
name : String!
}
type RootQuery
{
data : [products!]!
user : [user!]!
}
type RootMutation
{
getData(data: String): String
}
schema {
query: RootQuery
mutation: RootMutation
}
`
),
rootValue:
{
data: () => {
return product.find().
then(events =>
{
return events.map(event=>{
console.log(...event._doc);
return {...event._doc};
})
}).
catch(err=>
{
throw err;
})
},
user : async () => {
const users = await user.find({})
return users;
},
getData: (args) => {
const newData = args.name;
return newData
}
},
graphiql: true
}))
app.get("/", (req, res) => {
res.send("graphql practice Running");
});
app.listen(port, () => {
mongoose.connect(`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@birdly.ou1diwj.mongodb.net/birdly?retryWrites=true&w=majority`).catch((err) => {
console.log(err);
})
console.log("Graphql practice in port ", port);
});