Skip to content

Add [GraphQlIgnoreInput] and [GraphQlIgnoreOutput] #8769

@wietlol

Description

@wietlol

Product

Hot Chocolate

Is your feature request related to a problem?

In some models, we have a property for example public DateTimeOffset CreationDateTime { get; set; }
In many cases, the models are reused as both input and output.
While these properties should be in the output type in GraphQl, they should not be in the input type.

The solution you'd like

I wrote attributes that would apply [GraphQlIgnore] on input and output separately, allowing the developer to specify that it should only be ignored (in this case) in the input.

I would like these to be part of the HotChocolate libraries, to allow these to be included in tests for future versions, to relieve me from maintaining these attributes, and to allow the other devs to use these when they need them.

public class GraphQlInputIgnoreAttribute : InputFieldDescriptorAttribute
{
    protected override void OnConfigure(IDescriptorContext context, IInputFieldDescriptor descriptor, MemberInfo member)
    {
        descriptor.Ignore();
    }
}

public class GraphQlOutputIgnoreAttribute : ObjectFieldDescriptorAttribute
{
    protected override void OnConfigure(IDescriptorContext context, IObjectFieldDescriptor descriptor, MemberInfo member)
    {
        descriptor.Ignore();
    }
}

This allows the models to have these fields for example:

[GraphQlInputIgnore]
public int Version { get; set; }
[GraphQlInputIgnore]
public DateTimeOffset CreationDateTime { get; set; }
[GraphQlInputIgnore]
public DateTimeOffset LastModifiedDateTime { get; set; }
[GraphQlInputIgnore]
public string LastModifiedBy { get; set; }

In addition, I also made attributes for input/output specific [GraphQlType<T>] allowing the dev to easily specify which types should be used for input and output.
This is especially useful if a model is used for both input and output, but some properties need to be of differently configured types for input and output beyond ignored properties. (For example useful for the current implementation of one-of for a polymorphic model.)

public class GraphQlInputTypeAttribute(
    Type type
) : InputFieldDescriptorAttribute
{
    protected override void OnConfigure(IDescriptorContext context, IInputFieldDescriptor descriptor, MemberInfo member)
    {
        descriptor.Type(type);
    }
}

public class GraphQlOutputTypeAttribute(
    Type type
) : ObjectFieldDescriptorAttribute
{
    protected override void OnConfigure(IDescriptorContext context, IObjectFieldDescriptor descriptor, MemberInfo member)
    {
        descriptor.Type(type);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions