CraftCMS - CraftQL querying globals always returns error

72
February 12, 2019, at 9:00 PM

When querying globals from the GraphiQl interface, from my frontend application, or from an API explorer like Insomnia, CraftQL returns an error Trying to get property of non-object.

I am running Craft 3.1.8 and CraftQL 1.3.1.

query getGlobalTest {
  globals {
    ... on GlobalsSet {
      test {
        ... on Test {
          test
        }
      }
    }
  }
}

The error occurs in vendor/markhuot/craftql/src/Types/Query.php on line 215.

foreach ($setIds as $id) {
  $set = \Craft::$app->globals->getSetById($id, $siteId);
  $sets[$set->handle] = $set;  <-- Error line
}

This happens when querying anything in globals. I read somewhere that this has to do with Craft updating to UUIDs in Craft 3.1, however, I am not sure.

Answer 1

That error just means your getSetById() query returned nothing. You wouldn't pass a UUID to that either. It expects an int. Are you sure your $siteId variable is correct? If you want to avoid errors, you could add this to your code:

foreach ($setIds as $id) {
    if ($set = \Craft::$app->globals->getSetById($id, $siteId)) {
        $sets[$set->handle] = $set;
    }
}

But I would suggest taking a look a the $siteId, or removing it altogether.

READ ALSO
Sending email over SMTP with sendgrid

Sending email over SMTP with sendgrid

I have a html fieldset and a short php file for sending an email via sendgrid when the submit button is clicked

56
bulk insert working in ssms but not working while executing from php

bulk insert working in ssms but not working while executing from php

I'm trying to insert the data from a text file into my databaseI'm using SQL Server Management Studio 17

67
Laravel/LaraCSV - Global Variable in beforeEach

Laravel/LaraCSV - Global Variable in beforeEach

I have a function in my Controller where I'm trying to export acsv file using LaraCSV

60
Cannot get difference between two timestamps

Cannot get difference between two timestamps

I want return the number of dates which have a difference less than 30 minutesThese are the records stored in the database:

65