If requested, validate binding-types get the right fields (#70)

## Summary:
One sharp edge of the new `bindings` setting (when used for composite
types) is this: the (presumably struct) type to which you're binding
may expect to have particular fields, but it's GraphQL so you could have
requested some other set of fields.  Now, if you ask us, we check.

Specifically, I've added a new setting under the `bindings` items, which
says: everywhere we query this must select these fields.  (Or use its
own inline `# @genqlient(bind: ...)`.)  It must select exactly those
fields, in order, no more, no less.  This was fairly easy to implement;
actually comparing the selections was surprisingly much code but it's
all pretty straightforward.

## Test plan:
make check


Author: benjaminjkraft

Reviewers: dnerdy, aberkan, csilvers, 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/70
This commit is contained in:
Ben Kraft
2021-08-30 10:18:52 -07:00
committed by GitHub
parent fc4aa084ae
commit 95609f77a1
10 changed files with 180 additions and 6 deletions
+20
View File
@@ -104,6 +104,26 @@ type TypeBinding struct {
// map[string]interface{}
// github.com/you/yourpkg/subpkg.MyType
Type string `yaml:"type"`
// If set, a GraphQL selection which must exactly match the fields
// requested whenever this type is used. Only applies if the GraphQL type
// is a composite output type (object, interface, or union).
//
// This is useful if Type is a struct whose UnmarshalJSON or other methods
// expect that you requested certain fields. You can specify those fields
// like
// MyType:
// type: path/to/my.GoType
// expect_exact_fields: "{ id name }"
// and then genqlient will reject if you make a query
// { fieldOfMytype { id title } }
// The fields must match exactly, including the ordering: "{ name id }"
// will be rejected. But the arguments and directives, if any, need not
// match.
//
// TODO(benkraft): Also add ExpectIncludesFields and ExpectSubsetOfFields,
// or something, if you want to say, for example, that you have to request
// certain fields but others are optional.
ExpectExactFields string `yaml:"expect_exact_fields"`
}
// baseDir returns the directory of the config-file (relative to which