-
Notifications
You must be signed in to change notification settings - Fork 114
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
Add a mechanism to specify options on input-type fields #124
Conversation
This has been a bit of a thorn since we started using genqlient in production: just as you might want to specify, say, `omitempty` on an argument, you might equally want to specify it on an input-type field. But there's no obvious syntax to do that, because the input-type field does not appear in the query (only the schema) so there's nowhere to put the `# @genqlient` directive. This commit, at last, fixes that problem, via a new option, `for`, which you use in an option applied to the entire operation (or fragment), and says, "actually, apply this directive to the given field, not the entire operation". (It's mainly useful for input types, but I allowed it for output types too; I could imagine it being convenient if you want to say you always use a certain type or type-name for a certain field.) It works basically like you expect: the inline options take precedence over `for` take precedence over query-global options. The implementation was fairly straightforward once I did a little refactoring, mostly in the directive-parsing and directive-merging (which are now combined, since merging is now a bit more complicated). With that in place, and extended to support `for`, we need only add the same wiring to input-fields that we have for other places you can put directives. I did not attempt to solve the issue I've now documented as #123, wherein conflicting options can lead to confusing behavior; the new `for` is a new and perhaps more attractive avenue to cause it but the issue remains the same and requires nontrivial refactoring (described in the issue) to solve. (The breakage isn't horrible for the most part; the option will just apply, or not apply, where you don't expect it to.) But while applying that logic, I noticed a problem, which is that we were inconsistently cascading operation-level options down to input-object fields. (I think this came out of the fact that initially I thought to cascade them, then realized that this could cause problems like #123 and intended to walk them back, but then accidentally only "fixed" it for `omitempty`. I guess until this change, operation-level options were rare enough, and input-field options messy enough, that no one noticed.) So in this commit I bring things back into consistency, by saying that they do cascade: with at least a sketch of a path forward to fix #123 via better validation, I think that's by far the clearest behavior. Issue: #14 Test plan: make check Reviewers: csilvers, marksandstrom, steve, jvoll, adam, miguel, mahtab
@@ -1,11 +1,14 @@ | |||
# @genqlient(pointer: true) | |||
# @genqlient(for: "UserQueryInput.id", pointer: false) | |||
# @genqlient(for: "User.id", pointer: false) | |||
query PointersQuery( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to put those example in the docs. All your examples have just one directive and I think it's not necessarily obvious how they combine. This examples makes it very clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out I need this for one of the mutations I'm converting! So I'm glad you managed to squeeze it in. Even though I'm not super-familiar with the genqlient code this seemed self-contained enough that it made sense to me, so I'm happy to approve it!
var err error | ||
forField := "" | ||
for _, arg := range graphQLDirective.Arguments { | ||
if arg.Name == "for" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to check if for
is specified multiple times, and error out if so?
if dir.FieldDirectives[typeName] == nil { | ||
dir.FieldDirectives[typeName] = make(map[string]*genqlientDirective) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this happen? I guess it doesn't hurt to be defensive, but if all genqlientDirectives are made via newGenqlientDirective then this if
will never be true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes -- since it's a map of maps we can only initialize the toplevel one upfront. (It really pains me that Go didn't make the zero value more usable here, but they didn't.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. Yeah, being forced to call make
to make maps is pretty annoying. You don't have to do it for slices!, so...
parent *ast.Definition, | ||
operationDirective *genqlientDirective, | ||
) { | ||
var forField *genqlientDirective |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the for
field for some parent element whose for
points to us, is that right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll comment better.
generate/genqlient_directive.go
Outdated
// parent need only be set if node is an input-type field; it should be | ||
// the type containing this field. (We can get this from gqlparser in other | ||
// cases, but not input-type fields.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you maybe want to call it parentIfInputField then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out I need this for a mutation I'm doing as well, so very timely!
Summary:
This has been a bit of a thorn since we started using genqlient in
production: just as you might want to specify, say,
omitempty
on anargument, you might equally want to specify it on an input-type field.
But there's no obvious syntax to do that, because the input-type field
does not appear in the query (only the schema) so there's nowhere to put
the
# @genqlient
directive.This commit, at last, fixes that problem, via a new option,
for
, whichyou use in an option applied to the entire operation (or fragment), and
says, "actually, apply this directive to the given field, not the entire
operation". (It's mainly useful for input types, but I allowed it for
output types too; I could imagine it being convenient if you want to say
you always use a certain type or type-name for a certain field.) It
works basically like you expect: the inline options take precedence over
for
take precedence over query-global options.The implementation was fairly straightforward once I did a little
refactoring, mostly in the directive-parsing and directive-merging
(which are now combined, since merging is now a bit more complicated).
With that in place, and extended to support
for
, we need only add thesame wiring to input-fields that we have for other places you can put
directives. I did not attempt to solve the issue I've now documented
as #123, wherein conflicting options can lead to confusing behavior;
the new
for
is a new and perhaps more attractive avenue to cause itbut the issue remains the same and requires nontrivial refactoring
(described in the issue) to solve. (The breakage isn't horrible for the
most part; the option will just apply, or not apply, where you don't
expect it to.)
But while applying that logic, I noticed a problem, which is that we
were inconsistently cascading operation-level options down to
input-object fields. (I think this came out of the fact that initially
I thought to cascade them, then realized that this could cause problems
like #123 and intended to walk them back, but then accidentally only
"fixed" it for
omitempty
. I guess until this change, operation-leveloptions were rare enough, and input-field options messy enough, that no
one noticed.) So in this commit I bring things back into consistency,
by saying that they do cascade: with at least a sketch of a path forward
to solve #123 via better validation, I think that's by far the clearest
behavior.
Issue: #14
Test plan:
make check