GraphQL(5):使用Mutations修改数据

GraphQL中的使用Mutation查询,下面介绍详细操作,新建一个mutation.js文件。

1 定义schema

1.1 定义模型对象

(1)定义输入模型

复制代码
    input AccountInput {
        name: String
        age: Int
        sex: String
        department: String
    }

(2)定义输出模型

复制代码
    type Account {
        name: String
        age: Int
        sex: String
        department: String
    }

1.2 定义查询操作

定义了一个查询操作,返回所有用户数组

复制代码
    type Query {
        accounts: [Account]
    }

1.3 定义变更操作

复制代码
    type Mutation {
        createAccount(input: AccountInput): Account
        updateAccount(id: ID!, input: AccountInput): Account
    }

2 实现处理器

2.1 定义查询处理函数

复制代码
    accounts() {
        var arr = [];
        for(const key in fakeDb) {
            arr.push(fakeDb[key])
        }
        return arr;
    },

2.2 定义变更处理函数

复制代码
    createAccount({ input }) {
        // 相当于数据库的保存
        fakeDb[input.name] = input;
        // 返回保存结果
        return fakeDb[input.name];
    },

    updateAccount({ id, input }) {
        // 相当于数据库的更新
        const updatedAccount = Object.assign({}, fakeDb[id], input);
        fakeDb[id] = updatedAccount;
        // 返回保存结果
        return updatedAccount;
    }

3 完整代码

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

// 定义schema,查询和类型, mutation
const schema = buildSchema(`
    input AccountInput {
        name: String
        age: Int
        sex: String
        department: String
    }
    type Account {
        name: String
        age: Int
        sex: String
        department: String
    }
    type Mutation {
        createAccount(input: AccountInput): Account
        updateAccount(id: ID!, input: AccountInput): Account
    }
    type Query {
        accounts: [Account]
    }
`)

const fakeDb = {};

// 定义查询对应的处理器
const root = {
    accounts() {
        var arr = [];
        for(const key in fakeDb) {
            arr.push(fakeDb[key])
        }
        return arr;
    },
    createAccount({ input }) {
        // 相当于数据库的保存
        fakeDb[input.name] = input;
        // 返回保存结果
        return fakeDb[input.name];
    },

    updateAccount({ id, input }) {
        // 相当于数据库的更新
        const updatedAccount = Object.assign({}, fakeDb[id], input);
        fakeDb[id] = updatedAccount;
        // 返回保存结果
        return updatedAccount;
    }
}

const app = express();

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

app.listen(3000);

4 实例测试

启动程序

复制代码
node mutation.js

访问http://localhost:3000/graphql

测试新增功能

测试修改功能

测试查询功能

相关推荐
sunnyday04266 分钟前
Spring Boot 项目中使用 Dynamic Datasource 实现多数据源管理
android·spring boot·后端
晴虹9 分钟前
lecen:一个更好的开源可视化系统搭建项目--页面设计器(表单设计器)--全低代码|所见即所得|利用可视化设计器构建你的应用系统-做一个懂你的人
前端·后端·低代码
苏三的开发日记18 分钟前
Hive的安装与配置
后端
小楼v37 分钟前
一篇学会在IDEA中的Git常用操作及冲突的解决方法
git·后端
EMQX1 小时前
利用 EMQX 消息队列解决关键物联网消息传递挑战
人工智能·后端·物联网·mqtt·emqx
red润1 小时前
Python环境变量自动配置:实现生产与开发环境无缝切换
后端·python
给我推荐个名字1 小时前
pageoffice提取Excel表格区域数据
后端
小肥宅仙女2 小时前
限流方案
前端·后端
SadSunset2 小时前
关于springAOP的总结
java·后端·spring
计算机学姐2 小时前
基于SpringBoot的演出购票系统【2026最新】
java·vue.js·spring boot·后端·spring·tomcat·intellij-idea