Computed Default
In the current version of the GraphQL schema spec, default input values must be constants or variable names; no expressions are supported. Thus this can't be handled in GraphQL, which leaves the host language and the DB backend, either of which can handle it easily.
Whichever is used, the field argument in GraphQL should be made nullable, with a default value of null
. Additionally, the ID is a synthetic key and should be left to the DBMS to generate, so should be left out of the query field.
scalar Time
type Mutation {
insertInA(a_time: Time = null)
}
Note that for more complex models, the argument should be an object input type, rather than a scalar.
The insertInA
resolver should check for null
and then either set it itself (getting the current time using DateTime.UtcNow
):
A insertInA(TimeOnly? a_time = null) {
a_time ??= TimeOnly.FromDateTime(DateTime.UtcNow);
A newA = new A { a_time = a_time };
// …
}
or leave it to the DBMS:
A insertInA(TimeOnly? a_time = null) {
A newA = new A { a_time = a_time };
// …
}
If not using some ORM, your code may need to choose among different SQL statements, depending on whether or not a_time
is null
.
Time type
Though GraphQL doesn't have native date/time types, custom scalars can easily be added (specifics are beyond the scope of this Q&A).