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);
相关推荐
Csvn7 分钟前
🐍 Day 6: Python 异常处理 — 防御式编程的核心
后端·python
叫我少年11 分钟前
Git SSH 配置:从生成密钥到远程连接
git·后端
IT_陈寒14 分钟前
Python多进程池的坑:子进程竟然不会退出
前端·人工智能·后端
叫我:松哥1 小时前
基于flask图书管理系统,技术栈flask+layui+sqlite+Echarts
后端·python·数据分析·flask·echarts·layui
铁皮饭盒1 小时前
网页端, 5.5mb谷歌模型, 识别躯干, 视频都不卡
前端·javascript·后端
WongKyunban1 小时前
Go语言的简洁并发编程
开发语言·后端·golang
mldong1 小时前
一条命令,十分钟:jeeflow 工作流应用的六语言一键部署
前端·后端·架构
Shawn_Shawn1 小时前
本体论的基本核心概念
后端·llm·领域驱动设计
凤山老林11 小时前
Spring Boot 3.x AOT 编译实战:从原理、踩坑到生产落地
java·spring boot·后端
博傅11 小时前
Spring 核心原理
java·后端·spring