Recipient Creation Bug: shopId Remains Null
Brief Summary
When creating a recipient record in this Gadget Shopify app, the shopId foreign key field is saved as null in the database despite being set correctly in the action code. This causes the error "Cannot return null for non-nullable field Recipient.shop" when trying to view or edit recipients. After 9 different attempted fixes using various Gadget framework approaches (applyParams, save(), api.internal, manual field setting, relationship manipulation), the issue persists, suggesting a fundamental framework-level bug with how belongsTo fields marked as parent relationships are handled in create actions.
Attached is a detailed doc explaining what we tried so far
16 Replies
Any thoughts on this?
Could you please share a link to the action file that you're working on and I'll be able to take a look soon
Sure
https://shop-siren--development.gadget.app/edit/development/files/api/models/recipient/actions/create.ts
This is the action file we've been working on for all 9 attempts to fix the shopId null issue.
the issue affects the recipient model's create action specifically, where the shop belongsTo field (which has parent={"model":"shopifyShop"} metadata) is not properly persisting the shopId foreign key to the database despite being set correctly in code.
Are you making a request to this action from the Shopify admin (embedded UI)?
Yes - we are making the request from the Shopify embedded admin UI. The request originates from our frontend form at web/routes/_app.recipients.new.tsx, which is embedded in the Shopify Admin using Shopify's App Bridge. The form uses AutoForm to call api.recipient.create, and the user is authenticated via Shopify's OAuth session. The shop context is retrieved from connections.shopify.currentShopId, which is populated by Shopify's session token authentication.
Mind if I make a new environment and tear into the code?
Sure Please go ahead
Do let me know if you were able to make any progress
Thanks
Could you please try it now? I just made some changes to your code
Its in my environment
Hi Antoine,
I tried installing the app from the shop-siren-antoine-dev environment on my Shopify dev store (aiadmin-test.myshopify.com), but I’m still getting an Internal Server Error after the install attempt.
Here’s what I’ve already verified and tried:
The App URL and Redirect URL in Gadget and the Shopify Partner Dashboard match exactly.
The Shopify connection shows as active but still says “Install app.”
The Client ID matches correctly.
I deleted and reconnected the app, re-entering the Client ID and Secret Key.
I also tried uninstalling and reinstalling several times, without success.

I just tried somethign else, I wanted to see what change you made to api/models/recipient/actions/create.ts and understand what you did to address the issue.
Changes you made:
Restored the standard framework flow by calling applyParams(params, record) followed immediately by await preventCrossShopDataAccess(params, record) at the start of the run function
Changed all validation logic to read from record instead of params.recipient (since applyParams copies data to record)
Removed all manual attempts to set record.shop or record.shopId
Removed the api.internal.recipient.create() approach
Removed the try-catch wrapper around the action body
Used the standard await save(record) to persist the data
Moved the messaging provider initialization to the top level
We understand the key insight: preventCrossShopDataAccess is designed to automatically set the shop relationship from the authenticated Shopify session context, and we should let the framework handle this instead of trying to set it manually.
We applied these exact changes to our development environment, but we're still receiving the same error:
Unable to load recipient
Error details: [GraphQL] Cannot return null for non-nullable field Recipient.shop.
Recipient ID: 87
The recipient is created successfully (gets an ID, sends invitation emails), but the shopId field remains null in the database, causing the error when trying to view or edit the recipient.
What else have you tried for debugging? Note that the way that I've set it up should work
Hi
We've tried many other ways but all failed - you can see it all detailed in the doc at the first post on this thread
it's very frustrating since we were planning to launch and have all our marketing standing by since last week
I'm willing to do everything it takes to solve this as soon as possible
I don't think that the issue is necessarily with the action code. I need more information from you as to what you're seeing. TraceIds, reproduction steps, values you're sending from the frontend (the printed logs), the name of the route (frontend) that's making the request, an example store to test on in the previewer, etc
Sure
Reproduction Steps:
Navigate to /recipients/new in the Shopify embedded admin
Fill out the form:
First Name: "Test"
Email: "test@example.com"
Channels: Select "Email"
Click "Add Recipient"
Click on the newly created recipient to view it
Error occurs: "Cannot return null for non-ifiable field Recipient.shop"
Frontend Route Making the Request: web/routes/_app.recipients.new.tsx
Values Sent from Frontend:
JavaScript
{
recipient: {
firstName: "Test",
email: "test@example.com",
channels: ["email"]
}
}
(No shop field is sent from the frontend)
Recent Trace IDs:
2de1fe77bd94c660cfcd7cbfcbe0b173 (Recipient ID 87, most recent)
8a08cd721c7d1350490b83bef56f4740 (Earlier attempt)
What We're Seeing:
Recipient is created successfully with an ID
Email invitation sends successfully
But when querying the recipient, shopId is null in the database
This causes the GraphQL error when trying to view the recipient
We have three demo stores conencted
you can use this one
https://admin.shopify.com/store/aiadmin-demo
--
I'm standing by for any further info you might need
Thanks again
I just made a new recipient to your model with the form and the shop is linked:
id 89
Did this help you folks?It did Antoine.
Thansk for your help again