<p>I dug into the database and looked for differences in the <code>entries</code> table between an entry that <em>was</em> showing in GraphQL queries and an entry that was <em>not</em> showing in GraphQL queries.</p>
<p>The culprit was assigning a wrong <code>typeId</code> 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 <code>typeId</code>s correctly my new programmatic entries are showing in GraphQL as expected.</p>
<p>Thanks to @user9975 for hanging with me so I was not alone out here.</p>

CC BY-SA 4.0
2021-09-28T16:03:37.157Z
Glad you got it resolved
user9975
CC BY-SA 4.0
2021-09-28T16:08:58.790Z
Now I know... and knowing is half the battle.
Kirk Roberts
CC BY-SA 4.0
2021-09-28T15:53:15.010Z
2021-09-28T15:53:15.010Z
Kirk Roberts
<p>I am using a module controller to create Entries, like this:</p>
<pre><code>$personEvent = new Entry();
$personEvent-&gt;sectionId = 6;
$personEvent-&gt;typeId = 1;
$personEvent-&gt;authorId = $user-&gt;id;
$personEvent-&gt;enabled = true;
$personEvent-&gt;setFieldValues([
              'persons' =&gt; [$person-&gt;id],
              'events' =&gt; [$event-&gt;id],
              'group' =&gt; @$participant['group'],
              'leader' =&gt; @$participant['leader'],
          ]);
$success = Craft::$app-&gt;elements-&gt;saveElement($personEvent);
</code></pre>
<p>This works as expected.</p>
<p>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.</p>
<p>How can I make programmatically created elements available to GraphQL?</p>
<p><strong>EDIT:</strong> Updated the title/question, as it seems like it should be default behavior that GraphQL can &quot;see&quot; programmatically created Entries, so I must be missing something.</p>
<p><strong>EDIT 2:</strong> I should have mentioned that I have already invalidated the GraphQL data caches and set <code>'enableGraphqlCaching' =&gt; false</code> in config/general.php, just in case that would make a difference.</p>
<p><strong>EDIT 3:</strong> Added the <code>setFieldValues</code> portion of the code. Previously I was using the older <code>$entry-&gt;customField = 'foo'</code> approach. Updating to <code>setFieldValues</code> 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.</p>

CC BY-SA 4.0
2021-09-23T22:01:34.987Z
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?
Kirk Roberts
CC BY-SA 4.0
2021-09-27T15:29:45.307Z
Added another edit, hoping someone will give me a tip.
Kirk Roberts
CC BY-SA 4.0
2021-09-27T16:15:23.270Z
is it same with 'entries { id }' query?
user9975
CC BY-SA 4.0
2021-09-27T18:01:34.423Z
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")
Kirk Roberts
CC BY-SA 4.0
2021-09-27T19:15:34.873Z
*I meant when I search by id in GraphiQL.
Kirk Roberts
CC BY-SA 4.0
2021-09-28T12:24:59.973Z
i tested this with a craft site and programmatically added entries showed up on result too. aren't you changing query result with Gql events like EVENT_BEFORE_EXECUTE_GQL_QUERY? and are you sure that missed entry is propagated to the site which your requested via Gql query?
user9975
CC BY-SA 4.0
2021-09-28T13:27:22.680Z
Thanks for checking it out. Before I saw your reply I tried doing a command line `php craft resave/entries` and that did not help.
Kirk Roberts
CC BY-SA 4.0
2021-09-28T13:32:51.927Z
The programmatic entries are definitely present in the CP in the Entries area, but they do not show up in GraphiQL with an `entries` query. If I use the CP to create an entry in the same section that entry DOES appear in GraphiQL. I don't know what you mean by "aren't you changing query result with Gql events like EVENT_BEFORE_EXECUTE_GQL_QUERY?" so... no, I'm not doing that. I'm using the Module.php file that ships with Craft and the example code from `https://docs.craftcms.com/api/v3/craft-services-elements.html#public-methods`
Kirk Roberts
CC BY-SA 4.0
2021-09-28T14:14:40.123Z
I set up a new install with a stripped-down test case and was able to programmatically save an entry AND access it in a GraphiQL query. So there is some problem with my other Craft install and/or my expanded code. But... what? I am having trouble even knowing where to look and what to try to troubleshoot this. It is Craft 3.7.13 and was initially created as 3.7.12 (or close to it). Any tips on what to look at as to why an element (entry) might not be available to GraphQL may be helpful!
Kirk Roberts
<entries><graphql>
Why can't I see my programmatically-created Entries in GraphQL/GraphiQL?
# Why can't I see my programmatically-created Entries in GraphQL/GraphiQL?

(Asked by: username_0 on 2021-09-21)

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.

**Tags:** entries, graphql
> **Comments:**
> **username_0**: 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?  
> **username_0**: Added another edit, hoping someone will give me a tip.  
> **username_1**: is it same with 'entries { id }' query?  
> **username_0**: 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")  
> **username_0**: *I meant when I search by id in GraphiQL.  
> **username_1**: i tested this with a craft site and programmatically added entries showed up on result too. aren't you changing query result with Gql events like EVENT_BEFORE_EXECUTE_GQL_QUERY? and are you sure that missed entry is propagated to the site which your requested via Gql query?  
> **username_0**: Thanks for checking it out. Before I saw your reply I tried doing a command line `php craft resave/entries` and that did not help.  
> **username_0**: The programmatic entries are definitely present in the CP in the Entries area, but they do not show up in GraphiQL with an `entries` query. If I use the CP to create an entry in the same section that entry DOES appear in GraphiQL. I don't know what you mean by "aren't you changing query result with Gql events like EVENT_BEFORE_EXECUTE_GQL_QUERY?" so... no, I'm not doing that. I'm using the Module.php file that ships with Craft and the example code from `https://docs.craftcms.com/api/v3/craft-services-elements.html#public-methods`  
> **username_0**: I set up a new install with a stripped-down test case and was able to programmatically save an entry AND access it in a GraphiQL query. So there is some problem with my other Craft install and/or my expanded code. But... what? I am having trouble even knowing where to look and what to try to troubleshoot this. It is Craft 3.7.13 and was initially created as 3.7.12 (or close to it). Any tips on what to look at as to why an element (entry) might not be available to GraphQL may be helpful!  

---

## 1 answers

### Answer by username_0 on 2021-09-28. Score: 1 | Accepted answer

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 `typeId`s 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.

> **Comments:**
> **username_1**: Glad you got it resolved  
> **username_0**: Now I know... and knowing is half the battle.  

---

