Skip to content

Conversation

arnabrahman
Copy link
Contributor

@arnabrahman arnabrahman commented Sep 10, 2025

Summary

This PR adds the ability for router composition

Changes

Follows the implementation provided in the linked issue description. Removed context sharing as discussed

  • includeRouter method for router composition
  • 100% unit test coverage
  • Documentation added, mostly followed what we have in python documentation
// Schema
type Post {
	id: ID
	title: String
	content: String
}

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

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) => {
  return [{ id: 1, title: 'First post', content: 'Hello world!' }];
});

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

const userRouter = new Router();

userRouter.onQuery('getUsers', async (args) => {
  return [{ id: 1, name: 'John Doe', email: 'john@example.com' }];
});

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) => {
  return app.resolve(event, context);
};
query MyQuery {
  getUsers {
    id,
    name,
    email
  }
}
{
  "data": {
    "getUsers": [
      {
        "id": "1",
        "name": "John Doe",
        "email": "john@example.com"
      }
    ]
  }
}
query MyQuery {
  getPosts {
    id,
    title,
    content,
  }
}
{
  "data": {
    "getPosts": [
      {
        "id": "1",
        "title": "First post",
        "content": "Hello world!"
      }
    ]
  }
}

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
@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
@dreamorosi
Copy link
Contributor

@sdangol / @svozza can you please follow this PR while I'm away?

Been chatting with @arnabrahman on Discord and he's blocked with a question we need to answer in #4131 - after this he's probably going to be able to move forward.

@pull-request-size pull-request-size bot added size/XL PRs between 500-999 LOC, often PRs that grown with feedback and removed size/XXL PRs with 1K+ LOC, largely documentation related labels Oct 12, 2025
@arnabrahman arnabrahman changed the title feat(event-handler): Add includeRouter and context sharing support to AppSync GraphQL resolver feat(event-handler): Add includeRouter support to AppSync GraphQL resolver Oct 12, 2025
@arnabrahman arnabrahman changed the title feat(event-handler): Add includeRouter support to AppSync GraphQL resolver feat(event-handler): Add includeRouter support to AppSync GraphQL resolver Oct 12, 2025
@arnabrahman arnabrahman marked this pull request as ready for review October 12, 2025 04:22
@svozza
Copy link
Contributor

svozza commented Oct 12, 2025

One small comment but other than that LGTM!

@arnabrahman arnabrahman requested a review from svozza October 12, 2025 12:20
@svozza
Copy link
Contributor

svozza commented Oct 13, 2025

Fantastic work as always @arnabrahman!

Copy link

@svozza svozza merged commit ada48bb into aws-powertools:main Oct 13, 2025
34 checks passed
@arnabrahman arnabrahman deleted the 4131-graphql-includerouter branch October 13, 2025 09:56
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/XL PRs between 500-999 LOC, often PRs that grown with feedback 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

3 participants