-
Notifications
You must be signed in to change notification settings - Fork 156
feat(event-handler): add single resolver functionality for AppSync GraphQL API #3999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(event-handler): add single resolver functionality for AppSync GraphQL API #3999
Conversation
…tions for GraphQL events
…resolution details
…e event objects for GraphQL operations
…vent handling and error formatting
…egistration and resolution behavior
…tion and logging behavior
…pattern in `Router` class
…solver` class with examples and parameter descriptions
… RouteHandlerRegistry and Router tests
…phQLResolver and RouteHandlerRegistry
…n RouteHandlerRegistry
…egistry and related tests
Please retry analysis of this Pull-Request directly on SonarQube Cloud |
I have also begun exploring the implementation of the batch resolver. I am trying the powertools-python batch resolver example. Schema: type Post {
post_id: ID!
title: String
author: String
relatedPosts: [Post]
}
type Query {
getPost(post_id: ID!): Post
}
schema {
query: Query
} from __future__ import annotations
from typing import Any, TypedDict
from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
from aws_lambda_powertools.utilities.typing import LambdaContext
app = AppSyncResolver()
logger = Logger()
posts = {
"1": { "post_id": "1", "title": "First book", "author": "Author1", },
"2": { "post_id": "2", "title": "Second book", "author": "Author2", },
"3": { "post_id": "3", "title": "Third book", "author": "Author3", },
"4": { "post_id": "4", "title": "Fourth book", "author": "Author4", },
"5": { "post_id": "5", "title": "Fifth book", "author": "Author5", }
}
posts_related = {
"1": [posts["4"]],
"2": [posts["3"], posts["5"]],
"3": [posts["2"], posts["1"]],
"4": [posts["2"], posts["1"]],
"5": [],
}
def search_batch_posts(posts: list) -> dict[str, Any]:
return {post_id: posts_related.get(post_id) for post_id in posts}
class Post(TypedDict, total=False):
post_id: str
title: str
author: str
@app.resolver(type_name="Query", field_name="getPost")
def get_post(
post_id: str = ""
) -> Post:
return posts.get(post_id, {})
@app.batch_resolver(type_name="Query", field_name="relatedPosts")
def related_posts(event: list[AppSyncResolverEvent]) -> list[Any]:
# Extract all post_ids in order
post_ids: list = [record.source.get("post_id") for record in event]
# Get unique post_ids while preserving order
unique_post_ids = list(dict.fromkeys(post_ids))
# Fetch posts in a single batch operation
fetched_posts = search_batch_posts(unique_post_ids)
# Return results in original order
return [fetched_posts.get(post_id) for post_id in post_ids]
def lambda_handler(event, context: LambdaContext) -> dict:
return app.resolve(event, context) If I try this query query MyQuery {
getPost(post_id: "2") {
relatedPosts {
post_id
author
relatedPosts {
post_id
author
}
}
}
} There is an error: If I change the typeName to Result: {
"data": {
"getPost": {
"relatedPosts": [
{
"post_id": "3",
"author": "Author3",
"relatedPosts": [
{
"post_id": "2",
"author": "Author2"
},
{
"post_id": "1",
"author": "Author1"
}
]
},
{
"post_id": "5",
"author": "Author5",
"relatedPosts": []
}
]
}
}
} The reason is probably because parentTypeName value is Not sure what I am doing wrong here. @dreamorosi @leandrodamascena |
Hi @arnabrahman, thank you so much for the PR. I'm going to need a bit more time to review this, I was out of office yesterday and catching up with things today. I expect to provide a first review tomorrow morning. |
Summary
This PR will add the ability to register single resolvers for AppSync GraphQL API.
Changes
appsync-graphql
utility insideevent-handler
appsync-events
kwargs
. We must match the function signature accordingly. So this is not possible, I resolved the arguments as an object.onQuery
andonMutation
methods for a single resolver. Although this probably improves the developer experience (DX), I feel like it might not be worth it. It could be merged into a singleonResolve
method instead. This way, we can avoid anotherMap
lookup. Need opinion on this.Example usage of the utility: Followed this
Issue number: #1166
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.