github.com/tobiash/gqlgen@v0.5.1/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  	{{- else }}
    16  		type {{.GoType}} struct {
    17  			{{- range $field := .Fields }}
    18  				{{- with .Description}}
    19  					{{.|prefixLines "// "}}
    20  				{{- end}}
    21  				{{- if $field.GoFieldName }}
    22  					{{ $field.GoFieldName }} {{$field.Signature}} `json:"{{$field.GQLName}}"`
    23  				{{- else }}
    24  					{{ $field.GoFKName }} {{$field.GoFKType}}
    25  				{{- end }}
    26  			{{- end }}
    27  		}
    28  	{{- end }}
    29  {{- end}}
    30  
    31  {{ range $enum := .Enums }}
    32  	{{with .Description}}{{.|prefixLines "// "}} {{end}}
    33  	type {{.GoType}} string
    34  	const (
    35  	{{- range $value := .Values}}
    36  		{{- with .Description}}
    37  			{{.|prefixLines "// "}}
    38  		{{- end}}
    39  		{{$enum.GoType}}{{ .Name|toCamel }} {{$enum.GoType}} = {{.Name|quote}}
    40  	{{- end }}
    41  	)
    42  
    43  	func (e {{.GoType}}) IsValid() bool {
    44  		switch e {
    45  		case {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ $enum.GoType }}{{ $element.Name|toCamel }}{{end}}:
    46  			return true
    47  		}
    48  		return false
    49  	}
    50  
    51  	func (e {{.GoType}}) String() string {
    52  		return string(e)
    53  	}
    54  
    55  	func (e *{{.GoType}}) UnmarshalGQL(v interface{}) error {
    56  		str, ok := v.(string)
    57  		if !ok {
    58  			return fmt.Errorf("enums must be strings")
    59  		}
    60  
    61  		*e = {{.GoType}}(str)
    62  		if !e.IsValid() {
    63  			return fmt.Errorf("%s is not a valid {{.GQLType}}", str)
    64  		}
    65  		return nil
    66  	}
    67  
    68  	func (e {{.GoType}}) MarshalGQL(w io.Writer) {
    69  		fmt.Fprint(w, strconv.Quote(e.String()))
    70  	}
    71  
    72  {{- end }}