package generate import ( "fmt" "strings" "github.com/vektah/gqlparser/v2/ast" ) type typeBuilder struct { typeName string typeNamePrefix string strings.Builder *generator } func (g *generator) baseTypeForOperation(operation ast.Operation) (*ast.Definition, error) { switch operation { case ast.Query: return g.schema.Query, nil case ast.Mutation: return g.schema.Mutation, nil case ast.Subscription: if !allowBrokenFeatures { return nil, fmt.Errorf("genqlient does not yet support subscriptions") } return g.schema.Subscription, nil default: return nil, fmt.Errorf("unexpected operation: %v", operation) } } func (g *generator) getTypeForOperation(operation *ast.OperationDefinition, queryOptions *GenqlientDirective) (name string, err error) { // TODO: configure ResponseName format name = operation.Name + "Response" if def, ok := g.typeMap[name]; ok { // TODO: check for and handle conflicts a better way return "", fmt.Errorf("%s defined twice:\n%s", name, def) } fields, err := selections(g, operation.SelectionSet, queryOptions) if err != nil { return "", err } baseType, err := g.baseTypeForOperation(operation.Operation) if err != nil { return "", err } return g.addTypeForDefinition(operation.Name, name, baseType, fields, queryOptions) } var builtinTypes = map[string]string{ // GraphQL guarantees int32 is enough, but using int seems more idiomatic "Int": "int", "Float": "float64", "String": "string", "Boolean": "bool", "ID": "string", } func (g *generator) addTypeForDefinition(namePrefix, nameOverride string, typ *ast.Definition, fields []field, options *GenqlientDirective) (name string, err error) { // If this is a builtin type or custom scalar, just refer to it. goName, ok := g.Config.Scalars[typ.Name] if ok { return g.addRef(goName) } goName, ok = builtinTypes[typ.Name] if ok { return goName, nil } if nameOverride != "" { // if we have an explicit name, the passed-in prefix is what we // propagate forward name = nameOverride } else { typeGoName := upperFirst(typ.Name) if typ.Kind == ast.Enum || typ.Kind == ast.InputObject { // If we're an enum or an input-object, there is only one type we // will ever possibly generate for this type, so we don't need any // of the qualifiers. This is especially helpful because the // caller is very likely to need to reference these types in their // code. name = typeGoName } else if strings.HasSuffix(namePrefix, typeGoName) { // If the field and type names are the same, we can avoid the // duplication. (We include the field name in case there are // multiple fields with the same type, and the type name because // that's the actual name (the rest are really qualifiers); but if // they are the same then including it once suffices for both // purposes.) name = namePrefix } else { name = namePrefix + typeGoName } if typ.Kind != ast.Interface && typ.Kind != ast.Union { // for interface/union types, we do not add the type name to the // name prefix; we want to have QueryFieldType rather than // QueryFieldInterfaceType. Otherwise, the name will also be the // prefix for the next type. namePrefix = name } // TODO: for input and enum types, we can probably skip the prefix // entirely; they don't have fields that may be used in some places but // not others. } // Otherwise, build the type, put that in the type-map, and return its // name. builder := &typeBuilder{typeName: name, typeNamePrefix: namePrefix, generator: g} fmt.Fprintf(builder, "type %s ", name) err = builder.writeTypedef(typ, fields, options) if err != nil { return "", err } // TODO: this should also check for conflicts (except not for enums and // input-objects, see above) g.typeMap[name] = builder.String() return name, nil } func (g *generator) getTypeForInputType(opName string, typ *ast.Type, options, queryOptions *GenqlientDirective) (string, error) { // Sort of a hack: case the input type name to match the op-name. name := matchFirst(typ.Name(), opName) // TODO: we have to pass name 4 times, yuck builder := &typeBuilder{typeName: name, typeNamePrefix: name, generator: g} // TODO: passing options is actually kinda wrong, because it means we could // break the "there is only Go type for each input type" rule. In practice // it's probably rare that you use the same input type twice in a query and // want different settings, though, and it just means we choose one or the // other set of options. // TODO: it's also awkward because you have no way to pass an option for an // individual input-type field. // TODO: should we use pointers by default for input-types if they're // structs? err := builder.writeType(name, name, typ, selectionsForType(g, typ, queryOptions), options) return builder.String(), err } type field interface { Alias() string Options() (*GenqlientDirective, error) Type() *ast.Type SubFields() ([]field, error) } type outputField struct { *generator queryOptions *GenqlientDirective field *ast.Field } func (s outputField) Alias() string { // gqlparser sets Alias even if the field is not aliased, see e.g. // https://github.com/vektah/gqlparser/v2/blob/c06d8e0d135f285e37e7f1ff397f10e049733eb3/parser/query.go#L150 return s.field.Alias } func (s outputField) Options() (*GenqlientDirective, error) { _, directive, err := s.generator.parsePrecedingComment(s.field, s.field.Position) if err != nil { return nil, err } return s.queryOptions.merge(directive), nil } func (s outputField) Type() *ast.Type { if s.field.Definition == nil { return nil } return s.field.Definition.Type } func (s outputField) SubFields() ([]field, error) { return selections(s.generator, s.field.SelectionSet, s.queryOptions) } func selections(g *generator, selectionSet ast.SelectionSet, options *GenqlientDirective) ([]field, error) { retval := make([]field, len(selectionSet)) for i, selection := range selectionSet { switch selection := selection.(type) { case *ast.Field: retval[i] = outputField{g, options, selection} case *ast.FragmentSpread, *ast.InlineFragment: return nil, fmt.Errorf("not implemented: %T", selection) default: return nil, fmt.Errorf("invalid selection type: %v", selection) } } return retval, nil } type inputField struct { *generator field *ast.FieldDefinition queryOptions *GenqlientDirective } func (s inputField) Alias() string { return s.field.Name } func (s inputField) Options() (*GenqlientDirective, error) { _, directive, err := s.generator.parsePrecedingComment(s.field, s.field.Position) if err != nil { return nil, err } return s.queryOptions.merge(directive), nil } func (s inputField) Type() *ast.Type { return s.field.Type } func (s inputField) SubFields() ([]field, error) { return selectionsForType(s.generator, s.field.Type, s.queryOptions), nil } func selectionsForType(g *generator, typ *ast.Type, queryOptions *GenqlientDirective) []field { def := g.schema.Types[typ.Name()] fields := make([]field, len(def.Fields)) for i, field := range def.Fields { fields[i] = inputField{g, field, queryOptions} } return fields } func (builder *typeBuilder) writeField(field field) error { jsonName := field.Alias() // We need an exportable name for JSON-marshaling. goName := upperFirst(jsonName) builder.WriteString(goName) builder.WriteRune(' ') typ := field.Type() if typ == nil { // Unclear why gqlparser hasn't already rejected this, // but empirically it might not. return fmt.Errorf("undefined field %v", field.Alias()) } fields, err := field.SubFields() if err != nil { return err } options, err := field.Options() if err != nil { return err } err = builder.writeType( // Note we don't deduplicate suffixes here -- if our prefix is GetUser // and the field name is User, we do GetUserUser. This is important // because if you have a field called user on a type called User we // need `query q { user { user { id } } }` to generate two types, QUser // and QUserUser. // Note also this is the alias, not the field-name, because if we have // `query q { a: f { b }, c: f { d } }` we need separate types for a // and c, even though they are the same type in GraphQL, because they // have different fields. builder.typeNamePrefix+upperFirst(field.Alias()), "", typ, fields, options) if err != nil { return err } if builder.schema.Types[typ.Name()].IsAbstractType() { // abstract types are handled in our UnmarshalJSON builder.WriteString(" `json:\"-\"`") } else { fmt.Fprintf(builder, " `json:\"%s\"`", jsonName) } builder.WriteRune('\n') return nil } func (builder *typeBuilder) writeType(namePrefix, nameOverride string, typ *ast.Type, fields []field, options *GenqlientDirective) error { // gqlgen does slightly different things here, but its implementation may // be useful to crib from: // https://github.com/99designs/gqlgen/blob/master/plugin/modelgen/models.go#L113 if typ.Elem != nil { // Type is a list. builder.WriteString("[]") typ = typ.Elem } if options.GetPointer() { // TODO: this does []*T, you might in principle want *[]T or // *[]*T. builder.WriteString("*") } def := builder.schema.Types[typ.Name()] // Writes a typedef elsewhere (if not already defined) name, err := builder.addTypeForDefinition(namePrefix, nameOverride, def, fields, options) if err != nil { return err } builder.WriteString(name) return nil } func (builder *typeBuilder) writeTypedef(typedef *ast.Definition, fields []field, options *GenqlientDirective) error { switch typedef.Kind { case ast.Object, ast.InputObject: builder.WriteString("struct {\n") for _, field := range fields { err := builder.writeField(field) if err != nil { return err } } builder.WriteString("}") // If any field is abstract, we need an UnmarshalJSON method to handle // it. return builder.maybeWriteUnmarshal(fields) case ast.Interface, ast.Union: if !allowBrokenFeatures { return fmt.Errorf("not implemented: %v", typedef.Kind) } // First, write the interface type. builder.WriteString("interface {\n") implementsMethodName := fmt.Sprintf("implementsGraphQLInterface%v", builder.typeName) // TODO: Also write GetX() accessor methods for fields of the interface builder.WriteString(implementsMethodName) builder.WriteString("()\n") builder.WriteString("}") // Then, write the implementations. // TODO(benkraft): Put a doc-comment somewhere with the list. for _, impldef := range builder.schema.GetPossibleTypes(typedef) { name, err := builder.addTypeForDefinition(builder.typeNamePrefix, "", impldef, fields, options) if err != nil { return err } builder.typeMap[name] += fmt.Sprintf( "\nfunc (v %v) %v() {}", name, implementsMethodName) } return nil case ast.Enum: // All GraphQL enums have underlying type string (in the Go sense). builder.WriteString("string\n") builder.WriteString("const (\n") for _, val := range typedef.EnumValues { fmt.Fprintf(builder, "%s %s = \"%s\"\n", builder.typeNamePrefix+goConstName(val.Name), builder.typeName, val.Name) } builder.WriteString(")\n") return nil case ast.Scalar: return fmt.Errorf("unknown scalar %v: please add it to genqlient.yaml", typedef.Name) default: return fmt.Errorf("unexpected kind: %v", typedef.Kind) } }