github.com/jepp2078/gqlgen@v0.7.2/codegen/templates/models.gotpl (about)

     1  // Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
     2  
     3  package {{ .PackageName }}
     4  
     5  import (
     6  	%%%IMPORTS%%%
     7  
     8  	{{ reserveImport "context"  }}
     9  	{{ reserveImport "fmt"  }}
    10  	{{ reserveImport "io"  }}
    11  	{{ reserveImport "strconv"  }}
    12  	{{ reserveImport "time"  }}
    13  	{{ reserveImport "sync"  }}
    14  	{{ reserveImport "errors"  }}
    15  	{{ reserveImport "bytes"  }}
    16  
    17  	{{ reserveImport "github.com/vektah/gqlparser" }}
    18  	{{ reserveImport "github.com/vektah/gqlparser/ast" }}
    19  	{{ reserveImport "github.com/99designs/gqlgen/graphql" }}
    20  	{{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }}
    21  )
    22  
    23  {{ range $model := .Models }}
    24  	{{with .Description}} {{.|prefixLines "// "}} {{end}}
    25  	{{- if .IsInterface }}
    26  		type {{.GoType}} interface {
    27  			Is{{.GoType}}()
    28  		}
    29  	{{- else }}
    30  		type {{.GoType}} struct {
    31  			{{- range $field := .Fields }}
    32  				{{- with .Description}}
    33  					{{.|prefixLines "// "}}
    34  				{{- end}}
    35  				{{- if $field.GoFieldName }}
    36  					{{ $field.GoFieldName }} {{$field.Signature}} `json:"{{$field.GQLName}}"`
    37  				{{- else }}
    38  					{{ $field.GoFKName }} {{$field.GoFKType}}
    39  				{{- end }}
    40  			{{- end }}
    41  		}
    42  
    43  		{{- range $iface := .Implements }}
    44  			func ({{$model.GoType}}) Is{{$iface.GoType}}() {}
    45  		{{- end }}
    46  
    47  	{{- end }}
    48  {{- end}}
    49  
    50  {{ range $enum := .Enums }}
    51  	{{with .Description}}{{.|prefixLines "// "}} {{end}}
    52  	type {{.GoType}} string
    53  	const (
    54  	{{- range $value := .Values}}
    55  		{{- with .Description}}
    56  			{{.|prefixLines "// "}}
    57  		{{- end}}
    58  		{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}
    59  	{{- end }}
    60  	)
    61  
    62  	func (e {{.GoType}}) IsValid() bool {
    63  		switch e {
    64  		case {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:
    65  			return true
    66  		}
    67  		return false
    68  	}
    69  
    70  	func (e {{.GoType}}) String() string {
    71  		return string(e)
    72  	}
    73  
    74  	func (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {
    75  		str, ok := v.(string)
    76  		if !ok {
    77  			return fmt.Errorf("enums must be strings")
    78  		}
    79  
    80  		*e = {{.GoType}}(str)
    81  		if !e.IsValid() {
    82  			return fmt.Errorf("%s is not a valid {{.GQLType}}", str)
    83  		}
    84  		return nil
    85  	}
    86  
    87  	func (e {{.GoType}}) MarshalGQL(w io.Writer) {
    88  		fmt.Fprint(w, strconv.Quote(e.String()))
    89  	}
    90  
    91  {{- end }}