Example queries

My queries are becoming more complex, so I thought I’d share some for examples. Some of the groupBy and filters can get hairy, but with a little practice and using the API explorer it wasn’t too bad.

In Agile fashion, this query gets all Features (below Epic, above Story), and groups and sorts by Epic. It only returns items with stories that have a status < 5 (before done).

const GET_FEATURES = gql`
  query Features {
    featuresList(groupBy: {
      query: {
        epic: {
          title: {
            as: "epicTitle"
          }
          id: {
            as: "epicId"
          }
          sortOrder: {
            as: "epicSortOrder"
          }
        }
        _group: {
          as: "features"
        }
      }, sort: {
        alias: "epicSortOrder", direction: ASC
      }
    }, sort: {
      sortOrder: ASC
    }, filter: {
      AND: [
        {
          product: {
            productId: {
              equals: "QUX"
            }
          }
        }
        {
          stories: {
            every: {
              status: {
                value: {
                  lt: 5
                }
              }
            }
          }
        }
      ]
    }) {
      groups {
        epicId: ID
        epicSortOrder: String
        epicTitle: String
        features: FeatureGroup {
          items {
            id
            title
            milestone {
              title
            }
            estimate {
              title
              points
            }
          }
        }
      }
    }
  }
`;

This is a search page which ANDs a bunch of fields to filter down the list of stories. There’s some React, but most if it just JSON. The keywords string is split by spaces into separate ANDs.

const GET_STORIES = gql`
  query StoriesList(
    $filter: StoryFilter!
  ) {
    storiesList(filter: $filter, sort: {
      sortOrder: ASC
    }) {
      items {
        ${fragments.story}
      }
    }
  }
`;

const [getData, { data }] = useLazyQuery(GET_STORIES);

useEffect(() => {
  getData({
    variables: {
      filter: {
        product: {
          productId: {
            equals: params.productId
          }
        },
        ...(searchKeywords && {
          AND: searchKeywords.split(' ').map(keyword => ({
            title: {
              contains: keyword
            }
          }))
        }),
        ...(searchStatus !== undefined && {
          status: {
            value: {
              equals: searchStatus
            }
          }
        }),
      }
    }
  });
}, [productId, searchKeywords, searchStatus]);

Thank’s for sharing, @mike_ekim1024.