Search code examples
aws-lambdagraphqlaws-appsync

How to create GraphQL Child Resolvers with Lambda


I'm trying to create a mutation that calls a child resolver in addition to the parent resolver if an optional parameter is sent in.

I'm using AWS AppSync to sent my queries to Lambda. AppSync creates and sends an AppSyncEvent to my resolver file that looks something like this:

{
"info": {
        "parentTypeName": "Mutation",
        "selectionSetList": [
            ...
        ],
        "selectionSetGraphQL": "...",
        "fieldName": "updateUser",
        "variables": {}
    }
}

This event gets passed to my lambda function where, based on the fieldName and parentTypeName, I call my updateUser function.

I have the below schema

schema {
  query: Query
  mutation: Mutation
}

type Query {
  getUser(id: ID!): User
}

type Mutation {
  updateUser(name: String, email: String, bookRead: BookReadInput): User
}

type User {
   name: String
   email: String
   booksRead: [Book]
}

type Book {
   title: String
   author: String
}

type BookReadInput {
   title: String
   author: String
}

I want that if the mutation gets passed bookRead then it will know to call a child resolver called addBook besides for the regular updateUser resolver.

I've seen various articles about implementing child resolvers but I can't figure out how they can work with lambda and the way my resolvers work.


Solution

  • The lambda could inspect the selectionSetList and decide what to do with the BookReadInput fields. See https://aws.amazon.com/blogs/mobile/appsync-and-the-graphql-info-object/

    You could also go with pipeline resolvers to first update the user, and then add the book.

    I don't think there is a way to have it automated. You need to set it up, one way or the other.