typescript - 模式拼接通过添加前缀解决冲突

标签 typescript graphql apollo apollo-server graphql-tools

所以我有这两个模式

架构1

type Permission {
    relation: Relation
}

enum Relation {
    ONE
    TWO
    THREE
}

模式2

type Permission {
    relation: Relation
}

enum Relation {
    FOUR
    FIVE
    SIX
}  

预期结果类似于:(但我对不同的想法持开放态度) 合并后我想提出的查询是:

{
    permissions{
        relation
    }
}

得到这样的结果

"permissions": [
  {
    "relation": "ONE"
  },
  {
    "relation": "SIX"
  }
]

"permissions": [
  {
    "relation": "schema1ONE"
  },
  {
    "relation": "schema2SIX"
  }
]

还有像这样的突变:

mutation{
  createPermission(
    relation: ONE
  ){
    relation
  }
}

mutation{
  createPermission(
    relation: SIX
  ){
    relation
  }
}

mutation{
  createPermission(
    relation: schema1ONE
  ){
    relation
  }
}

mutation{
  createPermission(
    relation: schema2SIX
  ){
    relation
  }
}

我正在尝试在 graphql-tools 上使用 transformSchema 函数,但不能完全正确地计算出来:

const Schema1 = await getRemoteSchema('schema1_url', 'schema1');
const Schema2 = await getRemoteSchema('schema2_url', 'schema2');

const schemas = [Schema1, Schema2]

const schema = mergeSchemas({
  schemas: schemas,
  resolvers: {}
});

getRemoteSchema 定义

export const getRemoteSchema = async (uri: string, schemaName: string): Promise<GraphQLSchema> => {
  const httpLink = new HttpLink({ uri, fetch });

  const schema = await introspectSchema(httpLink);

  const executableSchema = makeRemoteExecutableSchema({
    schema,
    httpLink,
  });

  // transform schema by renaming root fields and types
  const renamedSchema = transformSchema(
    executableSchema,
    [
      new RenameTypes(name => {
        if (name == 'Relation') {
          return schemaName + name
        } else {
          return name
        }
      }),
      // new RenameRootFields((operation, name) => `${schemaName}_${name}`)
    ]
  );

  return renamedSchema;
}    

我犯了这个错误 https://glitch.com/edit/#!/schema-stitching-conflict 因此更容易看到问题。

最佳答案

您需要 RenameTypesRenameRootFields 转换, RenameTypes 转换类型名

来自:PermissionRelation(冲突类型),

到:schema1_Permissionschema2_Permission

和:schema1_Relationschema1_Relation

RenameRootFields 转换这些类型的查询名称

来自:permission(id: ID!): Permission

到:schema1_permission(id: ID!): schema1_Permissionschema2_permission(id: ID!): schema2_Permission

和:权限:[Permission]

到:schema1_permissions: [schema1_Permission]schema2_permissions: [schema2_Permission]

转换会是这样的:

const {
  makeExecutableSchema,
  addMockFunctionsToSchema,
  transformSchema,
  RenameTypes,
  RenameRootFields
} = require('graphql-tools');

const schema1 = makeExecutableSchema({
  typeDefs: `
    type Permission {
      id: ID!
      text: String
      relation: Relation
    }

    type Query {
      permissions: [Permission]
      permission(id: ID!): Permission
    }

    enum Relation {
      ONE
      TWO
      THREE
    }
  `
});

addMockFunctionsToSchema({ schema: schema1 });

const renamedSchema1 = transformSchema(
  schema1,
  [
    new RenameTypes(name => {
      if (name == 'Relation' || name == 'Permission') {
        return 'schema1_' + name
      } else {
        return name
      }
    }, { renameBuiltins: false, renameScalars: true }),
    new RenameRootFields((_op, name) => {
      return name.includes('ermission') ? `schema1_${name}` : name
    })
  ]
);

引用资料: https://www.apollographql.com/docs/graphql-tools/schema-transforms/ https://www.apollographql.com/docs/graphql-tools/schema-stitching/

关于typescript - 模式拼接通过添加前缀解决冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54585565/

相关文章:

angular - 当我从 Angular 传递 graphql 查询时,为什么 GraphQL Controller 中的参数查询为空?

apollo - graphql orderBy 查询出现问题

javascript - 为 CommonJS 模块 (Typescript) 导出附加接口(interface)

typescript - 如何在打开的编辑器中搜索具有给定扩展名的文件?

Typescript 将动态变量分配给 Map 对象

syntax-error - 发现 GraphQL gql 语法错误 : Expected Name, }

postgresql - 运行 mix ecto.setup 命令时连接被拒绝,使用 asdf 安装的软件包

java - 在Spring Boot中访问请求头?

angular - typescript 类型 BeforeInstallPromptEvent

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