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);
相关推荐
陪我一起学编程21 分钟前
关于nvm与node.js
vue.js·后端·npm·node.js
舒一笑1 小时前
基于KubeSphere平台快速搭建单节点向量数据库Milvus
后端
JavaBuild1 小时前
时隔半年,拾笔分享:来自一个大龄程序员的迷茫自问
后端·程序员·创业
一只叫煤球的猫2 小时前
虚拟线程生产事故复盘:警惕高性能背后的陷阱
java·后端·性能优化
周杰伦fans3 小时前
C#中用于控制自定义特性(Attribute)
后端·c#
Livingbody3 小时前
GitHub小管家Trae智能体介绍
后端
考虑考虑4 小时前
rollback-only事务
spring boot·后端·spring
mrchan4 小时前
mongodb数组避免过大
后端
AirMan4 小时前
除了Cookie会话跟踪技术,你还知道其他的会话跟踪技术吗?会话技术的方案对比
后端
uhakadotcom4 小时前
Python中@staticmethod和@classmethod的简单理解与实用示例
javascript·后端