-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructing_types_programmatically.js
97 lines (90 loc) · 1.96 KB
/
constructing_types_programmatically.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
var express = require('express');
var { graphqlHTTP } = require('express-graphql');
var graphql = require('graphql');
// Maps id to User object
var fakeDatabase = {
'a': {
id: 'a',
name: 'alice',
},
'b': {
id: 'b',
name: 'bob',
},
};
// Define the User type
var userType = new graphql.GraphQLObjectType({
name: 'User',
fields: {
id: { type: graphql.GraphQLString },
name: { type: graphql.GraphQLString },
}
});
// Define the Query type
var queryType = new graphql.GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: userType,
// `args` describes the arguments that the `user` query accepts
args: {
id: { type: graphql.GraphQLString }
},
resolve: (_, {id}) => {
return fakeDatabase[id];
}
}
}
});
var schema = new graphql.GraphQLSchema({query: queryType});
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');
//等同于:
//
//
// var express = require('express');
// var { graphqlHTTP } = require('express-graphql');
// var { buildSchema } = require('graphql');
//
// var schema = buildSchema(`
// type User {
// id: String
// name: String
// }
//
// type Query {
// user(id: String): User
// }
// `);
//
// // Maps id to User object
// var fakeDatabase = {
// 'a': {
// id: 'a',
// name: 'alice',
// },
// 'b': {
// id: 'b',
// name: 'bob',
// },
// };
//
// var root = {
// user: ({id}) => {
// return fakeDatabase[id];
// }
// };
//
// var app = express();
// app.use('/graphql', graphqlHTTP({
// schema: schema,
// rootValue: root,
// graphiql: true,
// }));
// app.listen(4000);
// console.log('Running a GraphQL API server at localhost:4000/graphql');