[ ] Regression
[*] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Hello,
I'm using the graphql example( in the example directory of nest) with the Cat CRUD and i try to use a union type and interface but i didn't find a way to do it.
When i try to request my data with a fragment, i have the following error :
"Abstract type MutationResult must resolve to an Object type at runtime for field Mutation.createCat with value "[object Object]", received "undefined". Either the MutationResult type should provide a "resolveType" function or each possible types should provide an "isTypeOf" function."
There is nothing in the doc explaining how to use union / interface, and there is nothing in the graphql example.
In the apollo documentation, the type resolver ( here "Cat" Resolver") should implement a __resolveType function. I tried to set this function in the @resolver('Cat') class CatsResolvers
but it's not working.
I tried to add it on the cat resolvers class
Expected behavior
The request should return either a Cat item or GraphQLErrorItem from my schema definition.
Minimal reproduction of the problem with instructions
export interface GraphQLError {
readonly message: string;
readonly errorCode: number;
readonly type: string;
}
type GraphQLError {
message: String
errorCode: Int
type: String
}
union MutationResult = Cat | GraphQLError
- change the createCat Mutation in the schema
- createCat(name: String, age: Int): MutationResult
- add the function in cats.resolvers.ts in the CatsResolvers class
__resolveType(obj, context, info): string{
return obj.errorCode ? 'GraphQLError' : 'Cat';
}
What is the motivation / use case for changing the behavior?
Environment
Nest version: 4.5.10 (core)
For Tooling issues:
- Node version: 9.4
- Platform: Mac
Others:
I'm submitting a...
Current behavior
In the interface GqlModuleOptions
, the typeDefs
property is inherited from the apollo-server Config
interface, which defines it as typeDefs?: DocumentNode | Array<DocumentNode>;
However, when I pass a DocumentNode as the value of typeDefs
, I get the following error:
[Nest] 15752 - 2018-9-3 16:43:12 [ExceptionHandler] Syntax Error: Unexpected [ +102ms
Syntax Error: Unexpected [
GraphQL request (5:2)
4: }
5: ,[object Object]
^
6:
at syntaxError (C:\Development\vendure\vendure\server\node_modules\graphql\error\syntaxError.js:24:10)
at unexpected (C:\Development\vendure\vendure\server\node_modules\graphql\language\parser.js:1485:33)
at parseDefinition (C:\Development\vendure\vendure\server\node_modules\graphql\language\parser.js:160:9)
at parseDocument (C:\Development\vendure\vendure\server\node_modules\graphql\language\parser.js:115:22)
at parse (C:\Development\vendure\vendure\server\node_modules\graphql\language\parser.js:48:10)
at parseDocument (C:\Development\vendure\vendure\server\node_modules\graphql-tag\src\index.js:129:16)
at Object.gql (C:\Development\vendure\vendure\server\node_modules\graphql-tag\src\index.js:170:10)
at GraphQLFactory.mergeOptions (C:\Development\vendure\vendure\server\node_modules\@nestjs\graphql\dist\graphql.factory.js:32:55)
at Function.<anonymous> (C:\Development\vendure\vendure\server\node_modules\@nestjs\graphql\dist\graphql.module.js:73:55)
at Generator.next (<anonymous>)
Passing a string representation of the schema on the other hand works, but then I need to cast the string to any
to avoid type errors.
This line in the GraphQLFactory
class seems to be the point that the value is used as a string (or array of strings to be exact):
|
typeDefs: gql` |
|
${options.typeDefs} |
|
`, |
Expected behavior
Either the GraphQLFactory should perform a check to see if the typeDefs
is a DocumentNode, and if so then skip the gql
tag call.
Or just change the GqlModuleOptions
to make typeDefs a string type.
Environment
Nest version: 5.3.0, graphql v5.1.0