umbraco-community / umbraco-graphql

An implementation of GraphQL for Umbraco 8 using GraphQL for .NET.
MIT License
64 stars 32 forks source link

Fix for #7 When trying to convert an item in a collection to a Type s… #48

Closed jsommr closed 5 years ago

jsommr commented 5 years ago

Remove unwanted items when querying with fragments

Fixes #7

Query

{
  content {
    atRoot {
      Home {
        _children {
          items {
            _children {
              items {
                ... on Blogpost {
                  _id
                  _name
                  _children {
                    items {
                      _id
                    }
                  }
                  _ancestors {
                    items {
                      ... on Person {
                        _name
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Before:

{
  "data": {
    "content": {
      "atRoot": {
        "Home": [
          {
            "_children": {
              "items": [
                {
                  "_children": {
                    "items": [
                      {},
                      {},
                      {},
                      {},
                      {},
                      {},
                      {},
                      {}
                    ]
                  }
                },
                {
                  "_children": {
                    "items": [
                      {},
                      {},
                      {},
                      {},
                      {}
                    ]
                  }
                },
                {
                  "_children": {
                    "items": [
                      {},
                      {}
                    ]
                  }
                },
                {
                  "_children": {
                    "items": [
                      {
                        "_id": "1124",
                        "_name": "My Blog Post",
                        "_children": {
                          "items": []
                        },
                        "_ancestors": {
                          "items": [
                            {},
                            {}
                          ]
                        }
                      },
                      {
                        "_id": "1125",
                        "_name": "Another one",
                        "_children": {
                          "items": []
                        },
                        "_ancestors": {
                          "items": [
                            {},
                            {}
                          ]
                        }
                      },
                      {
                        "_id": "1126",
                        "_name": "This will be great",
                        "_children": {
                          "items": []
                        },
                        "_ancestors": {
                          "items": [
                            {},
                            {}
                          ]
                        }
                      }
                    ]
                  }
                },
                {
                  "_children": {
                    "items": []
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

After

{
  "data": {
    "content": {
      "atRoot": {
        "Home": [
          {
            "_children": {
              "items": [
                {
                  "_children": {
                    "items": []
                  }
                },
                {
                  "_children": {
                    "items": []
                  }
                },
                {
                  "_children": {
                    "items": []
                  }
                },
                {
                  "_children": {
                    "items": [
                      {
                        "_id": "1124",
                        "_name": "My Blog Post",
                        "_children": {
                          "items": []
                        },
                        "_ancestors": {
                          "items": []
                        }
                      },
                      {
                        "_id": "1125",
                        "_name": "Another one",
                        "_children": {
                          "items": []
                        },
                        "_ancestors": {
                          "items": []
                        }
                      },
                      {
                        "_id": "1126",
                        "_name": "This will be great",
                        "_children": {
                          "items": []
                        },
                        "_ancestors": {
                          "items": []
                        }
                      }
                    ]
                  }
                },
                {
                  "_children": {
                    "items": []
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}
rasmusjp commented 5 years ago

Hi @nerfpops

Thanks for the PR.

That's a nice solution, but I'm not entirely sure if we want to include this, since it makes querying inconsistent with how GraphQL works in general. Also we would need to implement this for every list/connection for it to be consistent and I'm not sure if we can enforce it everywhere.