-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial impl for nats data source * fix null data value * fix escaping problem with multiline comments in the sdl * add rootField to BaseDataSource * add addr and topic literals * update nats datasource to use directive definitions * add logging to nats data source * fix dependency injection * add missing copy value * add cleanup and additional logging * fix err logging
- Loading branch information
Showing
13 changed files
with
760 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package execution | ||
|
||
import ( | ||
"github.com/jensneuse/graphql-go-tools/pkg/ast" | ||
"github.com/jensneuse/graphql-go-tools/pkg/astvisitor" | ||
"github.com/jensneuse/graphql-go-tools/pkg/lexer/literal" | ||
"github.com/nats-io/nats.go" | ||
"go.uber.org/zap" | ||
"io" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type NatsDataSourcePlanner struct { | ||
BaseDataSourcePlanner | ||
} | ||
|
||
func NewNatsDataSourcePlanner(baseDataSourcePlanner BaseDataSourcePlanner) *NatsDataSourcePlanner { | ||
return &NatsDataSourcePlanner{ | ||
BaseDataSourcePlanner: baseDataSourcePlanner, | ||
} | ||
} | ||
|
||
func (n *NatsDataSourcePlanner) DirectiveName() []byte { | ||
return []byte("NatsDataSource") | ||
} | ||
|
||
func (n *NatsDataSourcePlanner) DirectiveDefinition() []byte { | ||
data, _ := n.graphqlDefinitions.Find("directives/nats_datasource.graphql") | ||
return data | ||
} | ||
|
||
func (n *NatsDataSourcePlanner) Plan() (DataSource, []Argument) { | ||
return &NatsDataSource{ | ||
log: n.log, | ||
}, n.args | ||
} | ||
|
||
func (n *NatsDataSourcePlanner) Initialize(walker *astvisitor.Walker, operation, definition *ast.Document, args []Argument, resolverParameters []ResolverParameter) { | ||
n.walker, n.operation, n.definition, n.args = walker, operation, definition, args | ||
} | ||
|
||
func (n *NatsDataSourcePlanner) EnterInlineFragment(ref int) { | ||
|
||
} | ||
|
||
func (n *NatsDataSourcePlanner) LeaveInlineFragment(ref int) { | ||
|
||
} | ||
|
||
func (n *NatsDataSourcePlanner) EnterSelectionSet(ref int) { | ||
|
||
} | ||
|
||
func (n *NatsDataSourcePlanner) LeaveSelectionSet(ref int) { | ||
|
||
} | ||
|
||
func (n *NatsDataSourcePlanner) EnterField(ref int) { | ||
n.rootField.setIfNotDefined(ref) | ||
} | ||
|
||
func (n *NatsDataSourcePlanner) LeaveField(ref int) { | ||
if !n.rootField.isDefinedAndEquals(ref) { | ||
return | ||
} | ||
definition, exists := n.walker.FieldDefinition(ref) | ||
if !exists { | ||
return | ||
} | ||
directive, exists := n.definition.FieldDefinitionDirectiveByName(definition, n.DirectiveName()) | ||
if !exists { | ||
return | ||
} | ||
value, exists := n.definition.DirectiveArgumentValueByName(directive, literal.ADDR) | ||
if !exists { | ||
return | ||
} | ||
variableValue := n.definition.StringValueContentBytes(value.Ref) | ||
arg := &StaticVariableArgument{ | ||
Name: literal.ADDR, | ||
Value: make([]byte, len(variableValue)), | ||
} | ||
copy(arg.Value, variableValue) | ||
n.args = append(n.args, arg) | ||
|
||
value, exists = n.definition.DirectiveArgumentValueByName(directive, literal.TOPIC) | ||
if !exists { | ||
return | ||
} | ||
variableValue = n.definition.StringValueContentBytes(value.Ref) | ||
arg = &StaticVariableArgument{ | ||
Name: literal.TOPIC, | ||
Value: make([]byte, len(variableValue)), | ||
} | ||
copy(arg.Value, variableValue) | ||
n.args = append(n.args, arg) | ||
} | ||
|
||
type NatsDataSource struct { | ||
log *zap.Logger | ||
conn *nats.Conn | ||
sub *nats.Subscription | ||
once sync.Once | ||
} | ||
|
||
func (n *NatsDataSource) Resolve(ctx Context, args ResolvedArgs, out io.Writer) Instruction { | ||
var err error | ||
n.once.Do(func() { | ||
|
||
addrArg := args.ByKey(literal.ADDR) | ||
topicArg := args.ByKey(literal.TOPIC) | ||
|
||
addr := nats.DefaultURL | ||
topic := string(topicArg) | ||
|
||
if len(addrArg) != 0 { | ||
addr = string(addrArg) | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
if n.sub != nil { | ||
n.log.Debug("NatsDataSource.unsubscribing", | ||
zap.String("addr", addr), | ||
zap.String("topic", topic), | ||
) | ||
err := n.sub.Unsubscribe() | ||
if err != nil { | ||
n.log.Error("Unsubscribe", zap.Error(err)) | ||
} | ||
} | ||
if n.conn != nil { | ||
n.log.Debug("NatsDataSource.closing", | ||
zap.String("addr", addr), | ||
zap.String("topic", topic), | ||
) | ||
n.conn.Close() | ||
} | ||
}() | ||
|
||
n.log.Debug("NatsDataSource.connecting", | ||
zap.String("addr", addr), | ||
zap.String("topic", topic), | ||
) | ||
|
||
n.conn, err = nats.Connect(addr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
n.log.Debug("NatsDataSource.subscribing", | ||
zap.String("addr", addr), | ||
zap.String("topic", topic), | ||
) | ||
|
||
n.sub, err = n.conn.SubscribeSync(topic) | ||
if err != nil { | ||
panic(err) | ||
} | ||
}) | ||
|
||
if err != nil { | ||
return CloseConnection | ||
} | ||
|
||
message, err := n.sub.NextMsg(time.Minute) | ||
if err != nil { | ||
return CloseConnection | ||
} | ||
|
||
_, err = out.Write(message.Data) | ||
if err != nil { | ||
return CloseConnection | ||
} | ||
|
||
return KeepStreamAlive | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.