GraphQL: "Subqueries" for recursion?

In building a query for tree-like collections, each of the children need to repeat their parents’ fields in order to get the same data. Assuming all the children have the same schema as their parent, is there a way to put that into GraphQL as a “subquery” or similar?

Right now it looks like:

query MyQuery {
  allMenus(filter: { parent: { exists: "*" } }) {
    id
    menuId
    json
    plaintext
    position
    title
    urlOverride
    reference {
      ... on PressReleaseRecord {
        id
      }
      ... on PostRecord {
        id
      }
      ... on PageRecord {
        id
      }
      ... on MenuRecord {
        id
      }
      ... on LearningResourceRecord {
        id
      }
      ... on HomeRecord {
        id
      }
      ... on ExhibitionRecord {
        id
      }
      ... on EventRecord {
        id
      }
    }

    children {
      id
      menuId
      json
      plaintext
      position
      title
      urlOverride
      reference {
        ... on PressReleaseRecord {
          id
        }
        ... on PostRecord {
          id
        }
        ... on PageRecord {
          id
        }
        ... on MenuRecord {
          id
        }
        ... on LearningResourceRecord {
          id
        }
        ... on HomeRecord {
          id
        }
        ... on ExhibitionRecord {
          id
        }
        ... on EventRecord {
          id
        }
      }
      children {
        id
        menuId
        json
        plaintext
        position
        title
        urlOverride
        reference {
          ... on PressReleaseRecord {
            id
          }
          ... on PostRecord {
            id
          }
          ... on PageRecord {
            id
          }
          ... on MenuRecord {
            id
          }
          ... on LearningResourceRecord {
            id
          }
          ... on HomeRecord {
            id
          }
          ... on ExhibitionRecord {
            id
          }
          ... on EventRecord {
            id
          }
        }
        children {
          id
          menuId
          json
          plaintext
          position
          title
          urlOverride
          reference {
            ... on PressReleaseRecord {
              id
            }
            ... on PostRecord {
              id
            }
            ... on PageRecord {
              id
            }
            ... on MenuRecord {
              id
            }
            ... on LearningResourceRecord {
              id
            }
            ... on HomeRecord {
              id
            }
            ... on ExhibitionRecord {
              id
            }
            ... on EventRecord {
              id
            }
          }
        }
      }
    }
  }
}

I’m hoping to simplify it to something like:

query MyQuery {
  allMenus(filter: { parent: { exists: "*" } }) {
  {{subQuery}}
 children {
    {{subQuery}}
    children {
      {{subQuery}}
    }
  }
}

Is something like that possible?

Figured it out. That’s what GraphQL Fragments are for:

query MyQuery {
  allMenus(filter: {parent: {exists: "*"}}) {
    ...menuFields
    children {
      ...menuFields
    }
  }
}

fragment menuFields on MenuRecord {
  id
  menuId
  json
  plaintext
  position
  title
  urlOverride
  reference {
    ... on PressReleaseRecord {
      id
    }
    ... on PostRecord {
      id
    }
    ... on PageRecord {
      id
    }
    ... on MenuRecord {
      id
    }
    ... on LearningResourceRecord {
      id
    }
    ... on HomeRecord {
      id
    }
    ... on ExhibitionRecord {
      id
    }
    ... on EventRecord {
      id
    }
  }
}

That was easy!

2 Likes