Best approach for efficiency fetching from multiple databases #2228
Unanswered
DenyWatanabe
asked this question in
General
Replies: 1 comment 3 replies
-
@DenyWatanabe you probably want to define resolvers on your def dynamo_resolver(myObject: myObject, info: Info) -> str:
return db.getItem("myTable", {"id": myObject.id}, info.field_name)
@strawberry.federation.type
class myObject:
id: strawberry.ID
attr1: str = strawberry.field(resolver=dynamo_resolver)
attr2: str = strawberry.field(resolver=dynamo_resolver)
attr3: str = strawberry.field(resolver=neptune_resolver)
attr4: str = strawberry.field(resolver=redshift_resolver)
@strawberry.federation.type
class Query:
@strawberry.field
def myResolver(self, id: strawberry.ID, info:Info) -> myObject
return myObject(id=id) That way you'll only resolve the attributes that were requested by the client. To prevent multiple DB queries you can use a dataloader for each type of resolver: # Cache data loaders
DYNAMO_LOADERS = {}
def get_dynamo_dataloader(id: str):
if id in DYNAMO_LOADERS:
return DYNAMO_LOADERS[id]
async def load_from_dynamo(keys: List[str]) -> List[str]:
result_map = db.getItem("myTable", {"id": id}, ",".join(keys))
return [result_map[key] for key in keys]
return DataLoader(load_fn=load_from_dynamo)
async def dynamo_resolver(myObject: myObject, info: Info) -> str:
loader = get_dynamo_dataloader(myObject.id)
return loader.load(info.field_name)
@strawberry.federation.type
class myObject:
id: strawberry.ID
attr1: str = strawberry.field(resolver=dynamo_resolver)
attr2: str = strawberry.field(resolver=dynamo_resolver)
attr3: str = strawberry.field(resolver=neptune_resolver)
attr4: str = strawberry.field(resolver=redshift_resolver)
@strawberry.federation.type
class Query:
@strawberry.field
def myResolver(self, id: strawberry.ID, info:Info) -> myObject
return myObject(id=id) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I need to build an app which fetches data from multiple fields from multiple databases (DynamoDB, Neptune and Redshift). The app will likely receive tens of thousands of requests per second, so it needs to be as efficient as possible.
So my design idea is as follows:
I'm currently stuck in the last step, how to request data from multiple databases and then resolve them to a single object, assigning only its attributes which were requested and returned from the database calls.
creating a single resolver which returns Object(attr1=value1, attr2=value2, ...) doesn't seem the best option as not all attributes will be returned every time.
I have considered an extremely ugly solution but there must be a better way:
Important requirements:
What would be the best design/implementation approach in my case?
Beta Was this translation helpful? Give feedback.
All reactions