GraphQL订阅传递数据时仅一个参数返回null问题排查
action Returns Null Got it, let's figure out why your action field is coming back null while the rest of the data works. The root issue is a mismatch between the payload structure you're publishing and what your subscription schema expects.
Let's Break Down the Problem
First, look at your schema:
- The
Subscriptiontype has abookingfield that returns aSubscriptionDataobject. SubscriptionDatarequires abooking(of typeBooking!) and optionally anactionstring.
Your subscription resolver listens to the "booking" channel, which means when you publish to that channel, the payload needs to be wrapped in a key that matches the subscription field name (booking), and inside that key should be the full SubscriptionData object (with both booking and action).
Right now, your publish code is doing this:
pubsub.publish("booking", { booking: { booking }, action: "test" });
Here, you're putting action at the same level as the outer booking key—so the subscription resolver doesn't see it as part of the SubscriptionData object. That's why action is null.
The Fix
Adjust your publish payload to nest both booking and action inside the outer booking key, matching the structure your schema expects:
pubsub.publish("booking", { booking: { booking: booking, action: "test" } });
Verify It Works
With this change, when your frontend subscription runs, the subscriptionData.data.booking will correctly contain both the booking object and the action string ("test"). Your existing frontend query is already set up to request both fields, so no changes are needed there.
If you're still having issues, double-check:
- That your resolver is correctly returning the async iterator for the "booking" channel
- That the
bookingvariable in your publish code is a validBookingobject (since your schema marks it as non-null)
内容的提问来源于stack exchange,提问作者Sebastian Berg

