【问题标题】:Apollo Graphql: Rename schema for backward compatibilityApollo Graphql:重命名模式以实现向后兼容性
【发布时间】:2020-06-04 20:39:56
【问题描述】:

我想做什么?

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

示例:

客户查询

query { Person { ID firstName } }

重写到

query { Human { ID name } }

重写回复

{ data: { Person: { Id: 123, name:"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
  }
});

任何建议/帮助将不胜感激。

【问题讨论】:

    标签: graphql apollo apollo-server express-graphql


    【解决方案1】:

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

    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!]!
      }
    
    

    【讨论】:

      猜你喜欢
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      相关资源
      最近更新 更多