0

Given the GraphQL Schema:


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

type Mutation {
    addPost(id: ID! author: String! title: String content: String url: String): Post!
    updatePost(id: ID! author: String! title: String content: String url: String ups: Int! downs: Int! expectedVersion: Int!): Post!
    deletePost(id: ID!): Post!
}

type Post {
    id: ID!
    author: String!
    title: String
    content: String
    url: String
    ups: Int
    downs: Int
    version: Int!
}

type Query {
    allPost: [Post]
    getPost(id: ID!): Post
}

type Subscription {
    newPost: Post
}

We see that the addPost expects a Post type. So in the resolver function of addPost, we need to retrieve all the data that form a Post type from database, right? But what we are gonna do in our resolver function is to send a PutItem (ex.) request to Post table in database without getting all the data back from database. Then where do we get the data needed for Post type to pass to the children resolvers?

Is there some misunderstanding for me on how resolvers work?

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
OEurix
  • 393
  • 4
  • 19
  • You're free to do anything you want inside your resolver. If your database [doesn't support returning updated rows](https://stackoverflow.com/questions/11477121), you can always fetch the row after updating it. Or if you don't need the updated object client-side, you can change the return type of the `updatePost` field to something other than `Post`, like a `Boolean` indicating whether the update was successful. It's not really clear what you're asking here. – Daniel Rearden Jul 03 '19 at 01:33
  • That absolutely makes sense, but in this case, the resolver needs to operate on database more than a single query, which is not very efficient for Graphql. – OEurix Jul 03 '19 at 01:38
  • That's an issue with your database, not GraphQL. If you wrote a REST endpoint that updated a row and then returned the updated row, you would have the same issue. Again, even if it's convention, no one is forcing you to have that field return a `Post` type – Daniel Rearden Jul 03 '19 at 01:46
  • Was that the crux of your question? – Daniel Rearden Jul 03 '19 at 01:49
  • Actually Iam using aws appsync, which is basically a graphql server. It uses templates in VTL to parse the graphql requests and the responses from database. When its used with DynamoDB, the putitem operation seems will not receive the new item. That's my confusion, i wonder how they can put Post as the returned type. – OEurix Jul 03 '19 at 01:55

0 Answers0