GraphQL(7):ConstructingTypes

1 使用GraphQLObjectType 定义type(类型)

不使用ConstructingTypes定义方式如下:

使用ConstructingTypes定义方式如下:

更接近于构造函数方式

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

2 使用GraphQLObjectType 定义Query(查询)

不使用ConstructingTypes定义方式如下:

使用ConstructingTypes定义方式如下:

复制代码
var queryType = new graphql.GraphQLObjectType({
    name: 'Query',
    fields: {
        account: {
            type: AccountType,
            // `args` describes the arguments that the `user` query accepts
            args: {
                username: { type: graphql.GraphQLString }
            },
            resolve: function (_, { username }) {
                const name = username;
                const sex = 'man';
                const age = 18;
                const department = '开发部';
                return {
                    name,
                    sex,
                    age,
                    department
                }
            }
        }
    }
});

3 创建schema

复制代码
var schema = new graphql.GraphQLSchema({ query: queryType });

4 代码实现如下

复制代码
const express = require('express');
const graphql = require('graphql');
const grapqlHTTP = require('express-graphql').graphqlHTTP;

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` describes the arguments that the `user` query accepts
            args: {
                username: { type: graphql.GraphQLString }
            },
            resolve: function (_, { username }) {
                const name = username;
                const sex = 'man';
                const age = 18;
                const department = '开发部';
                return {
                    name,
                    sex,
                    age,
                    department
                }
            }
        }
    }
});

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

const app = express();

app.use('/graphql', grapqlHTTP({
    schema: schema,
    graphiql: true
}))

// 公开文件夹,供用户访问静态资源
app.use(express.static('public'))

app.listen(3000);
相关推荐
uzong3 小时前
面试官:Redis中的 16 库同时发送命令,服务端是串行执行还是并行执行
后端·面试·架构
追逐时光者5 小时前
.NET 使用 MethodTimer 进行运行耗时统计提升代码的整洁性与可维护性!
后端·.net
你的人类朋友5 小时前
【Node.js】什么是Node.js
javascript·后端·node.js
David爱编程7 小时前
面试必问!线程生命周期与状态转换详解
java·后端
LKAI.8 小时前
传统方式部署(RuoYi-Cloud)微服务
java·linux·前端·后端·微服务·node.js·ruoyi
Victor3568 小时前
Redis(11)如何通过命令行操作Redis?
后端
Victor3568 小时前
Redis(10)如何连接到Redis服务器?
后端
他日若遂凌云志9 小时前
深入剖析 Fantasy 框架的消息设计与序列化机制:协同架构下的高效转换与场景适配
后端
快手技术10 小时前
快手Klear-Reasoner登顶8B模型榜首,GPPO算法双效强化稳定性与探索能力!
后端
二闹10 小时前
三个注解,到底该用哪一个?别再傻傻分不清了!
后端