-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhello2.js
57 lines (51 loc) · 1.51 KB
/
hello2.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
var express = require('express');
var graphqlHTTP = require('express-graphql');
var Graphql = require('graphql');
const personType = new Graphql.GraphQLObjectType({
name: "Person",
fields:{
name: { type: Graphql.GraphQLString },
age: { type: Graphql.GraphQLInt }
}
});
const queryType = new Graphql.GraphQLObjectType({
name: "Query",
fields: {
hello: {
type: Graphql.GraphQLString,
resolve: () => 'Hello world!',
},
persons: {
type: new Graphql.GraphQLList(personType),
args: {
name: {type: Graphql.GraphQLString},
age: {type: Graphql.GraphQLInt}
},
resolve: (post, args, context, info) => {
console.log(context);
console.log(args);
const {name, age} = args;
return [
{name:"kim", age: 20},
{name:"lee", age: 30},
{name:"park", age: 40},
].filter((person) => {
if(!name && !age){ return true; }
if(!age && name && person.name === name){ return true; }
if(!name && age && person.age === age){ return true; }
if(name && age && person.name === name && person.age === age){ return true; }
return false;
});
}
}
}
});
const schema = new Graphql.GraphQLSchema({query: queryType});
var app = express();
const session = {id: "1001", expires: 20000};
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true,
context: session,
}));
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));