Skip to content

Commit

Permalink
feat: Change plugin signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
cliedeman authored and StevenACoffman committed Jun 18, 2024
1 parent 04b13fd commit a2d3d70
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
14 changes: 14 additions & 0 deletions api/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func Generate(cfg *config.Config, option ...Option) error {
cfg.Sources = append(cfg.Sources, s)
}
}
if inj, ok := p.(plugin.EarlySourcesInjector); ok {
s, err := inj.InjectSourcesEarly()
if err != nil {
return fmt.Errorf("%s: %w", p.Name(), err)
}
cfg.Sources = append(cfg.Sources, s...)
}
}

if err := cfg.LoadSchema(); err != nil {
Expand All @@ -70,6 +77,13 @@ func Generate(cfg *config.Config, option ...Option) error {
cfg.Sources = append(cfg.Sources, s)
}
}
if inj, ok := p.(plugin.LateSourcesInjector); ok {
s, err := inj.InjectSourcesLate(cfg.Schema)
if err != nil {
return fmt.Errorf("%s: %w", p.Name(), err)
}
cfg.Sources = append(cfg.Sources, s...)
}
}

// LoadSchema again now we have everything
Expand Down
17 changes: 9 additions & 8 deletions plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (f *federation) MutateConfig(cfg *config.Config) error {
return nil
}

func (f *federation) InjectSourceEarly() *ast.Source {
func (f *federation) InjectSourcesEarly() ([]*ast.Source, error) {
input := ``

// add version-specific changes on key directive, as well as adding the new directives for federation 2
Expand Down 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 All @@ -168,16 +168,17 @@ func (f *federation) InjectSourceEarly() *ast.Source {
scalar federation__Scope
`
}
return &ast.Source{

return []*ast.Source{{
Name: "federation/directives.graphql",
Input: input,
BuiltIn: true,
}
}}, nil
}

// InjectSourceLate creates a GraphQL Entity type with all
// the fields that had the @key directive
func (f *federation) InjectSourceLate(schema *ast.Schema) *ast.Source {
func (f *federation) InjectSourcesLate(schema *ast.Schema) ([]*ast.Source, error) {
f.setEntities(schema)

var entities, resolvers, entityResolverInputDefinitions string
Expand Down Expand Up @@ -259,11 +260,11 @@ type Entity {
}`
blocks = append(blocks, extendTypeQueryDef)

return &ast.Source{
return []*ast.Source{{
Name: "federation/entity.graphql",
BuiltIn: true,
Input: "\n" + strings.Join(blocks, "\n\n") + "\n",
}
}}, nil
}

func (f *federation) GenerateCode(data *codegen.Data) error {
Expand Down
10 changes: 7 additions & 3 deletions plugin/federation/federation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,15 @@ func load(t *testing.T, name string) (*federation, *config.Config) {
}

f := &federation{Version: cfg.Federation.Version}
cfg.Sources = append(cfg.Sources, f.InjectSourceEarly())
s, err := f.InjectSourcesEarly()
require.NoError(t, err)
cfg.Sources = append(cfg.Sources, s...)
require.NoError(t, cfg.LoadSchema())

if src := f.InjectSourceLate(cfg.Schema); src != nil {
cfg.Sources = append(cfg.Sources, src)
l, err := f.InjectSourcesLate(cfg.Schema)
require.NoError(t, err)
if l != nil {
cfg.Sources = append(cfg.Sources, l...)
}
require.NoError(t, cfg.LoadSchema())

Expand Down
12 changes: 12 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ type CodeGenerator interface {
}

// EarlySourceInjector is used to inject things that are required for user schema files to compile.
// Deprecated: Use EarlySourcesInjector instead
type EarlySourceInjector interface {
InjectSourceEarly() *ast.Source
}

// EarlySourcesInjector is used to inject things that are required for user schema files to compile.
type EarlySourcesInjector interface {
InjectSourcesEarly() ([]*ast.Source, error)
}

// LateSourceInjector is used to inject more sources, after we have loaded the users schema.
// Deprecated: Use LateSourcesInjector instead
type LateSourceInjector interface {
InjectSourceLate(schema *ast.Schema) *ast.Source
}
Expand All @@ -35,3 +42,8 @@ type LateSourceInjector interface {
type ResolverImplementer interface {
Implement(prevImplementation string, field *codegen.Field) string
}

// LateSourcesInjector is used to inject more sources, after we have loaded the users schema.
type LateSourcesInjector interface {
InjectSourcesLate(schema *ast.Schema) ([]*ast.Source, error)
}

0 comments on commit a2d3d70

Please sign in to comment.