gpt4 book ai didi

graphql - Apollo Graphql : Rename schema for backward compatibility

转载 作者:行者123 更新时间:2023-12-05 08:06:34 25 4
gpt4 key购买 nike

我想做什么?

在 Apollo Graphl 服务器中,我想将模式中的实体 Person 更改为 Human 但我不想破坏我的客户端(正在查询 graphql 的前端) .因此,如果客户端正在查询 Person,我想将其映射到 Human

示例:

客户查询

查询{
人 {
ID

}
}

重写到

查询{
人类 {
ID
名称
}
}

重写响应

{
数据: {
人: {
编号:123,
名称:“abc”
}
}
}

我尝试过的事情

graphql-rewriter提供类似于我正在寻找的东西。我浏览了它的文档,但它没有重写字段名称的选项。

在 apollo graphql 文档中 Apollow graphql directives , 他们提到了重命名指令,但我没有找到 rename-directive-package 节点模块。

apollo-directives-package我也试过这个,但它没有重命名缩放器字段的选项,例如

import { makeExecutableSchema } from "graphql-tools";
import { RenameDirective } from "rename-directive-package";

const typeDefs = `
type Person @rename(to: "Human") {
name: String!
currentDateMinusDateOfBirth: Int @rename(to: "age")
}`;

const schema = makeExecutableSchema({
typeDefs,
schemaDirectives: {
rename: RenameDirective
}
});

如有任何建议/帮助,我们将不胜感激。

最佳答案

我希望这对你有帮助,首先我们必须创建模式指令

import { SchemaDirectiveVisitor } from "graphql-tools";
import { GraphQLObjectType, defaultFieldResolver } from "graphql";

/**
*
*/
export class RenameSchemaDirective extends SchemaDirectiveVisitor {
/**
*
* @param {GraphQLObjectType} obj
*/
visitObject(obj) {
const { resolve = defaultFieldResolver } = obj;


obj.name = this.args.to;
console.log(obj);
}
}

type-defs.js

directive @rename(to: String!) on OBJ

type AuthorizedUser @rename(to: "Human1") {
id: ID!
token: ID!
fullName: String!
roles: [Role!]!
}

关于graphql - Apollo Graphql : Rename schema for backward compatibility,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60321331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com