简体   繁体   English

GraphQLServer 联合类型“抽象类型 N 必须在运行时解析为对象类型”错误

[英]GraphQLServer union types "Abstract type N must resolve to an Object type at runtime" error

Given a GraphQL union return type:给定一个 GraphQL union返回类型:

union GetBankAccountsResponseOrUserInputRequest = GetAccountsResponse | UserInputRequest

meant to be returned by a given resolver:意味着由给定的解析器返回:

type Query {
  getBankAccounts: GetBankAccountsResponseOrUserInputRequest!
}

I'm getting these warnings or errors regarding a __resolveType or __isTypeOf function:我收到有关__resolveType__isTypeOf函数的这些警告或错误:

Abstract type N must resolve to an Object type at runtime for field Query.getBankAccounts with value { ..., __isTypeOf: [function __isTypeOf] }, received "undefined". Either the N type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.'

I've searched for days in github issues and SO questions looking to resolve this error.我已经在 github 问题和 SO 问题中搜索了几天,以期解决此错误。

Implementing __resolveType or __isTypeOf in my resolvers is not working:在我的解析器中实现__resolveType__isTypeOf不起作用:

export const Query = {
  getBankAccounts: (parent, args, context: IContext, info) => {
    return {
      __isTypeOf(obj) { // OR __resolveType, none of them work
        return `GetAccountsResponse`;
      },
      accounts: []
    };
  },
};

Since implementing __resolveType or __isTypeOf in my resolvers didn't work for me, I ended up solving this issue by adding the __typename directly into the resolver return Object.由于在我的解析器中实现__resolveType__isTypeOf对我不起作用,我最终通过将__typename直接添加到解析器返回对象中来解决这个问题。

In the getBankAccounts resolver implementation:getBankAccounts解析器实现中:

async getBankAccounts(): GetBankAccountsResponseOrUserInputRequest {
    if (shouldGetUserInputRequest()) {
      return {
        __typename: "UserInputRequest",
        ...response,
      };
    }

    return {
      __typename: "GetAccountsResponse",
      accounts,
    };
  }

Hope that this helps somebody.希望这对某人有帮助。

Your resolver map is not set up correctly.您的解析器映射设置不正确。

The resolver map passed to ApolloServer (or makeExecutableSchema ) should should be an object whose keys are type names in your schema, with each key mapping to another object whose keys are field names on that type.传递给ApolloServer (或makeExecutableSchema )的解析器映射应该是一个对象,其键是模式中的类型名称,每个键映射到另一个对象,其键是该类型的字段名称。 Each field name then maps to a resolver function.然后每个字段名称映射到解析器函数。

const resolvers = {
  SomeType: {
    someField: (parent, args, context, info) => { ... },
  },
}

You use the same resolver map to pass in the resolveType function for a union or interface.您使用相同的解析器映射为联合或接口传递resolveType函数。 The pattern is the same, but the name of the type is the name of the union or interface and instead of a field name, the key is __resolveType :模式是相同的,但类型的名称是联合或接口的名称,而不是字段名称,键是__resolveType

const resolvers = {
  SomeType: {
    someField: (parent, args, context, info) => { ... },
  },
  SomeUnion: {
    __resolveType: (parent) => { ... },
  }
}

The resolveType function should always return a string that matches the name of an existing object type in your schema. resolveType函数应始终返回与架构中现有对象类型名称匹配的字符串。

If you use __isTypeOf instead of __resolveType , then this will look a bit different because the isTypeOf function is associated with a particular object type, not the interface or union.如果您使用__isTypeOf而不是__resolveType ,那么这看起来会有些不同,因为isTypeOf函数与特定的对象类型相关联,而不是接口或联合。 This is all shown in the docs . 这都显示在文档中 So the resolver map looks like this:所以解析器映射如下所示:

const resolvers = {
  SomeType: {
    someField: (parent, args, context, info) => { ... },
    __isTypeOf: (parent) => { ... },
  },
}

__isTypeOf should always return either true or false , depending on whether the passed in object is in fact that type. __isTypeOf应始终返回truefalse ,具体取决于传入的对象是否实际上是该类型。

You only need to use either __resolveType or __isTypeOf .您只需要使用__resolveType__isTypeOf If you use __isTypeOf , you have to add it to every possible object type in the union or interface.如果使用__isTypeOf ,则必须将其添加到联合或接口中的每个可能的对象类型中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM