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);

相关推荐

  1. GraphQL入门

    2024-06-13 16:44:03       48 阅读
  2. GraphQL注入

    2024-06-13 16:44:03       35 阅读
  3. Graphql mock 方案

    2024-06-13 16:44:03       37 阅读
  4. 什么是graphQL

    2024-06-13 16:44:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-13 16:44:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-13 16:44:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-13 16:44:03       87 阅读
  4. Python语言-面向对象

    2024-06-13 16:44:03       96 阅读

热门阅读

  1. pycharm 包安装失败,换源下载,一行命令

    2024-06-13 16:44:03       23 阅读
  2. 【VS2022 编译UE5.1 错误 C4834 】

    2024-06-13 16:44:03       34 阅读
  3. 算法训练营day58

    2024-06-13 16:44:03       35 阅读
  4. 绝对人机交互和相对人机交互

    2024-06-13 16:44:03       33 阅读
  5. 狭义人机交互与广义人机交互

    2024-06-13 16:44:03       29 阅读
  6. Oracle数据库面试题-12

    2024-06-13 16:44:03       20 阅读
  7. 算法第9章 图算法设计

    2024-06-13 16:44:03       21 阅读
  8. define与typedef的区别和使用

    2024-06-13 16:44:03       31 阅读
  9. 图片角度调整 适配缩放 transform scale rotate

    2024-06-13 16:44:03       30 阅读