2017-07-21 3 views
0

Ich versuche, ein Abonnement zurückzukehren, die aus folgendem besteht:Wie übergibt man die Werte einer Verbindung an ein Abonnement?

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

Was passiert ist, dass Autor Art von Benutzer ist, und ich gehe gerade einen authorid. Das bedeutet, dass ich nicht den Namen des Autors, wenn ich die Post erstellen:

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

Hier ist der Autor Resolver:

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

     return Item 
    } 
    } 

Hier ist das Schema:

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 
} 

So , meine Frage ist, wie alle erforderlichen Felder (einschließlich der Verbindungen) an das Abonnement übergeben werden?

+1

Wird dieser Feld-Resolver aufgerufen? Kannst du eine Reproduktion erstellen oder das Schema teilen? – Urigo

+0

Da gehts los @Urigo Es funktioniert, denn wenn ich die 'allPosts' oder' post' abfrage bekomme ich den Autor zurück. Aber ich kann diese Information nicht im Abonnement erhalten. Es bricht also und sagt, dass es den Autor braucht – KadoBOT

Antwort

0

Ich habe es funktioniert, aber nicht wie ich wollte. 1) hatte ich diesen Teil des Codes zu entfernen:

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

    return Item 
    } 
} 

und hinzugefügt, um diese Funktion auf die erstelleBeitrag Funktion selbst:

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

So ist diese irgendwie fixiert. Aber, wenn Sie wissen, wie man das repariert, den ersten Ansatz verwendend (Post: Autor Thingy) werde ich schätzen.

Verwandte Themen