Search code examples
apigraphqlmicroservicesaws-appsync

GraphQL field resolver needs contextual information


Maybe my terminology is not accurate. I'm using AWS AppSync. My schema:

type Book {
  title: String
  author: Author
}

type Author {
  name: String
}

type Query {
  getBook(title:String!): Book
}

The resolver for getBook returns an object shaped as:

{
  title: <string>
  authorId: <number>
}

Where authorId is always returned.

What I'd like to do is specify a resolver for the field Book.author that will receive authorId and fetch that object from its own data store. Is this possible?

If what I'm trying to do is not possible, what is the proper way of doing this, where one data store is a table with two columns - { title, authorId }, and a separate store has a table with a list of authors, where the primary key is a column authorId. Since these are two different services, I can't just join the two like a SQL query.


Solution

  • As long as authorId is returned from the getBook resolver, it will be accessible via $ctx.source.authorId when resolving Book.author.

    I reproduced your API with local resolvers using your schema:

    Query.getBook request mapping template:

    {
        "version": "2018-05-29",
        "payload": {
            "title": "$context.arguments.title",
            "authorId": "2" ## returned in addition to other fields. It will be used by Book.author resolver.
        }
    }
    

    Query.getBook response mapping template:

    $util.toJson($context.result)
    

    Book.author request mapping template:

    {
        "version": "2018-05-29",
        "payload": {
            "name": "author name with authorId: $context.source.authorId"
        }
    }
    

    Book.author response mapping template:

    $util.toJson($context.result)
    

    The following query:

    query {
      getBook(title:"AWS AppSync") {
        title 
        author {
          name
        }
      }
    }
    

    will yield the results:

    {
      "data": {
        "getBook": {
          "title": "AWS AppSync",
          "author": {
            "name": "author name with authorId: 2"
          }
        }
      }
    }