Can I make connections NonNullable in GraphQL .NET?

122 views Asked by At

I'm working on a project, where we need to make non optional connections, and I can't really find anything on the subject. I made the fields work with the NonNullableGraphType as shown beneath, but I have no luck getting the connections to be non nullable. I've searched far and wide, and can't find anything about the issue, so I hope someone can help me here, as I'm completely lost. The fields that are non nullable are written as such:

        Field<NonNullGraphType<OrderPresetGraphType>>(
            "preset",
            resolve: context => {
                var loader = dataLoader.Context.GetOrAddBatchLoader<int, Base.Entities.Orders.OrderPreset>(
                    "OrderPresetById", orderPresetController.GetOrderPresetsByIdAsync
                );

                return loader.LoadAsync(context.Source.PresetId);
            }
        );

Sadly, the same method doesn't work with lists. Any help is much appreciated!

Edit My current attempt at solving this, looks like this:

        Connection<NonNullGraphType<AssetFilterPresetFieldGraphType>>()
            .Name("fields")
            .Unidirectional()
            .Description("Returns a list of AssetFilterPresetFieldGraphType connected to this AsetFilterPresetGraphType")
            .ResolveAsync(async context =>
            {
                var result = await assetFilterPresetController.GetFilterPresetFieldsByIdAsync(context.Source.Id)
                    .ConfigureAwait(false);

                return ConnectionResolver.ToConnection(result.Data, context);
            });
1

There are 1 answers

0
fest On BEST ANSWER

I stumbled into this problem as well. I solved by wrapping the current type with NonNullableGraphType using an extension method.

public static ConnectionBuilder<TSource> NonNullable<TSource>(
    this ConnectionBuilder<TSource> builder
)
{
    builder.FieldType.Type = typeof(NonNullGraphType<>).MakeGenericType(builder.FieldType.Type);
    return builder;
}

Use it like this:

Connection<UserGraphType>()
    .Name("users")
    .NonNullable()
    .Resolve(ctx => /*...*/);