Type-graphql: GraphQL error: Cannot read property 'X' of null

Created on 9 May 2020  路  3Comments  路  Source: MichalLytek/type-graphql

I'm not completely sure if this is a bug is just me doing something wrong.

I have a InputType field of the same type it belongs, like this

@InputType()
export class String_type {
  @Field({ nullable: true })
  @IsNotEmpty()
  _eq: string;

  @Field({ nullable: true })
  @IsNotEmpty()
  _ilike: string;
}

@InputType()
class my_types {  
  @Field({ nullable: true })
  @ValidateNested()
  field_A: String_type;

  @Field({ nullable: true })
  @ValidateNested()
  field_B: String_type;

  @Field((_type) => [my_types], {
    nullable: 'itemsAndList',
  })
  @ValidateNested()
  _nested?: my_types[];
}

The idea behind this is that I can call my query with an array of the same fields, and I can add them recursively

So if I was calling this, it would be something like this

query myQuery {
  entity (_nested: [{field_A: {_eq: "some_value"}}, {field_B: {_eq: "another_value"}] }) {
    ...
  }
}

Then on the client I can create the query dynamically and end up with something like this

query myQuery($nested: my_types) {
  entity (_nested: [{field_A: {_eq: "some_value"}}, $nested] }) {
    ...
  }
}

and the problem with this is that $nested could be null, so I get this error

"graphQLErrors": [
  {
    "message": "Cannot read property 'field_A' of null",
    "locations": [
      {
        "line": 2,
        "column": 3
      }
    ],
    "path": [
      "users"
    ],
    "extensions": {
      "code": "INTERNAL_SERVER_ERROR",
      "exception": {
        "stacktrace": [
          "TypeError: Cannot read property 'field_A' of null",
          "    at .../node_modules/type-graphql/dist/resolvers/convert-args.js:54:27",
          "    at Array.reduce (<anonymous>)",
          "    at convertToInput (.../node_modules/type-graphql/dist/resolvers/convert-args.js:52:42)",
          "    at .../node_modules/type-graphql/dist/resolvers/convert-args.js:61:61",
          "    at Array.map (<anonymous>)",
          "    at .../node_modules/type-graphql/dist/resolvers/convert-args.js:61:44",
          "    at Array.reduce (<anonymous>)",
          "    at convertToInput (.../node_modules/type-graphql/dist/resolvers/convert-args.js:52:42)",
          "    at convertValueToInstance (.../node_modules/type-graphql/dist/resolvers/convert-args.js:74:11)",
          "    at convertValuesToInstances (.../node_modules/type-graphql/dist/resolvers/convert-args.js:85:12)"
        ]
      }
    }
  }
],
"networkError": null,
"message": "GraphQL error: Cannot read property 'field_A' of null"
}

I can set the $nested with a default value of of an empty {} but I was wondering if there is a way I can prevent this from the server, and just ignore the null values

Bug Community Solved

All 3 comments

Looks like the convert-args is not aware of the 'itemsAndList' option.
I will try to investigate and fix that soon 馃挭
Thanks for the report!

Fixed by 6de36d5 馃敀

thanks @MichalLytek !

Was this page helpful?
0 / 5 - 0 ratings