ardatan / graphql-tools

:wrench: Utility library for GraphQL to build, stitch and mock GraphQL schemas in the SDL-first approach
https://www.graphql-tools.com
MIT License
5.35k stars 815 forks source link

buildOperationNodeForField returns wrong result #3380

Closed yurtaev closed 3 years ago

yurtaev commented 3 years ago

Describe the bug

buildOperationNodeForField returns wrong result if return a field of type Query from mutation

To Reproduce

I created #3381 with tests to reproduce that.

# Schema

type Pizza {
  dough: String!
  toppings: [String!]
  query: Query
}
type Salad {
  ingredients: [String!]!
  query: Query
}
union Food = Pizza | Salad
type Query {
  pizza: Pizza
  getPizzaById(id: String!): Pizza
}
type Mutation {
  addRandomFood(name: String!): Food
}
const document = buildOperationNodeForField({
    schema,
    kind: 'mutation',
    field: 'addRandomFood',
    models,
    ignore: [],
  })!;

buildOperationNodeForField returns:

- mutation addRandomFood_mutation($name: String!) {
+ mutation addRandomFood_mutation($name: String!, $addRandomFood_query_query_getPizzaById_id: String!) {
    addRandomFood(name: $name) {
      ... on Pizza {
        dough
        toppings
      }
      ... on Salad {
        ingredients
+       query {
+         pizza {
+           dough
+           toppings
+         }
+         getPizzaById(id: $addRandomFood_query_query_getPizzaById_id) {
+           dough
+           toppings
+         }
+       }
      }
    }
  }

Expected behavior

should return:

mutation addRandomFood_mutation($name: String!) {
  addRandomFood(name: $name) {
    ... on Pizza {
      dough
      toppings
    }
    ... on Salad {
      ingredients
    }
  }
}

Environment:

Additional context

yurtaev commented 3 years ago

Ok, I found how to fix it #3383

P.S I found the bug when trying to use this rule https://graphql-rules.com/rules/mutation-payload-query

ardatan commented 3 years ago

Thanks for the PR :) Available in the latest version!