markhuot / craftql

A drop-in GraphQL server for Craft CMS
Other
319 stars 53 forks source link

Query returns blank entries #327

Closed theAdhocracy closed 4 years ago

theAdhocracy commented 4 years ago

When I write a fairly generic query i.e. { entries(){ ... on BlogPosts { id } } } CraftQL is now returning all of the blog posts, but also dozens of additional "blank" entries: image

I'm pretty certain this is relatively new behaviour, but unclear what is causing it. We've not updated the CMS or added any new plugins to cause a change. I am wondering if it has something to do with the way Craft is now treating/autosaving Drafts, but not clear how I would troubleshoot that. At any rate, it seems like an odd behaviour change.

narration-sd commented 4 years ago

Well, my first thought is that this might be a bit too generic a query, if that is what you are actually using. Without having identified a Section for the entries, you're going to get Entries of any type.

If I correct a model of your query to not have the empty parens for entries (not allowed), I will get just the same kind of empty records.

The reason: you're allowing all the Entry types in your Craft, but only providing a fragment that gives data for the BlogPosts type. All the Entries which are not BlogPost will show a null record, because there's nothing provided which queries Fields on them.

Try this (corrected w/no parens) form, moving the id field outside the BlogPosts fragment, and you'll see ids for all the records, just not any fields you add outside which are specific to Blogposts. title exists for all entries, so let's show that as well, and then you'll see where those extra records are coming from, as well as that they all do have ids:

{
   entries {
      id
      title
  }
}

Putting in a qualification for Sectionwill remove the extras you're not expecting:

{
   entries(section: blogposts) { # or your actual enum for BlogPosts...
      id
      title
  }
}

If you then add back in your BlogPosts fragment, and ask for a field that is specific to those (like body?), then you'll see that field showing for BlogPosts -- and only for BlogPosts. And that's the idea.

{
   entries {
      id
      title
      ... on BlogPosts {
        body { # or another field  you know is there
          content
        }
      }
  }
}

Then if you put back in your Section qualifier as wll, you'll see all results but only on BlogPosts as expected.

To conclude, I've run a quick check on a Craft site now at 3.1.13 , but which has been abused through the whole development of Drafts, and as expected, there are no records actually without ids, and as wll there are no Drafts showing in an Entries query for Craft. It is possible to see them, but that's a more complicated story...

narration-sd commented 4 years ago

...couple of missed brackets and one more query example put in - it's early...

theAdhocracy commented 4 years ago

Yep, hit the nail on the head, thanks. This was me focusing too much on the inside of the query and missing something obvious higher up. Thanks for the detailed explanation, I've tried exactly what you've suggested and (of course) it works as expected, removing all the "blank" entries. I also went back and had another look at past/live queries and (again, of course) these are all using some form of qualifier on entries, hence why this appeared to be "new behaviour".