-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Final solution for the eternal null VS undefined problem #1416
Comments
Very glad to see your effort to deal with the problem of optional/nullable.
|
Here's an additional, slightly different but just as ugly solution, it uses GetFieldContext instead of GetRequestContext and gives back a list of "touched" fields within an object // GetArgumentFieldnames returns a list of fieldnames from an argument
func (r *Resolver) GetArgumentFieldnames(ctx context.Context, name string) []string {
fieldContext := graphql.GetFieldContext(ctx)
names := []string{}
for _, arg := range fieldContext.Field.Arguments {
if arg.Name != name {
continue
}
for _, child := range arg.Value.Children {
names = append(names, child.Name)
}
}
return names
} |
Is there any progress on this? I would allow for a optional struct at least for input fields, Especially now with Go 1.18's generics this could be implemented, more or less type safe. |
Also interested! |
gqlgen supported Go 1.18 and Generics. |
I drafted a PR to address this if anyone wants to have a look! |
I would like to get more opinions on #2585 (comment) please! |
# graphql
type Mutation {
submit(input: SubmitInput!): SubmitPayload
}
input SubmitInput {
firstName: String
lastName: String
email: String
} // go
type SubmitInput struct {
FirstName **string
LastName **string
Email **string
}
var input SubmitInput
if input.Email == nil {
// this is undefined
}
if input.Email != nil && *input.Email == nil {
// this is null
}
if input.Email != nil && *input.Email != nil {
email := **input.Email
// this is value
} Hi !~ Can this be one of solutions ? |
The spec that is used by oapi-codegen looks like the best solution thus far. https://github.com/oapi-codegen/nullable/blob/main/nullable.go |
I think this topic could win the award as the most debated topic on web all along
(since that damn day
null
was invented, at least 😄)!I would like to summarize here everything I have read and tried on the subject, in the hope that Santa Claus will give us the robust solution we deserve this year (at least for gqlgen).
THE PROBLEM
As a developer I would like a quick way to identify in my resolvers if the user has deliberately chosen
null
as the value of the field (string/int/time/...) or if he is using sparse updates and has omitted those fields entirely.PROPOSALS READ AROUND
the official solution proposed by Gqlgen team (@vektah at least), use
map[string]interface{}
(https://gqlgen.com/reference/changesets/); but of course there are several counter-argumentsnullable types (proposed many times, one for all here); but as @jszwedko said it seems to not work either
checking ArgumentMap from context as suggested hereInput types: null vs unspecified #505 (comment); the solution I'm using although it is very verbose and I need things like code generation
methods for setters proposed by the amazing @danilobuerger which is still a WIP
THREADS ON THE TOPIC
Input types: null vs unspecified #505
Input types: cant use methods for setters #506
changesets without using reflections? #977
Make Input Schema optional #866
using sqlboiler, null.string fields in structs, field resolvers created #646
WE ARE NOT ALONE
https://webdevstation.com/post/Work-with-nullable-strings-in-gqlgen-GraphQL-Server-0x4e28
https://www.calhoun.io/how-to-determine-if-a-json-key-has-been-set-to-null-or-not-provided/
Differentiate between JSON 'key = null' and 'key not set' guregu/null#39
Add support for null literals graphql-go/graphql#401
Support null literal graphql-go/graphql#178
Support
null
literals as a value for arguments and input object fields datastax/cassandra-data-apis#6Support for Null Values in mutations graph-gophers/graphql-go#210
Support where
Null
value came from graphql-rust/juniper#183Arguments cannot handling patching correctly graphql-rust/juniper#108
PLEASE
Help us, gqlgen team.
🙏
The text was updated successfully, but these errors were encountered: