Commit Graph

49 Commits

Author SHA1 Message Date
Nikolay Edigaryev f80df6d4f1 Sort operations to guarantee a stable order (#156)
Problem: currently when using a wildcard that covers multiple files in `operations:` YAML directive the generator emits functions in a non-stable ordering due to the use of map for storing file names:

https://github.com/Khan/genqlient/blob/e0accbded177db9143314911bacedf61b2cda656/generate/parse.go#L68-L84

Solution: sort the operations before emitting them.
2021-11-26 12:32:03 -08:00
Ben Kraft 3a5bf46da4 Add getter methods to all fields, not just where needed (#126)
## Summary:
If your type implements an interface, we add getter methods for the
shared fields, so that those may be accessed via the interface.  But it
turns out occasionally it's useful to have these getter methods when
they don't implement a GraphQL interface, so you can use two
genqlient-generated types in the same function if they have the same
fields.  (This comes up most often when you have a GraphQL union that
maybe should really be an interface, or if you don't yet support
interfaces implementing other interfaces (indeed our parser doesn't
either).  But one can imagine other use cases.)

We can't predict how you want to do that, so we can't generate the
interface, but we can generate the methods, so you can define the
interface and do a type assertion from there.  Since these methods are
pretty simple to generate, we just do it always.  (As with #120, if
binary size becomes an issue we could later add an option to only
generate methods that are truly needed but including them seems like the
better default.)

This also fixes a subtle and rare bug, which would have become much more
common (indeed existing tests caught it).  Specifically, if you have a
query like
```graphql
fragment FragmentOne on T { id }
fragment FragmentTwo on T { id }
query Q {
    f {   # interface type T
        ...FragmentOne
        ...FragmentTwo
    }
}
```
since both `FragmentOne` and `FragmentTwo` request some common field,
say `id`, we generate a method `GetId` on each one.  But since
`FragmentOne` and `FragmentTwo` are both on `T`, we also include their
interfaces in the interface we generate for the type of `f`, `QFT`.  So
`QFT` includes a method `GetId`.  But on the implementations, the two
methods conflict, and neither gets promoted; this causes various code to
fail to compile.  With this change, this would have happened much more
frequently -- even if only one of the two fragments is on `T`, as long
as both request the field.  Anyway, we now generate explicit methods on
each struct for all of its recursively emebedded fields -- using the
logic from #120 to compute them -- so that we don't need to rely on
method-promotion.

## Test plan:
make tesc


Author: benjaminjkraft

Reviewers: csilvers, dnerdy, aberkan, jvoll, mahtabsabet, MiguelCastillo, StevenACoffman

Required Reviewers: 

Approved By: csilvers, dnerdy

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint

Pull Request URL: https://github.com/Khan/genqlient/pull/126
2021-10-01 15:02:07 -07:00
Ben Kraft 5995653583 Refactor argument-handling to use a struct (#103)
## Summary:
In this commit I refactor the argument-generation logic to move most of
the code out of the template and into the type-generator.  This logic
predates #51, and I didn't think to update it there, but I think it
benefits from similar treatment, for similar reasons.

Specifically, the main change is to treat variables as another struct
type we can generate, rather than handling them inline as a
`map[string]interface{}`.  Users still pass them the same way, but
instead of putting them into a `map[string]interface{}` and JSONifying
that, we generate a struct and put them there.

This turns out to simplify things quite a lot, because we already have a
lot of code to generate types.  Notably, the omitempty code goes from a
dozen lines to basically two, and fixes a bug (#43) in the process,
because now that we have a struct, `json.Marshal` will do our work for
us! (And, once we have syntax for it (#14), we'll be able to handle
field-level omitempty basically for free.)  More importantly, it will
simplify custom marshalers (#38, forthcoming) significantly, since we do
all that logic at the containing-struct level, but will need to apply it
to arguments.

It does require two breaking changes:

1. For folks implementing the `graphql.Client` API (rather than just
   calling `NewClient`): we now pass them variables as an `interface{}`
   rather than a `map[string]interface{}`.  For most callers, including
   Khan/webapp, this is basically a one-line change to the signature of
   their `MakeRequest`, and it should be a lot more future-proof.
2. genqlient's handling of the `omitempty` option has changed to match
   that of `encoding/json`, in particular it now never considers structs
   "empty".  The difference was never intentional (I just didn't realize
   that behavior of `encoding/json`); arguably our behavior was more
   useful but I think that's outweighed by the value of consistency with
   `encoding/json` as well as the simpler and more correct
   implementation (fixing #43 is actually quite nontrivial otherwise).
   Once we have custom unmarshaler support (#38), users will be able to
   map a zero value to JSON null if they wish, which is mostly if not
   entirely equivalent for GraphQL's purposes.

Issue: https://github.com/Khan/genqlient/issues/38
Issue: https://github.com/Khan/genqlient/issues/43

## Test plan:
make check

Author: benjaminjkraft

Reviewers: StevenACoffman, dnerdy, aberkan, jvoll, mahtabsabet, MiguelCastillo

Required Reviewers: 

Approved By: StevenACoffman, dnerdy

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint,  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14)

Pull Request URL: https://github.com/Khan/genqlient/pull/103
2021-09-22 17:16:36 -07:00
Ben Kraft ce079a7e16 Clean up example a little (#89)
## Summary:
I realized:
- it's simpler to avoid the usual `cmd` and just put it all in the same
  directory
- we don't need schema.json since GitHub now posts a schema in SDL
  format

## Test plan:
make lint example


Author: benjaminjkraft

Reviewers: dnerdy, StevenACoffman, aberkan, jvoll, mahtabsabet, MiguelCastillo

Required Reviewers: 

Approved By: dnerdy, StevenACoffman

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint

Pull Request URL: https://github.com/Khan/genqlient/pull/89
2021-09-15 10:48:50 -07:00
Steve Coffman 673840e495 Picking some nits that my IDE complained about (#91)
* Picking some nits that my IDE complained about

Signed-off-by: Steve Coffman <steve@khanacademy.org>

* Update graphql/util.go

Co-authored-by: Ben Kraft <benkraft@khanacademy.org>

* revert to original for comment

Signed-off-by: Steve Coffman <steve@khanacademy.org>

Co-authored-by: Ben Kraft <benkraft@khanacademy.org>
2021-09-14 13:42:47 -04:00
Ben Kraft 6c86eed770 Clean up, test, and document ContextType and ClientGetter options (#77)
## Summary:
ContextType is in use at Khan as a part of our ka-context system; it
basically just lets you configure the type to pass as the `ctx` argument
to genqlient helpers (or say to omit such an argument).  ClientGetter I
wrote thinking we might use it; then we didn't (because we have a few
different clients we may use) but it's not much code and may be helpful
to others.  In this commit I clean up, document, and add tests for both
options.

The cleanup is mainly for ClientGetter, which was kind of broken before
because it was a Go snippet but couldn't specify imports.  I was
thinking maybe you want to be able to write `ctx.Something()`, but I
just don't see how to make it work, so I made it a function of context,
which is probably the better idea anyway.

Additionally, I improved the documentation for both, and added tests for
those and several other config options that weren't completely tested.

Fixes #5.

Issue: https://github.com/Khan/genqlient/issues/5

## Test plan:
make check


Author: benjaminjkraft

Reviewers: dnerdy, aberkan, MiguelCastillo

Required Reviewers: 

Approved By: dnerdy

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint

Pull Request URL: https://github.com/Khan/genqlient/pull/77
2021-09-07 09:58:49 -07:00
Ben Kraft 222d6f191a Document and improve support for binding non-scalars to a specific type (#69)
## Summary:
We had this setting called "scalars", which said: bind this GraphQL type
to this Go type, rather than the one you would normally use.  It's
called that because it's most useful for custom scalars, where "the one
you would normally use" is "error: unknown scalar".  But nothing ever
stopped you from using it for a non-scalar type.  I was planning on
removing this functionality, because it's sort of a rough edge, but a
discussion with Craig found some good use cases, so instead, in this
commit, I document it better and add some slightly nicer ways to specify
it.

Specifically, here are a few potential non-scalar use cases:
- bind a GraphQL enum to a nonstandard type (or even `string`)
- bind an input type to some type that has exactly the fields you want;
  this acts as a sort of workaround for issues #14 and #44
- bind an object type to your own struct, so as to add methods to it
  (this is the use case Craig raised)
- bind an object type to your own struct, so as to share it between
  multiple queries (I believe named fragments will address this case
  better, but it doesn't hurt to have options)
- bind a GraphQL list type to a non-slice type in Go (presumably one
  with an UnmarshalJSON method), or any other different structure
The latter three cases still have the sharp edge I was originally
worried about, which is that nothing guarantees that the fields you
request in the query are the ones the type expects to get.  But I think
it's worth having the option, with appropriate disclaimers.

The main change to help support that better is that you can now specify
the type inline in the query, as an alternative to specifying it in the
config file; this means you might map a given object to a given struct,
but only in some cases, and when you do you have a chance to look at the
list of fields you're requesting.

Additionally, I renamed the config field from "scalars" to "bindings"
(but mentioned it in a few places where you might go looking for how to
map scalars, most importantly the error message you get for an unknown
(custom) scalar).  While I was making a breaking change, I also changed
it to be a `map[string]<struct>` instead of a `map[string]string`,
because I expect to add more fields soon, e.g. to handle issue #38.

Finally, since the feature is now intended/documented, I added some
tests, although it's honestly quite simple on the genqlient side.

## Test plan:
make tesc


Author: benjaminjkraft

Reviewers: csilvers, aberkan, dnerdy, MiguelCastillo

Required Reviewers: 

Approved by: csilvers

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint

Pull request URL: https://github.com/Khan/genqlient/pull/69
2021-08-27 15:57:10 -07:00
Ben Kraft 3746ecd063 Add integration tests against a gqlgen server (#50)
## Summary:
We have lots of tests covering codegen, but not a lot that actually run
the code.  For things where all we do is generate types, that's (mostly)
fine (especially now that we actually build the code), but as we
generate more nontrivial non-type code we need to actually run it.

So I wrote some integration tests that spin up a little gqlgen
server, and make calls to it; we can add more over time especially as
the JSON marshalling logic gets complex (to support fragments).
They're more work to write than the snapshot tests, but of course they
can test a lot more.

In addition to gqlgen, I pulled in testify assert/require, because I
really wanted to be able to use assert.Equal and such for these.  I
didn't bother converting existing tests, although I assume they will
become useful elsewhere in time.  Both gqlgen and testify are of course
only used in tests.

Fixes #21 and #24.
Issue: https://github.com/Khan/genqlient/issues/21
Issue: https://github.com/Khan/genqlient/issues/24

## Test plan:
make check

Author: benjaminjkraft

Reviewers: aberkan, dnerdy, benjaminjkraft, csilvers, MiguelCastillo

Required Reviewers: 

Approved by: aberkan, dnerdy

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint

Pull request URL: https://github.com/Khan/genqlient/pull/50
2021-08-20 10:54:41 -07:00
Ben Kraft 700392315a Enable golangci-lint (#49)
## Summary:
It would be nice to have some linting beyond `go vet`!  Now we do.  I
started by copying the config from Khan/webapp.  I did remove a couple
of staticcheck checks that I didn't feel were useful.  (Note also that
exportloopref is the replacement for scopelint in newer golangci-lint.)

Included are all the needed lint fixes; most are stylistic but the
changes in the example are a (minor) bugfix.

Fixes #22.
Issue: https://github.com/Khan/genqlient/issues/22

## Test plan:
make check

Author: benjaminjkraft

Reviewers: aberkan, dnerdy, benjaminjkraft, csilvers, MiguelCastillo

Required Reviewers: 

Approved by: aberkan, dnerdy

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Test (1.13),  Lint

Pull request URL: https://github.com/Khan/genqlient/pull/49
2021-08-20 10:39:12 -07:00
Ben Kraft d449acdda3 Clarify object type documentation
Craig pointed out this is a bit confusing when you don't have all the
fields.  Now we say so, but still include the type's description in case
it's useful.

Fixes #37.
2021-04-23 18:13:32 -07:00
Ben Kraft 748f2cf072 Add documentation to types and fields
Fixes #3.
2021-04-21 18:07:36 -07:00
Ben Kraft 136ac7729f include alias even if we don't need it 2021-04-15 11:36:46 -07:00
Ben Kraft b4e8316c6a add support for custom scalars -- mainly adding proper import machinery 2021-04-08 13:07:31 -07:00
Ben Kraft 9c8cf02181 proper test wiring for example test in github actions 2021-04-02 18:04:05 -07:00
Ben Kraft 21954ac62d general pass of README cleanup 2021-04-02 17:01:49 -07:00
Ben Kraft 9fe8f09e8c more miscellaneous fixes while integrating into webapp 2021-04-01 15:04:36 -07:00
Ben Kraft ec6681e715 allow multiple queries-files 2021-04-01 12:29:24 -07:00
Ben Kraft cbebeaaed4 rename in code 2021-03-30 12:41:37 -07:00
Ben Kraft 2c52314494 set opname 2021-03-30 11:27:43 -07:00
Ben Kraft 937540d244 make client an interface 2021-03-25 14:08:12 -07:00
Ben Kraft dd85597577 more wiring for configurable context/client, theoretically should be good enough for khan use 2021-03-22 19:17:06 -07:00
Ben Kraft a42c9b8166 clean up various TODOs and comments 2021-03-22 18:11:51 -07:00
Ben Kraft 463e3ed319 remove pointers for optionality -- shockingly easy 2021-03-22 17:39:22 -07:00
Ben Kraft fed38e4f55 redo type naming 2021-03-19 18:40:58 -07:00
Ben Kraft cf7136ca65 total rewrite to interface handling; not complete but it compiles 2020-07-16 13:28:43 -07:00
Ben Kraft 5042a9a549 allow omitting context 2020-05-01 18:45:39 -07:00
Ben Kraft 7ab06dfd30 add support for enums 2020-04-10 18:16:32 -07:00
Ben Kraft b600df7877 big refactor to put the codegen onto methods of an object 2020-04-10 15:21:23 -07:00
Ben Kraft dfea9bf128 push type-name into types.go 2020-04-10 14:30:56 -07:00
Ben Kraft af5d1bd542 lowercase for arguments 2020-03-27 18:01:56 -07:00
Ben Kraft 01b903b053 Doc comments 2020-03-27 17:44:30 -07:00
Ben Kraft 6caa3fc6c4 Give up on running example in GitHub Actions for now 2020-03-27 16:27:08 -07:00
Ben Kraft 8df2900607 Try example without viewer 2020-03-27 16:18:23 -07:00
Ben Kraft 0911fda620 Fix example envvar name 2020-03-27 16:06:23 -07:00
Ben Kraft 9074926cea Set up a test 2020-03-27 15:57:22 -07:00
Ben Kraft 0dcd124c8e fix filenames, set up go generate 2020-02-06 18:52:00 -08:00
Ben Kraft 35699a73ef more README stuff, example README, other misc cleanup 2020-01-16 17:56:13 -08:00
Ben Kraft 12605986d0 do our own aliasing if the queries aren't right 2020-01-02 19:01:26 -08:00
Ben Kraft 7dfd03738c genql: bits of whitespace 2020-01-02 18:00:23 -08:00
Ben Kraft 9746c818fd genql: quick hack at variables 2020-01-02 17:52:55 -08:00
Ben Kraft 77670363d3 genql: minor cleanups 2020-01-02 16:40:47 -08:00
Ben Kraft 7f6ab70ee3 wire auth to example, fix wire format, it's aliiiiiive! 2019-12-25 21:33:12 -05:00
Ben Kraft 4d880a701e move a bunch of the logic from generated into client 2019-12-25 16:26:32 -05:00
Ben Kraft 9d4b5559f4 wire up example by way of testing 2019-12-24 16:05:10 -05:00
Ben Kraft 9a157470b3 replace handwritten generated code with actual generated code 2019-12-24 15:56:01 -05:00
Ben Kraft e8b5ccbcda generates something mostly plausiblegit add . 2019-12-23 23:22:19 -05:00
Ben Kraft 7a20e2ae74 fill out the easy parts of the codegen 2019-12-23 21:07:07 -05:00
Ben Kraft efa9ccd122 add basic query struct, support 1.12 2019-12-23 19:35:38 -05:00
Ben Kraft a23b0e3756 sketches of generated graphql client 2019-12-21 00:03:19 -05:00