graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

ScopeContextData is discarded in NodeResolvers

Open dawust opened this issue 3 years ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Product

Hot Chocolate

Describe the bug

When data is set in ScopedContextData within a NodeResolver, it appears that the context is simply discarded and becomes unavailable for subsequent operations. This problem does not occur with a regular Resolver.

Steps to reproduce

Here is a simple Repro

public class Query
{
    [NodeResolver]
    public Product GetProductById(int id, IResolverContext resolverContext)
    {
        resolverContext.SetScopedState("foo", id);
        return new Product(id);
    }

    public IEnumerable<Product> GetProducts(IEnumerable<int> ids, IResolverContext resolverContext)
    {
        return ids.Select(id => GetProductById(id, resolverContext));
    }
}

public record Product(int Id)
{
    public int? GetIntId(IResolverContext context) => context.GetScopedStateOrDefault<int?>("foo");
}

If we run the following Query it becomes obvious that the context seems to be discarded Query:

query {
  nodes(ids: ["UHJvZHVjdAppMTIz", "UHJvZHVjdAppNDU2"]) {
    ... on Product {
      intId
    }
  }
  products(ids: [123, 456]) {
    ... on Product {
      intId
    }
  }
}

Result:

{
  "data": {
    "nodes": [
      {
        "intId": null
      },
      {
        "intId": null
      }
    ],
    "products": [
      {
        "intId": 123
      },
      {
        "intId": 456
      }
    ]
  }
}

Thank you!

Relevant log output

No response

Additional Context?

When using "node" instead of "nodes" it works fine, so I guess the cloned Context in ResolveManyNodeAsync has to preserved somehow.

Version

13.5.1 and 13.6.0-preview.23

dawust avatar Oct 04 '23 06:10 dawust

Thank you for this great repro ... I already created a test from this and we will fix it with 13.6

michaelstaib avatar Oct 04 '23 11:10 michaelstaib

I spoke to soon ... I just saw that you are using the nodes field. In this case we actually have some contention. While we use here the single resolver we are actually using multiple resolvers in parallel. But we actually just have one context.

The way this works is that we execute the nodes resolver and after that is executed the value returned from the nodes resolver is passed into the value completion. This value is a lost of all the results that we collected from the face resolvers.

The list now is completed as described by the spec and for each field of the object in the case you described will spawn a new resolver task which get the scopedContext from its parent context which is the context from the single executed nodes field.

In short this is not possible.

We have another case like this which is a bit different but similar where you produce list result in a field and now want to pass down some state into the context for each of the objects produced.

For now I will close this issue as will not be fixed.

michaelstaib avatar Oct 04 '23 11:10 michaelstaib