Today I want to show you how you can use discriminated unions for input types in GraphQL through the OneOf specification.
Disclaimer: The OneOf specification is still in draft, so it is possible that it will still change or not become a part of the GraphQL specification.
Let me first start by explaining the use case where I want to apply this features. In our GraphQL api , we have multiple root queries that can be queried either through the technical id(an integer) and the business key(a string). Our original API looked something like this:
A consumer of this API should either use the id or the business key but I could not prevent the user from using both:
To handle this situation I throw an error when the user tries to invoke the GraphQL query providing both arguments:
using HotChocolate; | |
using HotChocolate.Types; | |
using HotChocolate.Types.Relay; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace IRD3.API.GraphQL { | |
[ExtendObjectType("Query")] | |
public class ExploitantQueries { | |
public Task<Exploitant> GetExploitant(int? idIe, string exploitantNummer, [Service]IIRD3Service irdService) | |
{ | |
if (idIe.HasValue & !string.IsNullOrEmpty(exploitantNummer)) | |
throw new InvalidOperationException("You should provide either an idIe or exploitantNummer not both."); | |
if (idIe.HasValue) | |
return irdService.GetExploitantByIdIe(idIe.Value); | |
if (!string.IsNullOrEmpty(exploitantNummer)) | |
return irdService.GetExploitantByNr(exploitantNummer); | |
throw new InvalidOperationException("Either idIe or exploitantNummer should be provided."); | |
} | |
} | |
} |
This is a perfect case that can be improved through the usage of OneOf. By using OneOf on an input object we tell GraphQL that only one field of the input object can be set.
In a schema first approach, we add the OneOf directive to our input object:
input GetExploitantInput @oneof { | |
idIe: Int | |
exploitantNummer: String | |
} |
Or we use the code first approach in HotChocolate to achieve the same result:
public class GetExploitantInput | |
{ | |
public int? IdIe { get; set; } | |
public string ExploitantNummer { get; set; } | |
} |
public class GetExploitantInputType : InputObjectType<GetExploitantInput> | |
{ | |
protected override void Configure(IInputObjectTypeDescriptor<GetExploitantInput> descriptor) | |
{ | |
descriptor.OneOf(); | |
} | |
} |
Now of course we need to update our implementation to use this input object:
using HotChocolate; | |
using HotChocolate.Types; | |
using HotChocolate.Types.Relay; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace IRD3.API.GraphQL; | |
[ExtendObjectType("Query")] | |
public class ExploitantQueries | |
{ | |
public Task<Exploitant> GetExploitant(GetExploitantInput by, [Service] IIRD3Service irdService) | |
{ | |
if (by.IdIe.HasValue) | |
return irdService.GetExploitantByIdIe(by.IdIe.Value); | |
if (!string.IsNullOrEmpty(by.ExploitantNummer)) | |
return irdService.GetExploitantByNr(by.ExploitantNummer); | |
throw new InvalidOperationException("Either idIe or exploitantNummer should be provided."); | |
} | |
} | |
If we now try to pass multiple arguments, GraphQL will prevent this:
Nice!