Skip to content
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

Dynamic Strawberry Types with dynamic fields #3757

Open
2 tasks
cabal-daniel opened this issue Jan 26, 2025 · 2 comments
Open
2 tasks

Dynamic Strawberry Types with dynamic fields #3757

cabal-daniel opened this issue Jan 26, 2025 · 2 comments

Comments

@cabal-daniel
Copy link

Hello, we are exploring Strawberry as an option of our application. We have several DynamoDB single-tenant database deployments for an object say "users". The users table has 10 columns that are the same between each deployment but there are more than 20 other columns that can be different.

For example in Deployment 1 the users table has columns/fields:

  • friends
  • last_logged_on

Deployment 2 might have

  • siblings
  • cousins

Therefore we have to determine the schema for Strawberry at runtime. Is there any documentation on this? We've tried several options suggested through GPT and looked into "hacking" into the StrawberryDefinitions class. The problem is that the return type of a query field must be compatible with Strawberry

@strawberry.type
class Query:

    @strawberry.field
    def hello(self) -> str:
        return "Hello"

    @strawberry.field
    def all_users(self) -> typing.List[FullUser]:
        return []

    @strawberry.field
    def get_dynamic_types(self) -> Any:
       return []

The last get_dynamic_types is incompatible as Any is not supported.

Feature Request Type

  • Core functionality
  • [X ] Alteration (enhancement/optimization) of existing feature(s)
  • New behavior

Description

@cabal-daniel
Copy link
Author

Looks like there's many an opportunity to override the Schema class?

class CustomSchema(strawberry.Schema):
    def get_fields(
        self, type_definition: StrawberryObjectDefinition
    ) -> List[StrawberryField]:
        if type_definition.name == 'FullUser':
            type_definition.fields.append(
                strawberry.field(name='custom_field', resolver=custom_field_callback)
            )
        return type_definition.fields

@cabal-daniel
Copy link
Author

It looks like the above works but will require a restart of the server if the schema changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant