github.com/vetcher/gqlgen@v0.6.0/codegen/templates/models.gotpl (about)

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