Skip to content

Commit

Permalink
99designs#3089 generate populateMany method for explicit_requires
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailLipanin committed Jun 2, 2024
1 parent e07134a commit 3a065fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
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

0 comments on commit 3a065fa

Please sign in to comment.