-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Plugin: ForceResolver #354
Changes from all commits
c2105c5
b9d0d2d
fab2abb
a8225b3
86f984b
41e81ef
9a38f5c
a62b9e7
8aff449
7e13181
6462718
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package codegen | ||
|
||
import ( | ||
"github.com/vektah/gqlparser/ast" | ||
) | ||
|
||
// Plugin is an interface for a gqlgen plugin | ||
type Plugin interface { | ||
Name() string | ||
} | ||
|
||
// PluginConfigurer is an interface a plugin can satisfy in order to make changes to configuration before codegen | ||
type PluginConfigurer interface { | ||
PostNormalize(c *Config, schema *ast.Schema) error | ||
} | ||
|
||
// PluginSchema is an interface a plugin can satisfy if they wish to merge additional schema with the base schema | ||
type PluginSchema interface { | ||
Schema(cfg *Config) (string, error) | ||
} | ||
|
||
// PluginRegistry is a collection of plugins that will be accessed during codegen | ||
type PluginRegistry struct { | ||
plugins []Plugin | ||
} | ||
|
||
// Register a set of plugins to the plugin registry | ||
func (r *PluginRegistry) Register(p ...Plugin) { | ||
r.plugins = append(r.plugins, p...) | ||
} | ||
|
||
func (r *PluginRegistry) schemas(c *Config) (srcs []*ast.Source, err error) { | ||
for _, p := range r.plugins { | ||
name := p.Name() | ||
if p, ok := p.(PluginSchema); ok { | ||
src, err := p.Schema(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
srcs = append(srcs, &ast.Source{Name: name, Input: src}) | ||
} | ||
} | ||
return srcs, err | ||
} | ||
|
||
func (r *PluginRegistry) postNormalize(cfg *Config, schema *ast.Schema) error { | ||
for _, p := range r.plugins { | ||
if p, ok := p.(PluginConfigurer); ok { | ||
err := p.PostNormalize(cfg, schema) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,26 +9,26 @@ import ( | |
"time" | ||
) | ||
|
||
type resolver struct { | ||
type chat struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
Rooms map[string]*Chatroom | ||
mu sync.Mutex // nolint: structcheck | ||
} | ||
|
||
func (r *resolver) Mutation() MutationResolver { | ||
func (r *chat) Mutation() MutationResolver { | ||
return &mutationResolver{r} | ||
} | ||
|
||
func (r *resolver) Query() QueryResolver { | ||
func (r *chat) Query() QueryResolver { | ||
return &queryResolver{r} | ||
} | ||
|
||
func (r *resolver) Subscription() SubscriptionResolver { | ||
func (r *chat) Subscription() SubscriptionResolver { | ||
return &subscriptionResolver{r} | ||
} | ||
|
||
func New() Config { | ||
return Config{ | ||
Resolvers: &resolver{ | ||
Resolvers: &chat{ | ||
Rooms: map[string]*Chatroom{}, | ||
}, | ||
} | ||
|
@@ -40,7 +40,7 @@ type Chatroom struct { | |
Observers map[string]chan Message | ||
} | ||
|
||
type mutationResolver struct{ *resolver } | ||
type mutationResolver struct{ *chat } | ||
|
||
func (r *mutationResolver) Post(ctx context.Context, text string, username string, roomName string) (Message, error) { | ||
r.mu.Lock() | ||
|
@@ -67,7 +67,7 @@ func (r *mutationResolver) Post(ctx context.Context, text string, username strin | |
return message, nil | ||
} | ||
|
||
type queryResolver struct{ *resolver } | ||
type queryResolver struct{ *chat } | ||
|
||
func (r *queryResolver) Room(ctx context.Context, name string) (*Chatroom, error) { | ||
r.mu.Lock() | ||
|
@@ -81,7 +81,7 @@ func (r *queryResolver) Room(ctx context.Context, name string) (*Chatroom, error | |
return room, nil | ||
} | ||
|
||
type subscriptionResolver struct{ *resolver } | ||
type subscriptionResolver struct{ *chat } | ||
|
||
func (r *subscriptionResolver) MessageAdded(ctx context.Context, roomName string) (<-chan Message, error) { | ||
r.mu.Lock() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want one large interface with lots of methods or lots of small interfaces that can optionally be implemented?
eg we may eventually have codegen hooks here, but most plugins wont use them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lots of 1-function interfaces will be easier to use, then one big.