type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

@ObjectType() with a double underscore in the `name` argument prevents field resolvers from executing

Open atomzone opened this issue 2 years ago • 6 comments

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

atomzone avatar Dec 15 '21 11:12 atomzone

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

atomzone avatar Dec 15 '21 14:12 atomzone

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?

MichalLytek avatar Dec 15 '21 15:12 MichalLytek

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

atomzone avatar Dec 15 '21 18:12 atomzone

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() 😉

MichalLytek avatar Dec 16 '21 08:12 MichalLytek

Thanks for providing information on this issue. Will you be moving this bug onto your queue?

atomzone avatar Dec 18 '21 22:12 atomzone

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 😉

MichalLytek avatar Dec 19 '21 09:12 MichalLytek

Closed by #1309 🔒

MichalLytek avatar Jan 06 '24 17:01 MichalLytek