Skip to content

Commit

Permalink
Fix Complexity case selection
Browse files Browse the repository at this point in the history
Use GraphQL field name rather than the Go field name.
  • Loading branch information
mbranch committed Apr 12, 2019
1 parent 5ff6092 commit 7a9b085
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 13 deletions.
2 changes: 1 addition & 1 deletion codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
{{ if not $object.IsReserved }}
{{ range $field := $object.UniqueFields }}
{{ if not $field.IsReserved }}
case "{{$object.Name}}.{{$field.GoFieldName}}":
case "{{$object.Name}}.{{$field.Name}}":
if e.complexity.{{$object.Name|go}}.{{$field.GoFieldName}} == nil {
break
}
Expand Down
107 changes: 96 additions & 11 deletions integration/generated.go

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

23 changes: 23 additions & 0 deletions integration/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ describe('Input defaults', () => {
});
});

describe('Complexity', () => {
it('should fail when complexity is too high', async () => {
let res;
try {
res = await client.query({
query: gql`{ complexity(value: 200) }`,
});
} catch (err) {
expect(err.networkError.statusCode).toEqual(422);
}
expect(res).toBe(undefined);
});

it('should succeed when complexity is not too high', async () => {
let res = await client.query({
query: gql`{ complexity(value: 100) }`,
});

expect(res.data.complexity).toBe(true);
expect(res.errors).toBe(undefined);
});
});

describe('Errors', () => {
it('should respond with correct paths', async () => {
let res = await client.query({
Expand Down
4 changes: 4 additions & 0 deletions integration/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (r *queryResolver) JSONEncoding(ctx context.Context) (string, error) {
return "\U000fe4ed", nil
}

func (r *queryResolver) Complexity(ctx context.Context, value int) (bool, error) {
return true, nil
}

type userResolver struct{ *Resolver }

func (r *userResolver) Likes(ctx context.Context, obj *remote_api.User) ([]string, error) {
Expand Down
1 change: 1 addition & 0 deletions integration/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Query {
viewer: Viewer
jsonEncoding: String!
error(type: ErrorType = NORMAL): Boolean!
complexity(value: Int!): Boolean!
}

enum ErrorType {
Expand Down
9 changes: 8 additions & 1 deletion integration/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ func main() {
port = defaultPort
}

cfg := integration.Config{Resolvers: &integration.Resolver{}}
cfg.Complexity.Query.Complexity = func(childComplexity, value int) int {
// Allow the integration client to dictate the complexity, to verify this
// function is executed.
return value
}
http.Handle("/", handler.Playground("GraphQL playground", "/query"))
http.Handle("/query", handler.GraphQL(
integration.NewExecutableSchema(integration.Config{Resolvers: &integration.Resolver{}}),
integration.NewExecutableSchema(cfg),
handler.ErrorPresenter(func(ctx context.Context, e error) *gqlerror.Error {
if e, ok := errors.Cause(e).(*integration.CustomError); ok {
return &gqlerror.Error{
Expand All @@ -33,6 +39,7 @@ func main() {
}
return graphql.DefaultErrorPresenter(ctx, e)
}),
handler.ComplexityLimit(100),
))

log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
Expand Down

0 comments on commit 7a9b085

Please sign in to comment.