#Always response Cannot return null for non-nullable field level2.level3 for nested resolver

1 messages · Page 1 of 1 (latest)

jagged spruce
#

I need to make a nested request in 3 levels:

  1. TodoItem
  2. Comment
  3. User(createdBy)

I use the "SDL First" approach, so I have a schema.graphql

type Query {
  todoItem(id: ID!): TodoItem
}

type TodoItem {
  id: ID!
  comments: [Comment!]!
}

type Comment {
  id: ID!
  todoItemID: ID!
  content: String!
  createdBy: User!
}

type User {
  uid: ID!
  username: String!
}

a file todo-item.resolver.ts

import { Resolver, Query, Args, ResolveField, Parent } from '@nestjs/graphql';
import { Comment, TodoItem, User } from 'src/graphql';

@Resolver('TodoItem')
export class TodoItemResolver {
  @Query()
  async todoItem(@Args('id') id: string): Promise<TodoItem> {
    return {
      id: 'todoItem1',
      comments: [],
    };
  }

  @ResolveField()
  async comments(@Parent() todoItem: TodoItem): Promise<Comment[]> {
    return [
      {
        id: 'comment1',
        todoItemID: 'todoItem1',
        content: 'i m comment1',
        createdBy: {} as User,
      },
    ];
  }
}

@Resolver('Comment')
export class CommentResolvers {
  @ResolveField()
  async createdBy(@Parent() comment: Comment): Promise<User> {
    return {
      uid: 'user1',
      username: 'user1',
    };
  }
}

and I query

query {
  todoItem(id: "1") {
    id
    comments {
      content
      createdBy {
        uid
        username
      }
    }
  }
}

I expect to get

{
  "data": {
    "todoItem": {
        "id": "todoItem1",
        "comments": [
            {
                "id": "comment1",
                "content": "i m comment",
                "createdByUID": "user1",
                "createdBy": {
                    "uid": "user1",
                    "username": "user`"
                }
            }
        ]
    }
  }
}

what am I doing wrong?

#

I always get an error.message

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Comment.createdBy.",
      "locations": [
        {
          "line": 10,
          "column": 7
        }
      ],
      "path": [
        "todoItems",
        0,
        "comments",
        0,
        "createdBy"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "Error: Cannot return null for non-nullable field Comment.createdBy.",
          ...
        ]
      }
    }
  ],
  "data": null
}

I've already referenced:

but it didn't work.

GitHub

I'm submitting a... [ ] Regression [ ] Bug report [x] Feature request [x] Documentation issue or request [ ] Support request => Please do not submit support request here, instead post your q...