Skip to content

Commit

Permalink
Merge pull request #287 from matiasanaya/feature/support-query-throug…
Browse files Browse the repository at this point in the history
…h-subscribe

Fix #286: Support queries/mutations through alternative transports
  • Loading branch information
pavelnikolov authored Nov 12, 2018
2 parents 9b57fd8 + 074fe87 commit 995849d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestSchemaSubscribe(t *testing.T) {
},
},
{
Name: "subscribe_to_query_errors",
Name: "subscribe_to_query_succeeds",
Schema: graphql.MustParseSchema(schema, &rootResolver{}),
Query: `
query Hello {
Expand All @@ -139,7 +139,11 @@ func TestSchemaSubscribe(t *testing.T) {
`,
ExpectedResults: []gqltesting.TestResponse{
{
Errors: []*qerrors.QueryError{qerrors.Errorf("%s: %s", "subscription unavailable for operation of type", "QUERY")},
Data: json.RawMessage(`
{
"hello": "Hello world!"
}
`),
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ func (s *Schema) subscribe(ctx context.Context, queryString string, operationNam
return sendAndReturnClosed(&Response{Errors: []*qerrors.QueryError{qerrors.Errorf("%s", err)}})
}

// TODO: Move to validation.Validate?
if op.Type != query.Subscription {
return sendAndReturnClosed(&Response{Errors: []*qerrors.QueryError{qerrors.Errorf("%s: %s", "subscription unavailable for operation of type", op.Type)}})
}

r := &exec.Request{
Request: selected.Request{
Doc: doc,
Expand All @@ -68,6 +63,11 @@ func (s *Schema) subscribe(ctx context.Context, queryString string, operationNam
varTypes[v.Name.Name] = introspection.WrapType(t)
}

if op.Type == query.Query || op.Type == query.Mutation {
data, errs := r.Execute(ctx, res, op)
return sendAndReturnClosed(&Response{Data: data, Errors: errs})
}

responses := r.Subscribe(ctx, res, op)
c := make(chan *Response)
go func() {
Expand Down

0 comments on commit 995849d

Please sign in to comment.