type-graphql
type-graphql copied to clipboard
@ObjectType() with a double underscore in the `name` argument prevents field resolvers from executing
Describe the Bug
When the name argument string contains a double underscore, field resolvers for this objectType fail to execute @ObjectType('product__tag')
To Reproduce
@ObjectType('product__tag')
class ProductTagType {
@Field()
public id: string;
}
@Resolver(() => ProductTagType)
class ProductTagResolver
// this does not execute
@FieldResolver(() => String)
public name(): string {
return 'hello';
}
}
{
"message": "Cannot return null for non-nullable field product__tag.name.",
}
Expected Behavior name strings with double underscore should not prevent field resolvers from executing correctly
Environment (please complete the following information):
- OS: MacOS
- Node v12.13.1
- Package version 1.1.1
- TypeScript version 4.2.4
Hi @MichalLytek
Some additional observations could render this possibly NOT a bug
Previously i was building the schema with
const { typeDefs, resolvers } = await buildTypeDefsAndResolvers({
resolvers: [
ProductTagResolver,
],
validate: schemaGeneratorValidator,
});
return makeExecutableSchema({
resolvers,
typeDefs: [typeDefs]
});
If i remove the makeExecutableSchema
from @graphql-tools/schema
The double underscore ObjectTypes resolve correctly
return buildSchemaSync({
resolvers: [
ProductSkuResolver,
],
validate: schemaGeneratorValidator,
});
The reason to use makeExecutableSchema
is to allow types from additional sources to be included in the rendered schema
const schema: GraphQLSchema = makeExecutableSchema({
resolvers,
typeDefs: [typeDefs, externalSdl],
});
Thanks for attention to this
So it looks like the resolvers
for buildTypeDefsAndResolvers
are missing proper resolver function for fields with double underscore name?
Maybe you can create a fresh super-simple reproduction repository or a failing test case for this repository?
Correct
For @ObjectType('product__tag')
buildTypeDefsAndResolvers
gives us
{
String: String,
}
For @ObjectType('product_tag')
buildTypeDefsAndResolvers
gives us
{
product_tag: { name: [Function] },
String: String,
}
As you speculated. Would appear the builder is missing a resolver for fields with double underscore name
It's becausue of that line: https://github.com/MichalLytek/type-graphql/blob/23409fd16fe85ddb2785d06712bdf6f996b89652/src/utils/createResolversMap.ts#L18
It should check for startsWith()
not includes()
😉
Thanks for providing information on this issue. Will you be moving this bug onto your queue?
It is fairly easy to fix and really rare to occur for other users, so if you want a quick fix, go ahead and create a PR with that fix.
Remember to first add a test case to existing suite which reproduce the issue and then apply the fix in the codebase to see if it really fixes the mentioned issue 😉
Closed by #1309 🔒