Skip to content

Conversation

arnabrahman
Copy link
Contributor

Summary

This PR adds the ability for router composition and sharing context data between routes

Changes

Follows the impmentation provided in the linked issue description.

  • includeRouter method for router composition
  • appendContext method for appending context that is shared with all the routes
  • new context option is set as sharedContext, this is up for debate
  • 100% unit test coverage
  • Documentation added, mostly followed what we have in python documentation
// Schema
type Post {
	id: ID
	title: String
	content: String
	requestId: String
}

type User {
	id: ID
	name: String
	email: AWSEmail
	requestId: String
}

type Query {
	getUsers: [User]
	getPosts: [Post]
}

schema {
	query: Query
}
//postRouter.ts
import { Router } from '@aws-lambda-powertools/event-handler/appsync-graphql';

const postRouter = new Router();

postRouter.onQuery('getPosts', async (args, { sharedContext }) => {
  const requestId = sharedContext?.get('requestId');
  return [{ id: 1, title: 'First post', content: 'Hello world!', requestId }];
});

export { postRouter };
// userRouter.ts
import { Router } from '@aws-lambda-powertools/event-handler/appsync-graphql';

const userRouter = new Router();

userRouter.onQuery('getUsers', async (args, { sharedContext }) => {
  const requestId = sharedContext?.get('requestId');
  return [{ id: 1, name: 'John Doe', email: '[email protected]', requestId }];
});

export { userRouter };
// app.ts
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
import { Logger } from '@aws-lambda-powertools/logger';
import type { Context } from 'aws-lambda';
import { userRouter } from './userRouter.js';
import { postRouter } from './todoRouter.js';

const logger = new Logger({ serviceName: 'GraphQLAPI' });
const app = new AppSyncGraphQLResolver({ logger });

app.includeRouter([userRouter, postRouter]);

export const handler = async (event: unknown, context: Context) => {
  app.appendContext({ 
    isAdmin: true, 
    requestId: 'request-123',
    timestamp: Date.now()
  });
  
  return app.resolve(event, context);
};
query MyQuery {
  getUsers {
    id,
    name,
    email,
    requestId
  }
}
{
  "data": {
    "getUsers": [
      {
        "id": "1",
        "name": "John Doe",
        "email": "[email protected]",
        "requestId": "request-123"
      }
    ]
  }
}
query MyQuery {
  getPosts {
    id,
    title,
    content,
    requestId
  }
}
{
  "data": {
    "getPosts": [
      {
        "id": "1",
        "title": "First post",
        "content": "Hello world!",
        "requestId": "request-123"
      }
    ]
  }
}

Issue number: closes #4131


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/XXL PRs with 1K+ LOC, largely documentation related label Sep 10, 2025
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation event-handler This item relates to the Event Handler Utility tests PRs that add or change tests labels Sep 10, 2025
* @param handler - The exception handler function.
*/
private registerErrorHandler(
#registerErrorHandler(
Copy link
Contributor Author

@arnabrahman arnabrahman Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to follow the convention of private methods in this project. I missed this in my last PR.

@arnabrahman arnabrahman marked this pull request as ready for review September 10, 2025 03:58
@arnabrahman arnabrahman marked this pull request as draft September 10, 2025 03:58
@dreamorosi
Copy link
Contributor

We have updated the linting rules of the project to make them a bit stricter - this should make reviewing contributions easier on both sides since you can now catch issues both with pre-commit hooks and CI here on the PR.

You might need to rebase and address the issues in the new code to get the CI green.

@arnabrahman arnabrahman force-pushed the 4131-graphql-includerouter branch from 1c4929f to 2b40997 Compare October 6, 2025 12:24
Copy link

sonarqubecloud bot commented Oct 6, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation event-handler This item relates to the Event Handler Utility size/XXL PRs with 1K+ LOC, largely documentation related tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add includeRouter and context sharing support to AppSync GraphQL resolver
2 participants