Graphql: Graphql UnionType

Created on 4 Apr 2019  路  7Comments  路  Source: nestjs/graphql

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 :smiley:

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

done

Most helpful comment

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/[email protected]). Please, let me know if you face any issue.

All 7 comments

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."

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/[email protected]). Please, let me know if you face any issue.

Published as 6.2.0 ($ npm i @nestjs/graphql@latest). You will also have to update @nestjs/core to 6.2.0 ($ npm i @nestjs/core@latest)

@kamilmysliwiec

TypeError: Cannot read property 'REQUEST' of undefined
    at Object.<anonymous> (/Users/anhnvt/my-working-space/fcl/namek/beerus/node_modules/@nestjs/core/router/request/request-providers.js:8:27)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/anhnvt/my-working-space/fcl/namek/beerus/node_modules/@nestjs/core/injector/internal-core-module.js:12:29)
    at Module._compile (internal/modules/cjs/loader.js:689:30)

When i try installed latest version of nestjs/core and nestjs/graphql. i run dev server and i face this problem. Any solution?

@nguyenviettuananh it seems that you didn't update all packages

@kamilmysliwiec
i thought you mention @nestjs/graphql@latest and @nestjs/core@latest only.
So pls suggeest me which packages should i need to upgrade ?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings