question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Graphql UnionType

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x?] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I try to integrate a UnionType with Graphql, but unfortunately without success.

I get the error message: Either the SearchResult type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function."

Expected behavior

A working UnionType with Graphql 😃

Minimal reproduction of the problem with instructions

I have already tried to solve the problem with a ResolveProperty named resolveType inside of a resolver. But the function is not executed.

When I name the function __resolveType I get the error message Name "__resolveType" must not begin with "__", which is reserved by GraphQL introspection.

the createUnionType function:

export const SearchResultUnion = createUnionType({
    name: "SearchResult",  // the name of the GraphQL union
    types: [CPublicStore,CArticle, CService], // array of object types classes
});

My Search-ObjectType:

@ObjectType()
export class CSearch implements ISearch {
    @Field(type => [SearchResultUnion]) source: (CArticle | CService | CPublicStore)[]
    ...
}

and the resolver which should resolve the source property:

@Resolver(of => CSearch)
export class SearchTransform implements ResolverInterface<CSearch>{
...
    @ResolveProperty("__resolveType",()=>String)
    __resolveType(obj) {
       ....
    }
}

Environment


[System Information]
OS Version     : Linux 4.15
NodeJS Version : v10.15.1
YARN Version    : 1.12.3
[Nest Information]
platform-express version : 6.0.0
passport version         : 6.0.0
graphql version          : 6.0.2
swagger version          : 3.0.2
common version           : 6.0.0
core version             : 6.0.0

Thank you

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilmysliwieccommented, May 11, 2019

It should be fixed in the @next version ($ npm i @nestjs/graphql@next). You will also have to update @nestjs/core to 6.2.0 ($ npm i @nestjs/core@6.2.0). Please, let me know if you face any issue.

1reaction
renehauckcommented, Apr 10, 2019

Okay, I found the problem. Is it possible that ResolveProperty does not work with a Union type? I have the following implementation:

@Resolver(of => CSearch)
export class SearchTransform implements ResolverInterface<CSearch>{
   ...
    @ResolveProperty("source",()=>[SearchResultUnion])
    async source(@Parent() recipe: CSearch) {
      ...
    }
}

but I get the following error: "message": "Abstract type SearchResult must resolve to an Object type at runtime for field CSearch.source with value [{}], received \"undefined\". Either the SearchResult type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function."

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unions and interfaces - Apollo GraphQL Docs
Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types. Union type.
Read more >
GraphQL Unions – Customize your results!
Let's understand unions: Unions are abstract schema types. To define a union type, we declare which object types are included in the union....
Read more >
Schemas and Types
Note that members of a union type need to be concrete object types; you can't create a union type out of interfaces or...
Read more >
Unions
Read more about the GraphQL Union Type in the official GraphQL docs. Usage. Let's start by creating the object types from the example...
Read more >
Class: GraphQL::UnionType
So, when you query, you must use fragment spreads to access fields. Examples: A union of object types. MediaUnion = GraphQL ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found