I'm trying to use Graphql-subscriptions to subscribe for new tweets that other users may create while I'm on home screen, It always returns me undefined user property, and Graphql doesn't clarify why is that actually happening nor where shall I start debugging

schema file

export default `
  scalar Date

  type User {
    _id: ID!
    username: String
    email: String!
    firstName: String
    lastName: String
    avatar: String
    createdAt: Date!
    updatedAt: Date!
  }
  type Tweet {
    _id: ID!
    text: String!
    user: User!
    favoriteCount: Int!
    createdAt: Date!
    updatedAt: Date!
  }
  type Query {
    getTweets: [Tweet]
  }
  type Mutation {
    createTweet(text: String!): Tweet
  }
  type Subscription {
    tweetAdded: Tweet!
  }

  schema {
    query: Query
    mutation: Mutation
    subscription: Subscription
  }
`;

tweet resolvers

const TWEET_ADDED = "TWEET_ADDED";

createTweet: async (_, args, { user, pubsub }) => {
        try {
            await requireAuth(user);
            let tweet = await Tweet.create({ ...args, user: user._id });
            tweet = await Tweet.populate(tweet, 'user')
            pubsub.publish(TWEET_ADDED, { [TWEET_ADDED]: tweet });
            return tweet;
        } catch (error) {
            throw error;
        }
    },
tweetAdded: {
        subscribe: (_, __, { pubsub }) => pubsub.asyncIterator(TWEET_ADDED)
    }

and on the client side, here is what I'm trying to fetch

subscription {
  tweetAdded {
    text
    _id
    createdAt
    favoriteCount
    user {
       username
       avatar
       firstName
       lastName
    }
  }
}

EDIT: the undefined user property shows up on the browser while I get cannot return null for non-nullable field on graphiql graphiql vs browser

0 Answers