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
This commit is contained in:
+23
-8
@@ -53,7 +53,7 @@ func (g *generator) convertOperation(
|
||||
|
||||
goTyp, err := g.convertDefinition(
|
||||
name, operation.Name, baseType, operation.Position,
|
||||
operation.SelectionSet, queryOptions)
|
||||
operation.SelectionSet, queryOptions, queryOptions)
|
||||
|
||||
if structType, ok := goTyp.(*goStructType); ok {
|
||||
// Override the ordinary description; the GraphQL documentation for
|
||||
@@ -140,6 +140,15 @@ func (g *generator) convertType(
|
||||
selectionSet ast.SelectionSet,
|
||||
options, queryOptions *GenqlientDirective,
|
||||
) (goType, error) {
|
||||
// We check for local bindings here, so that you can bind, say, a
|
||||
// `[String!]` to a struct instead of a slice. Global bindings can only
|
||||
// bind GraphQL named types, at least for now.
|
||||
localBinding := options.Bind
|
||||
if localBinding != "" && localBinding != "-" {
|
||||
goRef, err := g.addRef(localBinding)
|
||||
return &goOpaqueType{goRef}, err
|
||||
}
|
||||
|
||||
if typ.Elem != nil {
|
||||
// Type is a list.
|
||||
elem, err := g.convertType(
|
||||
@@ -150,7 +159,7 @@ func (g *generator) convertType(
|
||||
// If this is a builtin type or custom scalar, just refer to it.
|
||||
def := g.schema.Types[typ.Name()]
|
||||
goTyp, err := g.convertDefinition(
|
||||
name, namePrefix, def, typ.Position, selectionSet, queryOptions)
|
||||
name, namePrefix, def, typ.Position, selectionSet, options, queryOptions)
|
||||
|
||||
if options.GetPointer() {
|
||||
// Whatever we get, wrap it in a pointer. (Because of the way the
|
||||
@@ -172,11 +181,16 @@ func (g *generator) convertDefinition(
|
||||
def *ast.Definition,
|
||||
pos *ast.Position,
|
||||
selectionSet ast.SelectionSet,
|
||||
queryOptions *GenqlientDirective,
|
||||
options, queryOptions *GenqlientDirective,
|
||||
) (goType, error) {
|
||||
qualifiedGoName, ok := g.Config.Scalars[def.Name]
|
||||
if ok {
|
||||
goRef, err := g.addRef(qualifiedGoName)
|
||||
// Check if we should use an existing type. (This is usually true for
|
||||
// GraphQL scalars, but we allow you to bind non-scalar types too, if you
|
||||
// want, subject to the caveats described in Config.Bindings.) Local
|
||||
// bindings are checked in the caller (convertType) and never get here,
|
||||
// unless the binding is "-" which means "ignore the global binding".
|
||||
globalBinding, ok := g.Config.Bindings[def.Name]
|
||||
if ok && options.Bind != "-" {
|
||||
goRef, err := g.addRef(globalBinding.Type)
|
||||
return &goOpaqueType{goRef}, err
|
||||
}
|
||||
goBuiltinName, ok := builtinTypes[def.Name]
|
||||
@@ -306,7 +320,7 @@ func (g *generator) convertDefinition(
|
||||
// preprocessQueryDocument). But in practice it doesn't really
|
||||
// hurt, and would be extra work to avoid, so we just leave it.
|
||||
implTyp, err := g.convertDefinition(
|
||||
implName, namePrefix, implDef, pos, selectionSet, queryOptions)
|
||||
implName, namePrefix, implDef, pos, selectionSet, options, queryOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -335,8 +349,9 @@ func (g *generator) convertDefinition(
|
||||
return goType, nil
|
||||
|
||||
case ast.Scalar:
|
||||
// (If you had an entry in bindings, we would have returned it above.)
|
||||
return nil, errorf(
|
||||
pos, "unknown scalar %v: please add it to genqlient.yaml", def.Name)
|
||||
pos, `unknown scalar %v: please add it to "bindings" in genqlient.yaml`, def.Name)
|
||||
default:
|
||||
return nil, errorf(pos, "unexpected kind: %v", def.Kind)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user