Every field on a GraphQL object type can have zero or more arguments, for example the length
field below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Starship { | |
id: ID! | |
name: String! | |
length(unit: LengthUnit = METER): Float | |
} |
An argument can be either required or optional. For optional arguments, you can specify a default value, like METER
in the example above.
To configure this using HotChocolate, you can use the following syntax:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StarshipType | |
: ObjectType<Starship> | |
{ | |
protected override void Configure(IObjectTypeDescriptor<Starship> descriptor) | |
{ | |
descriptor.Field(t => t.Id) | |
.Type<NonNullType<IdType>>(); | |
descriptor.Field<SharedResolvers>(t => t.GetLength(default, default)) | |
.Argument("unit",a => {a.Type<LengthUnit>(); a.DefaultValue(Unit.Meter)}); | |
} | |
} |