7

I tried to resolve one error in my study code, but failed. Then I just try to launch this code...

https://github.com/nestjs/nest/tree/master/sample/23-type-graphql

and the same situation...

Error looks like

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Recipe.id.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "recipe",
        "id"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Recipe.id.",
            "    at completeValue (/home/innistry/Downloads/nest-master/sample/23-type-graphql/node_modules/graphql/execution/execute.js:560:13)",
            "    at /home/innistry/Downloads/nest-master/sample/23-type-graphql/node_modules/graphql/execution/execute.js:492:16",
            "    at process._tickCallback (internal/process/next_tick.js:68:7)"
          ]
        }
      }
    }
  ],
  "data": null
}

Has someone ideas?

innistry
  • 285
  • 1
  • 3
  • 10
  • 1
    If you have the same issue and reached this page as I did, I added an answer on this page. https://stackoverflow.com/questions/56319137/why-does-a-graphql-query-return-null I cannot add an answer on this page because this question is marked as a duplicated question. – Atsuhiro Teshima Feb 01 '20 at 08:21

1 Answers1

9

This is the fastest fix, just to launch.

import { Field, ID, ObjectType } from 'type-graphql';

@ObjectType()
export class Recipe {
  @Field(type => ID, { nullable: true })
  id?: string;

  @Field({ nullable: true })
  title?: string;

  @Field({ nullable: true })
  description?: string;

  @Field({ nullable: true })
  creationDate?: Date;

  @Field(type => [String], { nullable: true })
  ingredients?: string[];
}

innistry
  • 285
  • 1
  • 3
  • 10
  • 2
    Not a good way to solve it. Making the field nullable should be done intentionally, not to bypass a bug. – A Mehmeto Oct 19 '21 at 23:24
  • 1
    I had the exact same error message. It happened because I used a partial fixture in my test where I didn't filled all the necessary field in my entity. One should be looking for that kind of misuse instead of this quick fix workaround. – A Mehmeto Oct 23 '21 at 00:29