-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make models configurable via template (#2730)
- Loading branch information
Showing
7 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
{{ reserveImport "context" }} | ||
{{ reserveImport "fmt" }} | ||
{{ reserveImport "io" }} | ||
{{ reserveImport "strconv" }} | ||
{{ reserveImport "time" }} | ||
{{ reserveImport "sync" }} | ||
{{ reserveImport "errors" }} | ||
{{ reserveImport "bytes" }} | ||
|
||
{{ reserveImport "github.com/vektah/gqlparser/v2" }} | ||
{{ reserveImport "github.com/vektah/gqlparser/v2/ast" }} | ||
{{ reserveImport "github.com/99designs/gqlgen/graphql" }} | ||
{{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }} | ||
|
||
// Add any new functions or any additional code/template functionality here | ||
|
||
{{- range $model := .Interfaces }} | ||
{{ with .Description }} {{.|prefixLines "// "}} {{ end }} | ||
type {{ goModelName .Name }} interface { | ||
{{- if not .OmitCheck }} | ||
{{- range $impl := .Implements }} | ||
Is{{ goModelName $impl }}() | ||
{{- end }} | ||
Is{{ goModelName .Name }}() | ||
{{- end }} | ||
{{- range $field := .Fields }} | ||
{{- with .Description }} | ||
{{.|prefixLines "// "}} | ||
{{- end}} | ||
Get{{ $field.GoName }}() {{ $field.Type | ref }} | ||
{{- end }} | ||
} | ||
{{- end }} | ||
|
||
{{ range $model := .Models }} | ||
{{with .Description }} {{.|prefixLines "// "}} {{end}} | ||
type {{ goModelName .Name }} struct { | ||
{{- range $field := .Fields }} | ||
{{- with .Description }} | ||
{{.|prefixLines "// "}} | ||
{{- end}} | ||
{{ $field.GoName }} {{$field.Type | ref}} `{{$field.Tag}}` | ||
{{- end }} | ||
} | ||
|
||
{{ range .Implements }} | ||
func ({{ goModelName $model.Name }}) Is{{ goModelName . }}() {} | ||
{{- with getInterfaceByName . }} | ||
{{- range .Fields }} | ||
{{- with .Description }} | ||
{{.|prefixLines "// "}} | ||
{{- end}} | ||
{{ generateGetter $model . }} | ||
{{- end }} | ||
{{- end }} | ||
{{ end }} | ||
{{- end}} | ||
|
||
{{ range $enum := .Enums }} | ||
{{ with .Description }} {{.|prefixLines "// "}} {{end}} | ||
type {{ goModelName .Name }} string | ||
const ( | ||
{{- range $value := .Values}} | ||
{{- with .Description}} | ||
{{.|prefixLines "// "}} | ||
{{- end}} | ||
{{ goModelName $enum.Name .Name }} {{ goModelName $enum.Name }} = {{ .Name|quote }} | ||
{{- end }} | ||
) | ||
|
||
var All{{ goModelName .Name }} = []{{ goModelName .Name }}{ | ||
{{- range $value := .Values}} | ||
{{ goModelName $enum.Name .Name }}, | ||
{{- end }} | ||
} | ||
|
||
func (e {{ goModelName .Name }}) IsValid() bool { | ||
switch e { | ||
case {{ range $index, $element := .Values}}{{if $index}},{{end}}{{ goModelName $enum.Name $element.Name }}{{end}}: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (e {{ goModelName .Name }}) String() string { | ||
return string(e) | ||
} | ||
|
||
func (e *{{ goModelName .Name }}) UnmarshalGQL(v interface{}) error { | ||
str, ok := v.(string) | ||
if !ok { | ||
return fmt.Errorf("enums must be strings") | ||
} | ||
|
||
*e = {{ goModelName .Name }}(str) | ||
if !e.IsValid() { | ||
return fmt.Errorf("%s is not a valid {{ .Name }}", str) | ||
} | ||
return nil | ||
} | ||
|
||
func (e {{ goModelName .Name }}) MarshalGQL(w io.Writer) { | ||
fmt.Fprint(w, strconv.Quote(e.String())) | ||
} | ||
|
||
{{- end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
schema: | ||
- "testdata/schema.graphql" | ||
|
||
exec: | ||
filename: out/ignored.go | ||
model: | ||
filename: out/generated.go | ||
model_template: "testdata/customModelTemplate.gotpl" | ||
|
||
models: | ||
ExistingModel: | ||
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingModel | ||
ExistingInput: | ||
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingInput | ||
ExistingEnum: | ||
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingEnum | ||
ExistingInterface: | ||
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingInterface | ||
ExistingUnion: | ||
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingUnion | ||
ExistingType: | ||
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingType | ||
RenameFieldTest: | ||
fields: | ||
badName: | ||
fieldName: GOODnaME | ||
ExtraFieldsTest: | ||
extraFields: | ||
FieldInternalType: | ||
description: "Internal field" | ||
type: github.com/99designs/gqlgen/plugin/modelgen/internal/extrafields.Type | ||
FieldStringPtr: | ||
type: "*string" | ||
FieldInt: | ||
type: "int64" | ||
overrideTags: 'json:"field_int_tag"' | ||
FieldIntSlice: | ||
type: "[]int64" |