tinacms / tina-graphql-gateway

Makes it easier to build TinaCMS websites with a GraphQL Layer.
https://tina.io/cloud
Apache License 2.0
6 stars 2 forks source link

Question: Are edges supported? #333

Closed robdodson closed 3 years ago

robdodson commented 3 years ago

I'm a total GraphQL novice, so apologies in advance if this is a silly question 😅

I'm experimenting with using tina-graphql-gateway-cli and eleventy. Following the CLI docs, I was able to create a simple query to list all of my posts, including their authors.

query {
  getPostList {
    data {
      ... on Article_Doc_Data {
        title,
        author {
          data {
            ... on BasicAuthor_Doc_Data {
              name,
              avatar
            }
          }
        }
        _body
      }
    }
  }
}

But I was curious if there was a way to query all of my authors, and list every post associated with them? roughly something like this:

query {
  getAuthorsList(first: 10) {
    edges {
      node {
        data {
          ... on BasicAuthor_Doc_Data {
            posts(first: 10) {
              edges {
                node {
                  data {
                    ... on Article_Doc_Data {
                      title
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

I guess since the schema defines a relationship type, I was imagining I could traverse it in both directions. But maybe I'm thinking about it wrong?

DirtyF commented 3 years ago

:wave: @robdodson, very good question. We do plan to support edges and nodes in the GraphQL query @jeffsee55 could tell you more about it.

On a side note, I'm very curious how you can leverage Tina Content API with Eleventy 👀

jeffsee55 commented 3 years ago

We only support the "belongs_to" path at the moment, and will likely not support the "has_many" portion until we implement a more robust indexing service. It's a priority for us but will be a big chunk of work so it's not likely to land soon. Having said that, we're going to future-proof the API by changing the result of list queries. So even getPostList will have edges:

query {
  getPostList {
    edges {
      node {
         data {
            ... on Article_Doc_Data {
              title
            }
          }
        }
      }
    }
  }
}
robdodson commented 3 years ago

On a side note, I'm very curious how you can leverage Tina Content API with Eleventy 👀

As our site has grown, we've had to build a sort of ad hoc data layer using a combination of Eleventy's _data directory and .11tydata.js files. As an example, for https://web.dev/authors/ we have to loop through every post on the site, and for each post add it to an authors dictionary, and then return that dictionary in a way that eleventy can paginate. I'd much prefer using a standard query language to get that data.

My thinking was that in dev, and when we do our production build, we could fire up the local tina graphql server, and have our queries in the eleventy _data directory.

We only support the "belongs_to" path at the moment, and will likely not support the "has_many" portion until we implement a more robust indexing service.

OK, so if I'm understanding correctly, that means that for now you can query if an article or author belongs to a collection, but you can't query all of the author's relationships back to the articles, correct?

jeffsee55 commented 3 years ago

That's right, @robdodson. What you've done in your first query is the only thing we support at the moment. We'll get there, though!