架构拼接通过添加前缀

时间:2019-02-08 03:14:59

标签: typescript graphql apollo apollo-server graphql-tools

所以我有这两种模式

Schema1

type Permission {
    relation: Relation
}

enum Relation {
    ONE
    TWO
    THREE
}

Schema2

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 因此,更容易发现问题。

2 个答案:

答案 0 :(得分:1)

您需要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

和:permissions: [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/

答案 1 :(得分:0)

当前,我看不到使用 graphql-tools 实现所需行为的简便方法,因为在实现mergeSchemas()时,选项onTypeConflictfirst deprecated and later removed,即使它仍存在于公共界面上。使用该选项,我们可以简单地传递一个回调,该回调知道冲突类型及其对应的AST。

transformSchema()但是,当您尝试使用它时,只会重命名枚举类型名称,而不会重命名枚举值。您很可能需要实现自己的转换,而不是使用预定义的转换来实现目标。不过,我还是建议您看看ConvertEnumValues中的the implementation。在实施自己的Transform时,这可以使您更好地了解如何根据需要来操作和操作AST。

例如,我将考虑一个实现,该实现跟踪它已经看到的所有GraphQlEnumTypes并在遇到名称冲突时对其进行深度合并。您可以使用模块范围内的变量来跟踪,也可以使用Transform中的实例属性来跟踪。如果您选择后者,请不要忘记事先实例化它,并通过引用将其传递给后续的tranformSchema()调用。