graphql - Apollo Graphql : Rename schema for backward compatibility

标签 graphql apollo apollo-server express-graphql

我想做什么?

在 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/

相关文章:

node.js - 如何使用apollo服务器对graphQl中的数据进行排序?

angular - graphql Angular 突变在控制台中显示 "Error: Network error: Cannot read property ' 长度为 null"

GraphQL:无法读取 Apollo 客户端中的响应 header

reactjs - Next JS config 多插件配置

typescript - typescript 和 graphql-codegen 之间的枚举不匹配

javascript - Apollo服务器+ React Apollo pubSub不显示数据

c# - 将 GraphQL 查询映射到 Select() linq 到 sql

javascript - Graphql 自定义连接

android - 在带有 Kotlin 和流程的 Android 上使用带有 Apollo 的 GraphQL 订阅时处理网络错误

graphql - 如何知道 GraphQL 查询中请求了哪些字段?