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

Dynamic schemas can't be used #7954

Open
ep-workai opened this issue Jan 25, 2025 · 0 comments
Open

Dynamic schemas can't be used #7954

ep-workai opened this issue Jan 25, 2025 · 0 comments

Comments

@ep-workai
Copy link

ep-workai commented Jan 25, 2025

Product

Hot Chocolate

Version

15

Link to minimal reproduction

https://limewire.com/d/60631da7-56df-467f-a526-b5cd9fb1f5aa#KiK53SBHFrFdyZmGdEBVb5X6yt1MQNTBEgKEIKksCD0

Steps to reproduce

Hi,

I tried to use dynamic schemas, but seems there is no chance to support fully dynamic entites? I've added type module with dynamic objects and it appears correctly in schema, but I cannot query against this type, since it's not in query type. Is it possible to add this kind of dynamic entity to any of queries? I don't understand the point of dynamic schemas if they cannot be inside a query - because the type is dynamic and can't appear in query class.

After startup I receive
1. The object typeQuery has to at least define one field in order to be valid. (HotChocolate.Types.ObjectType)

I've tried to add
AddQueryType<UserQuery>() but still it works only for strongly typed. I need to support dynamic types in my query.

Image

https://chillicream.com/docs/hotchocolate/v15/defining-a-schema/dynamic-schemas

Please write a request if a link with example it's expired.

builder.AddGraphQL()
    .AddQueryType()
    .AddTypeModule<CustomTypeModule>()
    .AddFiltering();

public class CustomTypeModule : ITypeModule
{
    public event EventHandler<EventArgs>? TypesChanged;

    public async ValueTask<IReadOnlyCollection<ITypeSystemMember>> CreateTypesAsync(
        IDescriptorContext context,
        CancellationToken cancellationToken)
    {
        // using var db = context.Services.GetRequiredService<UsersDbContext>();
        //var fields = await db.CustomFields.ToListAsync(cancellationToken);
        var fields = new[]
        {
            new CustomField()
            {
                Id = 1,
                Name = "field1",
                Type = CustomFieldType.String,
            },
            new CustomField()
            {
                Id = 1,
                Name = "field2",
                Type = CustomFieldType.String,
            }
        };

        var types = new List<ITypeSystemMember>();

        var typeDefinition = new ObjectTypeDefinition(
            "DynamicUser",
            runtimeType: typeof(Dictionary<string, object>));

        foreach (var field in fields)
        {
            typeDefinition.Fields.Add(
                new ObjectFieldDefinition(
                    field.Name,
                    type: TypeReference.Parse($"{field.Type.ToString()}!"),
                    pureResolver: x => x.Parent<Dictionary<string, object>>()[field.Name]));
        }

        types.Add(ObjectType.CreateUnsafe(typeDefinition));

        return types;
    }
}

Schema:

schema {
  query: UserQuery
}

...

type DynamicUser {
  field1: String1
  field2: String1
}

What is expected?

Dynamic schema works correctly, so I can query my dynamic objects

What is actually happening?

There is an error about empty query

Relevant log output

### Additional context

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      HotChocolate.SchemaException: For more details look at the `Errors` property.

      1. The object type `Query` has to at least define one field in order to be valid. (HotChocolate.Types.ObjectType)

         at HotChocolate.SchemaBuilder.Setup.CompleteSchema(SchemaBuilder builder, IDescriptorContext context, LazySchema lazySchema, TypeRegistry typeRegistry)
         at HotChocolate.SchemaBuilder.Setup.Create(SchemaBuilder builder, LazySchema lazySchema, IDescriptorContext context)
         at HotChocolate.SchemaBuilder.Create(IDescriptorContext context)
         at HotChocolate.SchemaBuilder.HotChocolate.ISchemaBuilder.Create(IDescriptorContext context)
         at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaAsync(ConfigurationContext context, RequestExecutorSetup setup, RequestExecutorOptions executorOptions, IServiceProvider schemaServices, TypeModuleChangeMonitor typeModuleChangeMonitor, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaServicesAsync(ConfigurationContext context, RequestExecutorSetup setup, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorNoLockAsync(String schemaName, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorAsync(String schemaName, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorProxy.GetRequestExecutorAsync(CancellationToken cancellationToken)
         at HotChocolate.AspNetCore.HttpPostMiddlewareBase.HandleRequestAsync(HttpContext context)
         at HotChocolate.AspNetCore.HttpPostMiddlewareBase.InvokeAsync(HttpContext context)
         at Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.<>c__DisplayClass23_0.<<UseCancellation>b__1>d.MoveNext()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

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

1 participant