Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3089 generate populateMany method for explicit_requires #3115

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ func (f *federation) InjectSourceEarly() *ast.Source {
directive @interfaceObject on OBJECT
directive @link(import: [String!], url: String!) repeatable on SCHEMA
directive @override(from: String!, label: String) on FIELD_DEFINITION
directive @policy(policies: [[federation__Policy!]!]!) on
directive @policy(policies: [[federation__Policy!]!]!) on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
directive @requiresScopes(scopes: [[federation__Scope!]!]!) on
directive @requiresScopes(scopes: [[federation__Scope!]!]!) on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
Expand Down Expand Up @@ -366,8 +366,12 @@ func (f *federation) GenerateCode(data *codegen.Data) error {

for name, entity := range requiresEntities {
populator := Populator{
FuncName: fmt.Sprintf("Populate%sRequires", name),
Entity: entity,
Entity: entity,
}
if entity.Multi {
populator.FuncName = fmt.Sprintf("PopulateMany%sRequires", name)
} else {
populator.FuncName = fmt.Sprintf("Populate%sRequires", name)
}

populator.Comment = strings.TrimSpace(strings.TrimLeft(rewriter.GetMethodComment("executionContext", populator.FuncName), `\`))
Expand Down
9 changes: 9 additions & 0 deletions plugin/federation/federation.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,22 @@ func (ec *executionContext) __resolve_entities(ctx context.Context, representati
return err
}

{{ if and (index $options "explicit_requires") $entity.Requires }}
err = ec.PopulateMany{{$entity.Def.Name}}Requires(ctx, {{- if (not $usePointers) -}}&{{- end -}}entities, reps)
if err != nil {
return fmt.Errorf(`populating requires for Entity "{{$entity.Def.Name}}": %w`, err)
}
{{- end }}

for i, entity := range entities {
{{ if not (and (index $options "explicit_requires") $entity.Requires) }}
{{- range $entity.Requires }}
entity.{{.Field.JoinGo `.`}}, err = ec.{{.Type.UnmarshalFunc}}(ctx, reps[i]["{{.Field.Join `"].(map[string]interface{})["`}}"])
if err != nil {
return err
}
{{- end}}
{{- end }}
list[idx[i]] = entity
}
return nil
Expand Down
4 changes: 4 additions & 0 deletions plugin/federation/requires.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
{{- else -}}
// {{.FuncName}} is the requires populator for the {{.Entity.Def.Name}} entity.
{{- end }}
{{- if .Entity.Multi -}}
func (ec *executionContext) {{.FuncName}}(ctx context.Context, entities []*{{.Entity.GetTypeInfo}}, reps []map[string]interface{}) error {
{{- else }}
func (ec *executionContext) {{.FuncName}}(ctx context.Context, entity *{{.Entity.GetTypeInfo}}, reps map[string]interface{}) error {
{{- end }}
{{.Implementation}}
}
{{ end }}
Expand Down
Loading