Toc
  1. 使用构造函数GraphQLObjectType定义type,便于提高可维护性(快速定位错误)
Toc
0 results found
bbcfive
使用GraphQLObjectType定义type
2020/06/03 前端 graphQL

使用构造函数GraphQLObjectType定义type,便于提高可维护性(快速定位错误)

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
const express = require('express');
const graphql = require('graphql');
const graphqlHTTP = require('express-graphql');

var AccountType = new graphql.GraphQLObjectType({
name: 'Account',
fields: {
name: { type: graphql.GraphQLString },
age: { type: graphql.GraphQLInt },
sex: { type: graphql.GraphQLString },
department: { type: graphql.GraphQLString }
}
});

var queryType = new graphql.GraphQLObjectType({
name: 'Query',
fields: {
account: {
type: AccountType,
args: {
username: { type: graphql.GraphQLString }
},
resolve: function (_, { username }) {
const name = username;
const sex = 'male';
const age = 18;
const department = 'school';
return {
name,
sex,
age,
department
}
}
}
}
});

var schema = new graphql.GraphQLSchema({ query: queryType });

const app = express();

app.use('/graphql', graphqlHTTP({
schema: schema,
graphql: true
}));

app.listen(3000);
本文作者:bbcfive
版权声明:本文首发于bbcfive的博客,转载请注明出处!