ChilliCream / ChilliCream/graphql-platform

ServiceKind.Resolver/Synchronized does not work with Schema First

Open
#6,732 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🌶️ hot chocolate 🔍 investigate Area: Type System
Dominant language
C#
Stars
5.8k
Forks
810
Avg merge
15h 39m
Merged PRs (30d)
98

Description

Is there an existing issue for this?
  • I have searched the existing issues
Product

Hot Chocolate

Describe the bug

There is a bug with the implementation of ServiceKind.Resolver and ServiceKind.Synchronized when combined with schema first registration (AddDocumentFromString for example).

When using ServiceKind.Resolver with schema first, an exception is thrown at runtime:

The specified key MediatR.IMediator does not exist on context.ScopedContextData.
     at HotChocolate.Resolvers.Expressions.ExpressionHelper.GetScopedStateWithDefault[TContextData](IPureResolverContext context, IReadOnlyDictionary`2 contextData, String key, Boolean hasDefaultValue, TContextData defaultValue)\n   at lambda_method6(Closure , IResolverContext )\n   at HotChocolate.Types.Helpers.FieldMiddlewareCompiler.<>c__DisplayClass9_0.<<CreateResolverMiddleware>b__0>d.MoveNext()\n--- End of stack trace from previous location ---\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)

When using ServiceKind.Synchronized with schema first, an exception is not thrown at runtime, but queries that are dependent on the registered service are not executed in a synchronized manner.

Steps to reproduce

Demo.zip

Attached is a demo project that reproduces the issue. Essentially the bug is when you combine a schema first registration method such as AddDocumentFromString with RegisterService<T>(ServiceKind.Resolver/Synchronized).

Reproduce ServiceKind.Resolver Exception

  1. Run the app using dotnet run
  2. Navigate to BCP using http://127.0.0.1:5052/graphql/
  3. Query the app using
  query {
    greeting
  }
  1. The following exception is returned
The specified key MediatR.IMediator does not exist on context.ScopedContextData.
   at HotChocolate.Resolvers.Expressions.ExpressionHelper.GetScopedStateWithDefault[TContextData](IPureResolverContext context, IReadOnlyDictionary`2 contextData, String key, Boolean hasDefaultValue, TContextData defaultValue)\n   at lambda_method6(Closure , IResolverContext )\n   at HotChocolate.Types.Helpers.FieldMiddlewareCompiler.<>c__DisplayClass9_0.<<CreateResolverMiddleware>b__0>d.MoveNext()\n--- End of stack trace from previous location ---\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)

Reproduce ServiceKind.Synchronized Behavior

ServiceKind.Synchronized will not throw a runtime exception. However the behavior that is stated in the documentation of synchronous/serialized execution is not observed when the ServiceKind is combined with schema first registration methods.

Steps to reproduce:

  1. Update line 9 in Program.cs to .RegisterService<IMediator>(ServiceKind.Synchronized)
  2. Run the app using dotnet run
  3. Navigate to BCP using http://127.0.0.1:5052/graphql/
  4. Query the app using
  query {
    greeting
    goodbye
  }
  1. Observe the console output and notice that the 2 queries are not called in a serialized manner
    Example: output:
Begin 889fe980-591d-4dd1-a4a7-a1c8a842c4da
Begin 56ab3a14-d519-4090-8c43-1ffa0f81c65c
End 56ab3a14-d519-4090-8c43-1ffa0f81c65c
End 889fe980-591d-4dd1-a4a7-a1c8a842c4da
Relevant log output

No response

Additional Context?

No response

Version

13.7.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the attached Demo.zip and Program.cs, focusing on AddDocumentFromString and RegisterService with ServiceKind.Resolver and ServiceKind.Synchronized. Run the app and reproduce both the missing ScopedContextData exception and the non-serialized greeting/goodbye execution. Done means schema-first registration resolves the service and synchronized fields execute serially.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, graphql
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.