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

feat: Change plugin signatures #2011

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
10 changes: 7 additions & 3 deletions plugin/federation/federation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,15 @@ 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 {
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 @@ -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)
}