cmeeren / FSharp.HotChocolate

Support for F# types and nullability in HotChocolate
MIT License
7 stars 1 forks source link

Support nullability for directive arguments #21

Closed cmeeren closed 1 week ago

cmeeren commented 1 week ago

The following code defines a directive with a nullable (option-wrapped) argument MyValue:

open System
open HotChocolate.Types

type MyDirective = { MyValue: string option }

type MyDirectiveDescriptor() =
    inherit DirectiveType<MyDirective>()

    override this.Configure(descriptor: IDirectiveTypeDescriptor<MyDirective>) : unit =
        descriptor.Location(DirectiveLocation.FieldDefinition) |> ignore

[<AttributeUsage(AttributeTargets.Property ||| AttributeTargets.Method)>]
type MyDirectiveAttribute(value: string) =
    inherit ObjectFieldDescriptorAttribute()

    override this.OnConfigure(_context, descriptor, _member) =
        descriptor.Directive({ MyValue = Some value }) |> ignore

Usage:

type MyType = {
    [<MyDirective("myValue")>]
    MyField: int
}

When enabling the new F# 9 nullable reference types, the myValue directive argument becomes String! instead of String.

Furthermore, using GraphQLType as shown below will make it a nullable String again:

[<GraphQLType(typeof<StringType>)>]
MyValue: string option

The two points above indicate that FSharp.HotChocolate is currently not applying nullability to directive arguments.