Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScopedDataContext is lost when querying nodes for global object identification #7672

Open
ChauThan opened this issue Nov 1, 2024 · 0 comments
Assignees
Milestone

Comments

@ChauThan
Copy link

ChauThan commented Nov 1, 2024

Product

Hot Chocolate

Version

14.0.0

Link to minimal reproduction

https://github.com/ChauThan/bug-report/tree/main/hotchocolate/globalobjectidentification

Steps to reproduce

  • Run reproduction.
  • Execute query node to get AdditionalData
{
  node(id: "QXV0aG9yOjE=") {
    id
    ... on Author {
      AdditionalData
    }
  }
}

Data returns correctly.

{
  "data": {
    "node": {
      "id": "QXV0aG9yOjE=",
      "AdditionalData": "This is the additional data of Author 1"
    }
  }
}
  • Execute query nodes with same id to get AdditionalData
query {
  nodes(ids: ["QXV0aG9yOjE="]) {
    id
    ... on Author {
      AdditionalData
      id
      name
    }
  }
}
{
  "data": {
    "nodes": [
      {
        "id": "QXV0aG9yOjE=",
        "AdditionalData": "Data not found.",
        "name": "Author 1"
      }
    ]
  }
}

What is expected?

Data of nodes returns same as node

What is actually happening?

I am using ScopeContextData to resolve this field..

public class AuthorType : ObjectType<Author>
{
    protected override void Configure(IObjectTypeDescriptor<Author> descriptor)
    {
        descriptor.ImplementsNode()
            .IdField(s => s.Id)
            .ResolveNode((ctx, id) =>
            {
                var author = ctx.Services.GetRequiredService<AuthorRepository>().GetAuthorById(id);
                ctx.SetScopedState("AdditionalData", "This is the additional data of " + author?.Name);
                return Task.FromResult(author);
            });
        
        descriptor.Field("AdditionalData")
            .Type<StringType>()
            .Resolve<string>(ctx => ctx.ScopedContextData.TryGetValue("AdditionalData", out var scopedContextData) 
                ? scopedContextData.ToString() 
                : "Data not found.");
    }
}

It appears all ScopedContextData is cleared in nodes

Relevant log output

No response

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants