Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const postsSubscription = gql`
- subscription postAdded {
- postAdded {
- id
- title
- description
- author{
- name
- }
- }
- }
- `
- 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
- }
- Post: {
- author: async({ authorId }, req, { users }) => {
- const Key = { id: authorId }
- const { Item } = await users.get({ Key })
- return Item
- }
- }
- createPost: async (root, { input }, { posts, users }) => {
- const Key = { id: '3b1884b8-9ee7-4d9d-ab2f-ff32bcd69b9a' }
- const user = await users.get({ Key })
- const Item = {
- id: uuid.v4(),
- author: user.Item,
- ...input
- }
- await posts.create({ Item })
- await pubsub.publish(POST_ADDED_TOPIC, { [POST_ADDED_TOPIC]: Item })
- return { post: Item }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement