ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.07k stars 723 forks source link

Adding an attribute to prevent type from being added #6990

Closed anli-xsigns closed 3 months ago

anli-xsigns commented 3 months ago

Product

Hot Chocolate

Is your feature request related to a problem?

Please see the source code at https://github.com/anli-xsigns/hc-graphql.

HotChocolate.SchemaException: For more details look at the `Errors` property.

1. Interface `IDomainEvent` has no fields declared. (HotChocolate.Types.InterfaceType<Domain.IDomainEvent>)

   at HotChocolate.Configuration.TypeInitializer.EnsureNoErrors()
   at HotChocolate.Configuration.TypeInitializer.CompleteTypes()
   at HotChocolate.Configuration.TypeInitializer.Initialize()
   at HotChocolate.SchemaBuilder.Setup.InitializeTypes(SchemaBuilder builder, IDescriptorContext context, IReadOnlyList`1 types)
   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__DisplayClass20_0.<<UseCancellation>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

The solution you'd like

I would like to prevent HotChocolate from adding the IDomainEvent and even GetDomainEvents() to the schema with an attribute, e. g. [GraphQLIgnore]

michaelstaib commented 3 months ago

In general if doing DDD the entity types should not be exposed to the GraphQL type system. What we tend to do is throwing CQRS in and have a dedicated query layer that has access to an optimized read model. Only commands have access to the domain entities itself.

But you can also solve you problem ... if you inherit from the DefaultTypeInspector and override the IsMemberIgnored method you can check if your ignore condition is met and otherwise refer to the base.

public override bool IsMemberIgnored(MemberInfo member)
{
    if (member is null)
    {
        throw new ArgumentNullException(nameof(member));
    }

    if(member.Name == "GetDomainEvents")
    {
        return true;
    }

    return base.IsMemberIgnored(member);
}

Register you custom instance of the convention like this:

services.AddGraphQLServer().TryAddConvention<ITypeInspector, MyTypeInspector>();

Help our project by staring us on GitHub. Its the easiest way to give back to this project.