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);
相关推荐
江南一点雨11 分钟前
ChatGPT与最大似然估计
后端
程序员爱钓鱼1 小时前
Go语言实战案例-判断一个数是否为质数
后端·google·go
程序员爱钓鱼1 小时前
Go语言实战案例-读取本地文本文件内容
后端·google·go
mCell9 小时前
Go 并发编程基础:从 Goroutine 到 Worker Pool 实践
后端·性能优化·go
Python智慧行囊10 小时前
Flask 框架(一):核心特性与基础配置
后端·python·flask
代码搬运媛10 小时前
HTTP REST API、WebSocket、 gRPC 和 GraphQL 应用场景和底层实现
websocket·http·graphql
ん贤11 小时前
如何加快golang编译速度
后端·golang·go
摸鱼仙人~13 小时前
Spring Boot 参数校验:@Valid 与 @Validated
java·spring boot·后端
思无邪667513 小时前
从零构建搜索引擎 build demo search engine from scratch
后端
Littlewith13 小时前
Node.js:创建第一个应用
服务器·开发语言·后端·学习·node.js