Newbie GraphQL Questions

I decided it was time to really learn GraphQL and I am having trouble grasping a few things. So I am reaching out for some guidance.

I started out by cloning the RW example blog and when I spin up the local GraphQL “sandbox” I see on the right hand side of the screen a “Docs” tab with a list Queries & Mutations.

If I click on one it gives me what I assume are payload options. However, when I enter the query in the sandbox I get the following errors:

{
  "errors": [
    {
      "message": "The \"PostsSet\" definition is not executable.",
      "locations": [
        {
          "line": 1,
          "column": 1
        }
      ]
    }
  ]
}

What does it mean that “definition is not executable”?

My second question is that if I navigate to api -> scripts -> seed.js I can see a whole slew of data that I would like to write queries for. For example (in the example blog)I see postData with title, slug, author and other fields. I am assuming that postData could be a Root Query Type that I can then traverse the edges “up” and retrieve for example the title(s). So, how do I write a query that grabs that data? I tried the following in the GraphQL sandbox:

type postData {
  title: [String]!
}

but got this error:

{
  "errors": [
    {
      "message": "The \"postData\" definition is not executable.",
      "locations": [
        {
          "line": 1,
          "column": 1
        }
      ]
    }
  ]
}

Any help getting off in the right direction to sort this out would be immensely appreciated. Thank you.

Hi! What you see in the docs area are the definitions of the queries and mutation, along with their types. To run an actual query, you can try this:

  {
    allPosts(page: 1, limit: 10) {
      posts {
        id
        title
        slug
        author
        body
        image
        postedAt
        tags {
          id
          name
        }
      }
      count
    }
  }

This runs the allPosts query passing in page and limit parameters, and then requests the fields from the posts that are returned along with a count of how many total posts there are in the database.

You can see the code that runs to fulfill that request here: example-blog/posts.js at 648cb15139f842449a41f5d9dc3e1dca9287f258 · redwoodjs/example-blog · GitHub

And you can see how that query is executed from the React frontend here: example-blog/PostSummariesCell.js at 648cb15139f842449a41f5d9dc3e1dca9287f258 · redwoodjs/example-blog · GitHub

Let me know if this helps answer your questions!

1 Like

That is super helpful! Thank you, I feel like I am really starting to grok it now :wink: :smiling_face_with_three_hearts:

1 Like