Apollo GraphQL:查询返回不同类型对象的架构?

Vik*_*ikR 1 apollo-server apollostack

我有三个不同的 PostGres 表,每个表包含不同类型的同事。每种类型的数据库字段都不同——这就是它们位于三个单独的表中的原因。

我有一个组件可以访问任何类型的同事。现在从我迄今为止遇到的示例来看,一个组件通常与一个 GraphQL 查询相关联,例如:

const withData = graphql(GETONEASSOCIATE_QUERY, {
    options({ navID }) {
        return {
            variables: { _id: navID}
        };
    }
    ,
    props({ data: { loading, getOneAssociate } }) {
        return { loading, getOneAssociate };
    },


});

export default compose(
    withData,
    withApollo
)(AssociatesList);
Run Code Online (Sandbox Code Playgroud)

似乎给定的 GraphQL 查询只能返回单一类型的记录,例如在模式中:

getOneAssociate(associateType: String): [associateAccountingType]
Run Code Online (Sandbox Code Playgroud)

问题:是否可以设计一个 GraphQL 模式,使得单个查询可以返回不同类型的对象?解析器可以接收一个 AssociateType 参数,该参数告诉它要引用哪个 postGres 表。但是架构会是什么样子,以便它可以根据需要返回 AssociateAccountingType、associateArtDirectorType、associateAccountExecType 等类型的对象?

预先感谢所有人提供的任何信息。

dav*_*aha 5

这里你有两个选择。声明一个 Interface 作为返回类型,并确保每个 AssociateType 都扩展该接口。如果所有这些类型都有共同字段,那么这是一个好主意。它看起来像这样:

interface associateType {
  id: ID
  department: Department
  employees: [Employee]
}

type associateAccountingType implements associateType {
  id: ID
  department: Department
  employees: [Employee]
  accounts: [Account]
}

type associateArtDirectorType implements associateType {
  id: ID
  department: Department
  employees: [Employee]
  projects: [Project]
}
Run Code Online (Sandbox Code Playgroud)

如果您没有任何公共字段,或者由于某种原因您希望这些类型不相关,则可以使用联合类型。此声明要简单得多,但要求您为查询的每个字段使用一个片段,因为引擎假设这些类型没有公共字段。

union associateType = associateAccountingType | associateArtDirectorType | associateAccountExecType
Run Code Online (Sandbox Code Playgroud)

更重要的一件事是如何实现一个解析器,它将告诉您的 graphql 服务器和您的客户端实际的具体类型是什么。对于 apollo,您需要在 union/interace 类型上提供 __resolveType 函数:

{
  associateType: {
    __resolveType(associate, context, info) {
      return associate.isAccounting ? 'associateAccountingType' : 'associateArtDirectorType';
    },
  }
},
Run Code Online (Sandbox Code Playgroud)

该函数可以实现您想要的任何逻辑,但它必须返回您正在使用的类型的名称。associate参数将是您从父解析器返回的实际对象。context是您常用的上下文对象,并info保存查询和模式信息。