0

I am using a module controller to create Entries, like this:

$personEvent = new Entry();
$personEvent->sectionId = 6;
$personEvent->typeId = 1;
$personEvent->authorId = $user->id;
$personEvent->enabled = true;
$personEvent->setFieldValues([
              'persons' => [$person->id],
              'events' => [$event->id],
              'group' => @$participant['group'],
              'leader' => @$participant['leader'],
          ]);
$success = Craft::$app->elements->saveElement($personEvent);

This works as expected.

When I try to grab the Entries using the GraphiQL explorer they do not appear, despite being shown in the Entries admin panel. If I create an Entry in the same section using the admin panel that Entry can be seen in GraphiQL. The difference seems to be that one was created programmatically while the other was created through the admin panel.

How can I make programmatically created elements available to GraphQL?

EDIT: Updated the title/question, as it seems like it should be default behavior that GraphQL can "see" programmatically created Entries, so I must be missing something.

EDIT 2: I should have mentioned that I have already invalidated the GraphQL data caches and set 'enableGraphqlCaching' => false in config/general.php, just in case that would make a difference.

EDIT 3: Added the setFieldValues portion of the code. Previously I was using the older $entry->customField = 'foo' approach. Updating to setFieldValues does not appear to have made a difference with new programmatically saved entries as far as being able to access them in GraphiQL. Also of note, I am using Full Schema in GraphiQL.

9
  • Hmm... two days with no response, not that I am entitled to one. Is my question lacking important information? Has no one ever encountered this issue? Sep 23 at 22:01
  • Added another edit, hoping someone will give me a tip. Sep 27 at 15:29
  • is it same with 'entries { id }' query?
    – user9975
    Sep 27 at 16:15
  • Yes. If I search by id I can see the Entries created in the CP but not Entries that were programmatically created (and are present in the CP). e.g. entries(id: "240") Sep 27 at 18:01
  • *I meant when I search by id in GraphiQL. Sep 27 at 19:15
0

I dug into the database and looked for differences in the entries table between an entry that was showing in GraphQL queries and an entry that was not showing in GraphQL queries.

The culprit was assigning a wrong typeId when creating the Entry. I had mistakenly been setting them all to 1, which is the Login type (at least it is on my site). Now that I am assigning the typeIds correctly my new programmatic entries are showing in GraphQL as expected.

Thanks to @user9975 for hanging with me so I was not alone out here.

2
  • Glad you got it resolved
    – user9975
    Sep 28 at 16:03
  • Now I know... and knowing is half the battle. Sep 28 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.