MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
https://typegraphql.com
MIT License
7.98k stars 674 forks source link

Bug when the field and the field resolver has the same name #1699

Closed mjy9088 closed 2 weeks ago

mjy9088 commented 1 month ago

Describe the Bug

Say we have GraphQL ObjectType Foo with field @Field((type) => Boolean) bar: boolean, and its field resolver @FieldResolver((returns) => Boolean, { nullable: true }) bar(): boolean | null { return null; }.

Then, resulting schema is type Foo { bar: Boolean! }, but querying Foo::bar produces an error: Cannot return null for non-nullable field Foo.bar.

To Reproduce

https://github.com/my-trash-bin/240531.git

Expected Behavior

Logs

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Foo.bar.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "foo",
        "bar"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "Error: Cannot return null for non-nullable field Foo.bar.",
          "    at completeValue (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:605:13)",
          "    at executeField (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:500:19)",
          "    at executeFields (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:414:22)",
          "    at completeObjectValue (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:925:10)",
          "    at completeValue (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:646:12)",
          "    at completeValue (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:595:23)",
          "    at executeField (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:500:19)",
          "    at executeFields (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:414:22)",
          "    at executeOperation (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:344:14)",
          "    at execute (/Users/user/workspace/my-trash-bin/240531/node_modules/graphql/execution/execute.js:136:20)"
        ]
      }
    }
  ]
}

Environment (please complete the following information):

MichalLytek commented 1 month ago

Why do you want to declare the object type field to be non nullable and then overwrite it to make it nullable?

The internals work in a way that the field resolver info is ignored if the field is defined in object type class. Only if there's no such field declared, it will use the field resolver info to register a new field.

mjy9088 commented 2 weeks ago

I'm just now seeing this because it's buried in so many notifications, sorry.

I found this bug by mistake, and while it's not a fix I need, I'm just reporting it because I found it.

If it's not a bug and that's how it's supposed to work, I guess it doesn't matter! Thanks :)

MichalLytek commented 2 weeks ago

I guess I should add this to the list of errors in #12 - to tell user that you can't overwrite the type of field via field resolver. Thanks for the report!