Gus*_*Gus 3 typescript graphql
给定 GraphQLunion返回类型:
union GetBankAccountsResponseOrUserInputRequest = GetAccountsResponse | UserInputRequest
Run Code Online (Sandbox Code Playgroud)
意味着由给定的解析器返回:
type Query {
getBankAccounts: GetBankAccountsResponseOrUserInputRequest!
}
Run Code Online (Sandbox Code Playgroud)
我收到有关 a__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.'
Run Code Online (Sandbox Code Playgroud)
我在 github issues 和 SO questions 中搜索了几天,希望解决这个错误。
在我的解析器中实现__resolveType或__isTypeOf不起作用:
export const Query = {
getBankAccounts: (parent, args, context: IContext, info) => {
return {
__isTypeOf(obj) { // OR __resolveType, none of them work
return `GetAccountsResponse`;
},
accounts: []
};
},
};
Run Code Online (Sandbox Code Playgroud)
由于在我的解析器中实现__resolveTypeor__isTypeOf对我不起作用,我最终通过将__typename直接添加到解析器返回对象中来解决这个问题。
在getBankAccounts解析器实现中:
async getBankAccounts(): GetBankAccountsResponseOrUserInputRequest {
if (shouldGetUserInputRequest()) {
return {
__typename: "UserInputRequest",
...response,
};
}
return {
__typename: "GetAccountsResponse",
accounts,
};
}
Run Code Online (Sandbox Code Playgroud)
希望这对某人有帮助。
| 归档时间: |
|
| 查看次数: |
6468 次 |
| 最近记录: |