Skip to content

Commit

Permalink
Fix pointer returns from directive
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Jun 30, 2019
1 parent 21b6511 commit 28207f0
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 8 deletions.
4 changes: 4 additions & 0 deletions codegen/args.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func (ec *executionContext) {{ $name }}(ctx context.Context, rawArgs map[string]
}
if data, ok := tmp.({{ $arg.TypeReference.GO | ref }}) ; ok {
arg{{$i}} = data
{{- if $arg.TypeReference.IsNilable }}
} else if tmp == nil {
arg{{$i}} = nil
{{- end }}
} else {
return nil, fmt.Errorf(`unexpected type %T from directive, should be {{ $arg.TypeReference.GO }}`, tmp)
}
Expand Down
5 changes: 5 additions & 0 deletions codegen/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
if data, ok := tmp.({{ .TypeReference.GO | ref }}) ; ok {
return data, nil
}
{{- if .TypeReference.IsNilable -}}
else if tmp == nil {
return nil, nil
}
{{- end }}
return nil, fmt.Errorf(`unexpected type %T from directive, should be {{ .TypeReference.GO }}`, tmp)
{{- else -}}
ctx = rctx // use context from middleware stack in children
Expand Down
4 changes: 4 additions & 0 deletions codegen/input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
}
if data, ok := tmp.({{ $field.TypeReference.GO | ref }}) ; ok {
it.{{$field.GoFieldName}} = data
{{- if $field.TypeReference.IsNilable }}
} else if tmp == nil {
it.{{$field.GoFieldName}} = nil
{{- end }}
} else {
return it, fmt.Errorf(`unexpected type %T from directive, should be {{ $field.TypeReference.GO }}`, tmp)
}
Expand Down
8 changes: 7 additions & 1 deletion codegen/testserver/directive.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ directive @logged(id: UUID!) on FIELD

extend type Query {
directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String
directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): String
directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): ObjectDirectives
directiveInputNullable(arg: InputDirectives): String
directiveInput(arg: InputDirectives!): String
directiveInputType(arg: InnerInput! @custom): String
Expand All @@ -15,6 +15,7 @@ extend type Query {

input InputDirectives {
text: String! @length(min: 0, max: 7, message: "not valid")
nullableText: String @length(min: 0, max: 7, message: "not valid")
inner: InnerDirectives!
innerNullable: InnerDirectives
thirdParty: ThirdParty @length(min: 0, max: 7)
Expand All @@ -23,3 +24,8 @@ input InputDirectives {
input InnerDirectives {
message: String! @length(min: 1, message: "not valid")
}

type ObjectDirectives {
text: String! @length(min: 0, max: 7, message: "not valid")
nullableText: String @length(min: 0, max: 7, message: "not valid")
}
225 changes: 221 additions & 4 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 28207f0

Please sign in to comment.