timqian / gql-generator

Generate queries from graphql schema, used for writing api test.
MIT License
374 stars 93 forks source link

Fields cross-referencing type results in bug in output schema #51

Closed Davste93 closed 2 years ago

Davste93 commented 2 years ago

Reproducible example:

type Query {
  testQuery(id: ID!): TestQuery
}

type TestQuery {
  foo: [Foo]
  bar: [Bar]
}

type Foo {
  bar: [Bar]
  surname: String
}

type Bar {
  foo: [Foo]
  name: String
}

Output:


query testQuery($id: ID!) {
  testQuery(id: $id) {
    foo {
      bar {
        foo {
          surname
        }
        name
      }
      surname
    }
    bar {
      name
    }
  }
}

Expected output with depthLimit = 3

query testQuery($id: ID!) {
  testQuery(id: $id) {
    foo {
      bar {
        foo {
          surname
        }
        name
      }
      surname
    }
   bar {
      foo {
        bar {
          name
        }
        surname
      }
      name
    }
  }
}

This is without passing in the depth limit flag, but there's the same issue with it anyways. Seems like it's stopping a bit too short as soon as it detects a circular reference, before it even starts expanding them.

Davste93 commented 2 years ago

also updating the root type names (not the type itself) from:


type TestQuery {
  foo: [Foo]
  bar: [Bar]
}

to


type TestQuery {
  foo: [Foo]
  bar2: [Bar]
}

results in a different output structure changing more than just the variable names - seems like more was expanded:

query testQuery($id: ID!) {
  testQuery(id: $id) {
    foo {
      bar {
        foo {
          surname
        }
        name
      }
      surname
    }
    bar2 {
      foo {
        surname
      }
      name
    }
  }
}

Correct me if I am wrong but this tells me that some mapping is happening based on the variable names, not the types

Davste93 commented 2 years ago

I saw this line in your docs just now:

As this tool is used for tests, it expands all of the fields in a query. There might be recursive fields in the query, so gqlg ignores the types which have been added in the parent queries already.

In the above case in my opinion the schema is incorrect, since the same field but nested under another field may be referencing different data entirely, whereas it will just be completely ignored. If you can save me some time to what needs to happen I'd be happy to put a PR in

Davste93 commented 2 years ago

https://github.com/dotansimha/graphql-code-generator/pull/6861/files I switched to using this, that is in alpha but does not have the above bug, as a workaround for the time being

MDrooker commented 2 years ago

Whats the thinking on if/how this should be solved/fixed?