Skip to content

Commit

Permalink
feat: Change plugin signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
cliedeman committed Mar 1, 2022
1 parent 5236fb0 commit d7994c0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
14 changes: 14 additions & 0 deletions api/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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 @@ -49,6 +56,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
12 changes: 6 additions & 6 deletions plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (f *federation) MutateConfig(cfg *config.Config) error {
return nil
}

func (f *federation) InjectSourceEarly() *ast.Source {
return &ast.Source{
func (f *federation) InjectSourcesEarly() ([]*ast.Source, error) {
return []*ast.Source{{
Name: "federation/directives.graphql",
Input: `
scalar _Any
Expand All @@ -80,12 +80,12 @@ directive @key(fields: _FieldSet!) repeatable on OBJECT | INTERFACE
directive @extends on OBJECT | INTERFACE
`,
BuiltIn: true,
}
}}, nil
}

// InjectSources 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 @@ -157,11 +157,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
}

// Entity represents a federated type
Expand Down
4 changes: 3 additions & 1 deletion plugin/federation/federation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ func load(t *testing.T, name string) (*federation, *config.Config) {
require.NoError(t, err)

f := &federation{}
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 {
Expand Down
12 changes: 12 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ 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
}

// 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 d7994c0

Please sign in to comment.