Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. const postsSubscription = gql`
  2. subscription postAdded {
  3. postAdded {
  4. id
  5. title
  6. description
  7. author{
  8. name
  9. }
  10. }
  11. }
  12. `
  13.  
  14. createPost: async (root, req, { posts }) => {
  15. const Item = {
  16. id: uuid.v4(),
  17. authorId: '565dbdc0-36f2-4bba-be67-c126d0c71fff',
  18. ...req
  19. }
  20. await posts.create({ Item })
  21.  
  22. pubsub.publish('postAdded', { postAdded: Item })
  23.  
  24. return Item
  25. },
  26.  
  27. Post: {
  28. author: async({ authorId }, req, { users }) => {
  29. const Key = { id: authorId }
  30. const { Item } = await users.get({ Key })
  31.  
  32. return Item
  33. }
  34. }
  35.  
  36. type Post {
  37. id: ID
  38. title: String
  39. description: String
  40. author: User @relation(name: "PostAuthor")
  41. }
  42.  
  43. type User {
  44. id: ID
  45. name: String
  46. email: String
  47. password: String
  48. posts: [Post] @relation(name: "UserPosts")
  49. }
  50.  
  51. type PostPayload {
  52. post: Post
  53. }
  54.  
  55. type CreateUserPayload {
  56. user: User
  57. }
  58.  
  59. type Query {
  60. allPosts: [Post]
  61. allUsers: [User]
  62. post(id: ID!): Post
  63. user(id: ID!): User
  64. }
  65.  
  66. type Mutation {
  67. createPost(input: CreatePostInput!): PostPayload
  68. updatePost(input : UpdatePostInput!): PostPayload
  69. createUser(input : CreateUserInput!): CreateUserPayload
  70. }
  71.  
  72. type Subscription {
  73. postAdded: Post
  74. }
  75.  
  76. input CreatePostInput {
  77. title: String!
  78. description: String!
  79. }
  80.  
  81.  
  82. input UpdatePostInput {
  83. id: ID!
  84. title: String!,
  85. description: String!
  86. }
  87.  
  88. input CreateUserInput {
  89. name: String!
  90. email: String!
  91. password: String!
  92. }
  93.  
  94. schema {
  95. query: Query
  96. mutation: Mutation
  97. subscription: Subscription
  98. }
  99.  
  100. Post: {
  101. author: async({ authorId }, req, { users }) => {
  102. const Key = { id: authorId }
  103. const { Item } = await users.get({ Key })
  104.  
  105. return Item
  106. }
  107. }
  108.  
  109. createPost: async (root, { input }, { posts, users }) => {
  110. const Key = { id: '3b1884b8-9ee7-4d9d-ab2f-ff32bcd69b9a' }
  111. const user = await users.get({ Key })
  112.  
  113. const Item = {
  114. id: uuid.v4(),
  115. author: user.Item,
  116. ...input
  117. }
  118. await posts.create({ Item })
  119. await pubsub.publish(POST_ADDED_TOPIC, { [POST_ADDED_TOPIC]: Item })
  120.  
  121. return { post: Item }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement