如何将连接的值传递给订阅?

时间:2017-07-21 17:11:40

标签: graphql apollo react-apollo apollo-client

我试图返回包含以下内容的订阅:

const postsSubscription = gql`
  subscription postAdded {
    postAdded {
      id
      title
      description
      author{
        name
      }
    }
  }
`

发生的是Author是User的类型,我只是传递了authorId。这意味着我在创建帖子时没有作者姓名:

    createPost: async (root, req, { posts }) => {
      const Item = {
        id: uuid.v4(),
        authorId: '565dbdc0-36f2-4bba-be67-c126d0c71fff',
        ...req
      }
      await posts.create({ Item })

      pubsub.publish('postAdded', { postAdded: Item })

      return Item
    },

以下是作者解析器:

Post: {
    author: async({ authorId }, req, { users }) => {
      const Key = { id: authorId }
      const { Item } = await users.get({ Key })

      return Item
    }
  }

这是架构:

type Post {
  id: ID
  title: String
  description: String
  author: User @relation(name: "PostAuthor")
}

type User {
  id: ID
  name: String
  email: String
  password: String
  posts: [Post] @relation(name: "UserPosts")
}

type PostPayload {
  post: Post
}

type CreateUserPayload {
  user: User
}

type Query {
  allPosts: [Post]
  allUsers: [User]
  post(id: ID!): Post
  user(id: ID!): User
}

type Mutation {
  createPost(input: CreatePostInput!): PostPayload
  updatePost(input : UpdatePostInput!): PostPayload
  createUser(input : CreateUserInput!): CreateUserPayload
}

type Subscription {
  postAdded: Post
}

input CreatePostInput {
  title: String!
  description: String!
}


input UpdatePostInput {
  id: ID!
  title: String!,
  description: String!
}

input CreateUserInput {
  name: String!
  email: String!
  password: String!
}

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

所以,我的问题是,如何将所有必填字段(包括连接)传递给订阅?

1 个答案:

答案 0 :(得分:0)

我做到了,但不是我想要的。 1)我不得不删除这部分代码:

CODE          |QTY
--------------|---------
123           |13
124           |3

并将此函数添加到createPost函数本身:

Post: {
  author: async({ authorId }, req, { users }) => {
    const Key = { id: authorId }
    const { Item } = await users.get({ Key })

    return Item
  }
}

所以这有点固定。但是,如果你知道如何解决这个问题,那么使用第一种方法(Post:author thingy),我会很感激。