-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for concrete-typed named fragments
In previous commits I added support to genqlient for interfaces and inline fragments. This means the only query structures that remain are named fragments and their spreads, e.g. ``` fragment MyFragment on MyType { myField } query MyQuery { getMyType { ...MyFragment } } ``` Other than mere completionism, these are potentially useful for code sharing: you can spread the same fragment multiple places; and then genqlient can notice that and generate the same type for each. (They can even be shared between different queries in the same package.) In this commit I add support for named fragments of concrete (object/struct, not interface) type, spread into either concrete or abstract scope. For genqlient's purposes, these are a new "root" type-name, just like each operation, and are then embedded into the appropriate struct. (Using embeds allows their fields to be referenced as fields of the containing type, if convenient. Further design considerations are discussed in DESIGN.md.) This requires new code in two main places (plus miscellaneous glue), both nontrivial but neither particularly complex: - We need to actually traverse both structures and generate the types (in `convert.go`). - We need to decide which fragments from this package to send to the server, both for good hyigene and because GraphQL requires we send only ones this query uses (in `generate.go`). - We need a little new wiring for options -- because fragments can be shared between queries they get their own toplevel options, rather than inheriting the query's options. Finally, this required slightly subtler changes to how we do unmarshaling (in `types.go` and `unmarshal.go.tmpl`). Basically, because embedded fields' methods, including `UnmarshalJSON`, get promoted to the parent type, and because the JSON library ignores their fields when shadowed by those of the parent type, we need a little bit of special logic in each such parent type to do its own unmarshal and then delegate to each embed. This is similar (and much simpler) to what we did for interfaces, although it required some changes to the "method-hiding" trick (used for both). It's only really necessary in certain specific cases (namely when an embedded type has an `UnmarshalJSON` method or a field with the same name as the embedder), but it's easier to just generate it always. This is all described in more detail inline. This does not support fragments of abstract type, which have their own complexities. I'll address those, which are now the only remaining piece of #8, in a future commit. Issue: #8 Test plan: make check Reviewers: marksandstrom, miguel, adam
- Loading branch information
1 parent
6fdb170
commit 0e64773
Showing
21 changed files
with
1,850 additions
and
122 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
fragment QueryFragment on Query { | ||
...InnerQueryFragment | ||
} | ||
|
||
fragment InnerQueryFragment on Query { | ||
randomItem { | ||
id name | ||
...VideoFields | ||
} | ||
randomLeaf { | ||
...VideoFields | ||
...MoreVideoFields | ||
} | ||
otherLeaf: randomLeaf { | ||
... on Video { | ||
...MoreVideoFields | ||
} | ||
} | ||
} | ||
|
||
fragment VideoFields on Video { | ||
id name url duration thumbnail { id } | ||
} | ||
|
||
# @genqlient(pointer: true) | ||
fragment MoreVideoFields on Video { | ||
id | ||
parent { | ||
name url | ||
# @genqlient(pointer: false) | ||
children { | ||
...VideoFields | ||
} | ||
} | ||
} | ||
|
||
query ComplexNamedFragments { | ||
... on Query { ...QueryFragment } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
fragment VideoFields on Video { | ||
id name url duration thumbnail { id } | ||
} | ||
|
||
query SimpleNamedFragment { | ||
randomItem { | ||
id name | ||
...VideoFields | ||
} | ||
randomLeaf { | ||
...VideoFields | ||
} | ||
} |
Oops, something went wrong.