-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
--------- Co-authored-by: Kirill Grigorev <[email protected]>
- Loading branch information
Showing
5 changed files
with
110 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/vektah/gqlparser/v2/gqlerror" | ||
) | ||
|
||
// DropTypeHintsFromErrorMessage removes suggested types from the error message | ||
func DropTypeHintsFromErrorMessage(_ context.Context, err error) *gqlerror.Error { | ||
var gqlError *gqlerror.Error | ||
|
||
if errors.As(err, &gqlError) { | ||
if gqlError.Rule == "FieldsOnCorrectType" { | ||
// Error message of this type exposes actual GraphQL types via suggestions: e.g.: Cannot query field "Commerce_Cart" on type "Query". Did you mean "Commerce_Product" or "Commerce_Customer"? | ||
// We are dropping all sentences after first full stop sign (dot) | ||
parts := strings.SplitAfter(gqlError.Message, ".") | ||
if len(parts) > 0 { | ||
gqlError.Message = parts[0] | ||
} | ||
} | ||
|
||
return gqlError | ||
} | ||
|
||
return gqlerror.Wrap(err) | ||
} |
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,71 @@ | ||
package graphql_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vektah/gqlparser/v2/gqlerror" | ||
|
||
"flamingo.me/graphql" | ||
) | ||
|
||
func Test_DropTypeHintsFromErrorMessage(t *testing.T) { | ||
t.Parallel() | ||
|
||
type args struct { | ||
ctx context.Context | ||
err error | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want *gqlerror.Error | ||
}{ | ||
{ | ||
name: "error message for validation rule without specific presenter is returned as is", | ||
args: args{ | ||
err: &gqlerror.Error{ | ||
Message: "some message", | ||
Rule: "NoUnusedVariables", | ||
}, | ||
}, | ||
want: &gqlerror.Error{ | ||
Message: "some message", | ||
Rule: "NoUnusedVariables", | ||
}, | ||
}, | ||
{ | ||
name: `error message for validation rule "FieldsOnCorrectType" is trimmed to prevent exposing correct types`, | ||
args: args{ | ||
err: &gqlerror.Error{ | ||
Message: `Cannot query field "Commerce_Cart" on type "Query". Did you mean "Commerce_Product" or "Commerce_Customer"?`, | ||
Rule: "FieldsOnCorrectType", | ||
}, | ||
}, | ||
want: &gqlerror.Error{ | ||
Message: `Cannot query field "Commerce_Cart" on type "Query".`, | ||
Rule: "FieldsOnCorrectType", | ||
}, | ||
}, | ||
{ | ||
name: `Non-GraphQL error is wrapped as GraphQL error`, | ||
args: args{ | ||
err: fmt.Errorf("some random error"), | ||
}, | ||
want: &gqlerror.Error{ | ||
Err: fmt.Errorf("some random error"), | ||
Message: `some random error`, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equalf(t, tt.want, graphql.DropTypeHintsFromErrorMessage(tt.args.ctx, tt.args.err), "DropTypeHintsFromErrorMessage(%v, %v)", tt.args.ctx, tt.args.err) | ||
}) | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"testing" | ||
|
||
"flamingo.me/flamingo/v3/framework/config" | ||
|
||
"flamingo.me/graphql" | ||
) | ||
|
||
|