Skip to content

Commit

Permalink
update resolver definition to use channels for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 16, 2018
1 parent d0244d2 commit d4c7f3b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
21 changes: 13 additions & 8 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
)

type Build struct {
PackageName string
Objects Objects
Inputs Objects
Interfaces []*Interface
Imports Imports
QueryRoot *Object
MutationRoot *Object
SchemaRaw string
PackageName string
Objects Objects
Inputs Objects
Interfaces []*Interface
Imports Imports
QueryRoot *Object
MutationRoot *Object
SubscriptionRoot *Object
SchemaRaw string
}

// Bind a schema together with some code to generate a Build
Expand Down Expand Up @@ -48,6 +49,10 @@ func Bind(schema *schema.Schema, userTypes map[string]string, destDir string) (*
b.MutationRoot = b.Objects.ByName(mr.TypeName())
}

if sr, ok := schema.EntryPoints["subscription"]; ok {
b.SubscriptionRoot = b.Objects.ByName(sr.TypeName())
}

// Poke a few magic methods into query
q := b.Objects.ByName(b.QueryRoot.GQLType)
q.Fields = append(q.Fields, Field{
Expand Down
8 changes: 7 additions & 1 deletion codegen/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Object struct {
Satisfies []string
Root bool
DisableConcurrency bool
Stream bool
}

type Field struct {
Expand Down Expand Up @@ -75,7 +76,12 @@ func (f *Field) ResolverDeclaration() string {
res += fmt.Sprintf(", %s %s", arg.GQLName, arg.Signature())
}

res += fmt.Sprintf(") (%s, error)", f.Signature())
result := f.Signature()
if f.Object.Stream {
result = "chan<-" + result
}

res += fmt.Sprintf(") (%s, error)", result)
return res
}

Expand Down
3 changes: 3 additions & 0 deletions codegen/object_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func buildObjects(types NamedTypes, s *schema.Schema, prog *loader.Program) Obje
if name == "mutation" {
objects.ByName(obj.Name).DisableConcurrency = true
}
if name == "subscription" {
objects.ByName(obj.Name).Stream = true
}
}

sort.Slice(objects, func(i, j int) bool {
Expand Down
3 changes: 3 additions & 0 deletions templates/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func NewExecutor(resolvers Resolvers) func(context.Context, string, string, map[
{{- if .MutationRoot}}
} else if op.Type == query.Mutation {
data = c._{{.MutationRoot.GQLType|lcFirst}}(op.Selections, nil)
{{- end}}{{ if .SubscriptionRoot}}
} else if op.Type == query.Subscription {
data = c._{{.SubscriptionRoot.GQLType|lcFirst}}(op.Selections, nil)
{{- end}}
} else {
return []*errors.QueryError{errors.Errorf("unsupported operation type")}
Expand Down

0 comments on commit d4c7f3b

Please sign in to comment.