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

Feature Request: Info context in scalar serialization #3443

Open
1 of 3 tasks
Skeen opened this issue Apr 8, 2024 · 2 comments
Open
1 of 3 tasks

Feature Request: Info context in scalar serialization #3443

Skeen opened this issue Apr 8, 2024 · 2 comments

Comments

@Skeen
Copy link
Member

Skeen commented Apr 8, 2024

Feature Request Type

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

Description

I'm trying to construct a JWT token during serialization with a custom scalar; see the code below:

class _SomeModel(BaseModel):
    a: int
    b: str

SomeModel = strawberry.scalar(
    _SomeModel,
    serialize=lambda v: jwt.encode(jsonable_encoder(v.dict()), "secret"),
    parse_value=lambda v: _SomeModel(**jwt.decode(v, "secret")),
)

The code above uses "secret" for the JWT secret, but I'd like to get this secret value from the info context, aka:

SomeModel = strawberry.scalar(
    _SomeModel,
    serialize=lambda v, info: jwt.encode(jsonable_encoder(v.dict()), info.context["secret"]),
    parse_value=lambda v, info: _SomeModel(**jwt.decode(v, info.context["secret"])),
)

However this requires info to be available to the serialize and parse_value functions.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@Skeen
Copy link
Member Author

Skeen commented Apr 8, 2024

Issue created per request of Patrick on Discord: https://discord.com/channels/689806334337482765/1226914223313911808

@Birdi7
Copy link
Contributor

Birdi7 commented Apr 30, 2024

Hello!
I am wondering if I am able to fulfill the request. However, the question raised.
Is it possible to introduce feature in strawberry alone? I've found that strawberry uses GraphQLScalarType from graphql-core library as well as graphql.execute is used for execution process. So, strawberry is dependent on these definitions from another package. @patrick91 would appreciate your response!

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

2 participants