使用Graphql订阅时如何处理嵌套查询

时间:2019-05-06 08:39:27

标签: graphql graphql-subscriptions

我正在尝试使用Graphql订阅来订阅其他用户在我进入主屏幕时可能创建的新推文,它总是返回给我未定义的用户属性,而Graphql并不清楚为什么这实际上在发生我应该从哪里开始调试

模式文件

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
  }
`;

推特解析器

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)
    }

在客户端,这是我要获取的内容

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

编辑:  未定义的用户属性显示在浏览器上,而我无法在graphiql上为非可空字段返回null graphiql vs browser

0 个答案:

没有答案