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

strawberry scalar of type JSON #2106

Open
devkral opened this issue Mar 18, 2024 · 3 comments
Open

strawberry scalar of type JSON #2106

devkral opened this issue Mar 18, 2024 · 3 comments

Comments

@devkral
Copy link
Contributor

devkral commented Mar 18, 2024

How can I add some scalar like strawberry JSON?

schemathesis always crashes while printing the AST.

is the definition

from strawberry.scalars import JSON

@st.composite
def json_strategy(draw: st.DrawFn):
    return JSON(
        {
            "action": draw(
                st.sampled_from(
                    [
                        "manage",
                        "admin",
                        "view",
                        "create",
                        "delete",
                        "update",
                    ]
                )
            )
        }
    )

wrong? Or there a bug in schemathesis?

I get the error:
TypeError: Invalid AST Node: {'action': 'manage'}.

@Stranger6667
Copy link
Member

Schemathesis uses the graphql package for nodes. It is re-exported bia schemathesis.graphql.nodes. From the docs:

from hypothesis import strategies as st
import schemathesis
from schemathesis.graphql import nodes

schemathesis.graphql.scalar("Date", st.dates().map(nodes.String))

my guess is (based on the relevant js impl) that the actual node type depends on the JSON value type, so should it be wrapped in nodes.Object instead or JSON from strawberry.scalars?

from hypothesis import strategies as st
import schemathesis
from schemathesis.graphql import nodes


@st.composite
def json_strategy(draw: st.DrawFn):
    return {
            "action": draw(
                st.sampled_from(
                    [
                        "manage",
                        "admin",
                        "view",
                        "create",
                        "delete",
                        "update",
                    ]
                )
            )
        }

schemathesis.graphql.scalar("JSON", json_strategy().map(Object))

@devkral
Copy link
Contributor Author

devkral commented Mar 19, 2024

sadly still failing with:
TypeError: Invalid AST Node: {'action': 'manage'}

I wonder what kind of magic the strawberry guys are using

@Stranger6667
Copy link
Member

I’ll check it in more detail, the inner structure of GraphQL nodes should be relatively straightforward. Maybe some inner parts should be wrapped too

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