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

测试新增功能

测试修改功能

测试查询功能

相关推荐
码事漫谈1 小时前
人设带处境,AI 更入戏
后端
掘金酱1 小时前
【10.24 特别活动】AI 时代,程序员的新饭碗在哪里?
前端·人工智能·后端
leeyi1 小时前
缓存同步之后,还要考虑没收到消息的节点
后端·性能优化·erlang
IT_陈寒1 小时前
Redis的原子性竟然失效了?记一次分布式锁翻车实录
前端·人工智能·后端
shengjk11 小时前
既然 gRPC 底层也是 HTTP,为什么大厂还非要死磕 RPC?真相全在这!
后端
回家路上绕了弯1 小时前
人和 Agent 怎么一起干活:把任务交出去,把判断留在关键处
后端
波力海苔夹心脆6752 小时前
C# 正则表达式零基础入门:从“看得懂“到“会用“
经验分享·后端·mysql·正则表达式·c#
ServBay2 小时前
DeepSeek 又出王炸,用 DeepInfer 搭建自己的服务是不是很容易?
后端·ai编程·deepseek
.Bryce.3 小时前
MCP+A2A+ANP,Agent通信协议的完整知识地图
java·人工智能·后端·agent·通信协议
JuiceFS3 小时前
面向 EB 级 AI 存储:JuiceFS 企业版架构演进
人工智能·后端