首页 文章

Apollo Graphql Schema拼接冲突

提问于
浏览
2

我有关于GraphQL Schema拼接的问题 . 我有两个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]
});

但不是我想要实现的(扩展的用户类型):

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
}

它导致了这个:类型名称{firstname:String!姓氏:字符串! }

type Address {
   street: String!
   number: Int!
}

type User {
   name: Name!
   address: Address!
}

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

最终架构中只显示一个UserTypes . 我尝试使用 mergeSchemas 中的 onTypeConflict API扩展Type但我没有取得任何结果 .

有没有办法通过扩展冲突类型来合并Schemas?

2 回答

  • 1

    这是合并对象类型的可能解决方案 . 也许有必要在 onTypeConflict 中按类型名称进行过滤,而不是合并每种类型 .

    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编写 .

  • 1

    这应该有所帮助

    extend type User {
       age: String!
    }
    

相关问题