Skip to content

Commit

Permalink
chore: fix execution normalization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devsergiy committed Nov 11, 2024
1 parent d019a2c commit 714b83a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
3 changes: 1 addition & 2 deletions execution/engine/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ func TestExtractor_ExtractFieldsFromRequest(t *testing.T) {
graphql.NewExtractor().ExtractFieldsFromRequest(&request, schema, &report, fields)

expectedFields := graphql.RequestTypes{
"Foo": {"fooField": {}},
"Post": {"description": {}, "id": {}, "user": {}},
"Query": {"foo": {}, "posts": {}},
"Query": {"posts": {}},
"User": {"id": {}, "name": {}},
}

Expand Down
7 changes: 5 additions & 2 deletions execution/graphql/normalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ func (r *Request) Normalize(schema *Schema, options ...astnormalization.Option)
}
}

normalizer := astnormalization.NewWithOpts(options...)

if r.OperationName != "" {
options = append(options, astnormalization.WithRemoveNotMatchingOperationDefinitions())
normalizer := astnormalization.NewWithOpts(options...)
normalizer.NormalizeNamedOperation(&r.document, &schema.document, []byte(r.OperationName), &report)
} else {
// TODO: we should validate count of operations - to throw an error
// and do full normalization for the single anonymous operation
normalizer := astnormalization.NewWithOpts(options...)
normalizer.NormalizeOperation(&r.document, &schema.document, &report)
}

Expand Down
29 changes: 7 additions & 22 deletions execution/graphql/normalization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestRequest_Normalize(t *testing.T) {
t.Run("should successfully normalize request with fragments", func(t *testing.T) {
schema := StarwarsSchema(t)
request := StarwarsRequestForQuery(t, starwars.FileFragmentsQuery)
request.OperationName = "Fragments"
documentBeforeNormalization := request.document

result, err := request.Normalize(schema)
Expand Down Expand Up @@ -125,27 +126,11 @@ func TestRequest_Normalize(t *testing.T) {
request := StarwarsRequestForQuery(t, starwars.FileMultiQueriesWithArguments)
request.OperationName = "GetDroid"

runNormalization(t, &request, `{"a":"1"}`, `query GetDroid($a: ID!){
runNormalization(t, &request, `{"a":"1"}`,
`query GetDroid($a: ID!){
droid(id: $a){
name
}
}
query Search {
search(name: "C3PO"){
... on Droid {
name
primaryFunction
}
... on Human {
name
height
}
... on Starship {
name
length
}
}
}`)
})

Expand All @@ -154,9 +139,9 @@ query Search {
request := Request{
OperationName: "charactersByIds",
Variables: stringify(map[string]interface{}{"a": 1}),
Query: `query ($a: [Int]) { charactersByIds(ids: $a) { name }}`,
Query: `query charactersByIds($a: [Int]) { charactersByIds(ids: $a) { name }}`,
}
runNormalizationWithSchema(t, schema, &request, `{"a":[1]}`, `query($a: [Int]){
runNormalizationWithSchema(t, schema, &request, `{"a":[1]}`, `query charactersByIds($a: [Int]){
charactersByIds(ids: $a){
name
}
Expand Down Expand Up @@ -184,9 +169,9 @@ query Search {
Variables: stringify(map[string]interface{}{
"ids": 1,
}),
Query: `query($ids: [Int]) {charactersByIds(ids: $ids) { name }}`,
Query: `query charactersByIds($ids: [Int]) {charactersByIds(ids: $ids) { name }}`,
}
runNormalizationWithSchema(t, schema, &request, `{"ids":[1]}`, `query($ids: [Int]){
runNormalizationWithSchema(t, schema, &request, `{"ids":[1]}`, `query charactersByIds($ids: [Int]){
charactersByIds(ids: $ids){
name
}
Expand Down

0 comments on commit 714b83a

Please sign in to comment.