Skip to content

Parent requires don't work #9082

@liyanjie2048

Description

@liyanjie2048

Product

Hot Chocolate

Version

15.1.12

Link to minimal reproduction

https://github.com/liyanjie2048/Study.Hotchocolate

Steps to reproduce

do this query:

{
  users {
    nameLength
    emailDomain
    addressDetail
    stateDescription
  }
}

What is expected?

select User.Name, User.Email, User.Address, User.State in SQL command, and return what i queried

What is actually happening?

only select User.Id (isProjected) in SQL command, and the graphql query result is error:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "users"
      ],
      "extensions": {
        "code": "HC0018"
      }
    },
    {
      "message": "Unexpected Execution Error",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "users",
        0,
        "nameLength"
      ]
    }
  ],
  "data": null
}

Relevant log output

Additional context

code:

public class User
{
    public Guid Id { get; set; }
    public required string Name { get; set; }
    public required string Email { get; set; }
    public required Address Address { get; set; }
    public required State State { get; set; }
    public virtual ICollection<Post> Posts { get; set; } = [];

    public int NameLength => Name.Length;
    public string EmailDomain => Email.Split('@').Last();
    public string AddressDetail => $"Test: {Address.AdCode} {Address.Detail}";
}
[ExtendObjectType<User>]
public class UserExtension
{
    public string StateDescription(
        [Parent(requires: nameof(User.State))] User user)
    {
        return $"{user.State.Value}-{user.State.Remark}";
    }
}
public class UserType : ObjectType<User>
{
    protected override void Configure(IObjectTypeDescriptor<User> descriptor)
    {
        descriptor.Field(_ => _.Id).IsProjected();
        descriptor.Field(_ => _.EmailDomain).ParentRequires<User>(_ => _.Email);
        descriptor.Field(_ => _.NameLength).ParentRequires<User>(_ => _.Name);
        descriptor.Field(_ => _.AddressDetail).ParentRequires<User>(_ => _.Address);
    }
}
[QueryType]
public class Query
{
    [UseProjection]
    [UseFiltering]
    [UseSorting]
    public IQueryable<User> Users(
        [Service] DataContext context)
        => context.Set<User>().AsQueryable();
}

is my code wrong?

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions