Apollo Graphql架构拼接冲突

我有一个关于GraphQL架构拼接的问题。 我有两个Graphql模式:

type Name { firstname: String! lastname: String! } type Address { street: String! number: Int! } type User { name: Name! address: Address! } type Query { user(userId: String!): User } 

 type User { age: String! } type Query { user(userId: String!): User } 

我现在尝试使用graphql-tools的mergeSchemas函数来合并模式:

 const schema = mergeSchemas({ schemas: [schema1, schema2] }); 

但是,而不是我想实现(扩展用户types):

 type Name { firstname: String! lastname: String! } type Address { street: String! number: Int! } type User { name: Name! address: Address! age: String! } type Query { user(userId: String!): User } 

它导致了这个:type Name {firstname:String! 姓氏:string! }

 type Address { street: String! number: Int! } type User { name: Name! address: Address! } type Query { user(userId: String!): User } 

只有一个UserTypes显示在最终模式中。 我试图在onTypeConflict中使用onTypeConflict API来扩展types,但我没有做任何结果。

有没有办法通过在冲突上扩展types来合并模式?

这是合并对象types的一个可能的解决scheme。 也许有必要在onTypeConflict按types名称进行过滤,而不是合并每个types。

 import cloneDeep from 'lodash.clonedeep' import { GraphQLObjectType } from 'graphql/type/definition' import { mergeSchemas } from 'graphql-tools' function mergeObjectTypes (leftType, rightType) { if (!rightType) { return leftType } if (leftType.constructor.name !== rightType.constructor.name) { throw new TypeError(`Cannot merge with different base type. this: ${leftType.constructor.name}, other: ${rightType.constructor.name}.`) } const mergedType = cloneDeep(leftType) mergedType.getFields() // Populate _fields for (const [key, value] of Object.entries(rightType.getFields())) { mergedType._fields[key] = value } if (leftType instanceof GraphQLObjectType) { mergedType._interfaces = Array.from(new Set(leftType.getInterfaces().concat(rightType.getInterfaces()))) } return mergedType } const schema = mergeSchemas({ schemas: [schema1, schema2], onTypeConflict: (leftType, rightType) => { if (leftType instanceof GraphQLObjectType) { return mergeObjectTypes(leftType, rightType) } return leftType } }) 

积分: mergeObjectTypes函数是由Jared Wolinsky编写的。

这应该有所帮助

 extend type User { age: String! }