Skip to content

Commit

Permalink
always return OperationContext for postpone process
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Nov 28, 2019
1 parent 14dbf1a commit 275f6fe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion graphql/context_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func GetRequestContext(ctx context.Context) *RequestContext {
}

func GetOperationContext(ctx context.Context) *OperationContext {
if val, ok := ctx.Value(operationCtx).(*OperationContext); ok {
if val, ok := ctx.Value(operationCtx).(*OperationContext); ok && val != nil {
return val
}
panic("missing operation context")
Expand Down
3 changes: 2 additions & 1 deletion graphql/executable_schema_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions graphql/handler/apollotracing/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"testing"

"github.com/99designs/gqlgen/graphql/handler/apollotracing"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/testserver"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/gqlerror"
)

func TestApolloTracing(t *testing.T) {
Expand Down Expand Up @@ -48,7 +51,24 @@ func TestApolloTracing(t *testing.T) {
require.EqualValues(t, "Query", tracing.Execution.Resolvers[0].ParentType)
require.EqualValues(t, "name", tracing.Execution.Resolvers[0].FieldName)
require.EqualValues(t, "String!", tracing.Execution.Resolvers[0].ReturnType)
}

func TestApolloTracing_withFail(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.POST{})
h.Use(extension.AutomaticPersistedQuery{Cache: lru.New(100)})
h.Use(apollotracing.Tracer{})

resp := doRequest(h, "POST", "/graphql", `{"operationName":"A","extensions":{"persistedQuery":{"version":1,"sha256Hash":"338bbc16ac780daf81845339fbf0342061c1e9d2b702c96d3958a13a557083a6"}}}`)
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
b := resp.Body.Bytes()
t.Log(string(b))
var respData struct {
Errors gqlerror.List
}
require.NoError(t, json.Unmarshal(b, &respData))
require.Equal(t, 1, len(respData.Errors))
require.Equal(t, "PersistedQueryNotFound", respData.Errors[0].Message)
}

func doRequest(handler http.Handler, method string, target string, body string) *httptest.ResponseRecorder {
Expand Down
8 changes: 4 additions & 4 deletions graphql/handler/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (e executor) CreateOperationContext(ctx context.Context, params *graphql.Ra

for _, p := range e.operationParameterMutators {
if err := p.MutateOperationParameters(ctx, params); err != nil {
return nil, gqlerror.List{err}
return rc, gqlerror.List{err}
}
}

Expand All @@ -136,18 +136,18 @@ func (e executor) CreateOperationContext(ctx context.Context, params *graphql.Ra
var listErr gqlerror.List
rc.Doc, listErr = e.parseQuery(ctx, &rc.Stats, params.Query)
if len(listErr) != 0 {
return nil, listErr
return rc, listErr
}

rc.Operation = rc.Doc.Operations.ForName(params.OperationName)
if rc.Operation == nil {
return nil, gqlerror.List{gqlerror.Errorf("operation %s not found", params.OperationName)}
return rc, gqlerror.List{gqlerror.Errorf("operation %s not found", params.OperationName)}
}

var err *gqlerror.Error
rc.Variables, err = validator.VariableValues(e.server.es.Schema(), rc.Operation, params.Variables)
if err != nil {
return nil, gqlerror.List{err}
return rc, gqlerror.List{err}
}
rc.Stats.Validation.End = graphql.Now()

Expand Down

0 comments on commit 275f6fe

Please sign in to comment.