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] Generating JSON nodes in GraphQL #2106

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

[FEATURE] Generating JSON nodes in GraphQL #2106

devkral opened this issue Mar 18, 2024 · 5 comments
Labels
Core: Data Generation Generating test data Priority: Low Cosmetic or low-impact changes Specification: GraphQL Specific to GraphQL

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

@Stranger6667
Copy link
Member

@devkral did you manage to implement it? If so and if you are willing to share the results, I'd be happy yo include it in the docs or maybe even bring some implementation helpers to Schemathesis itself

@Stranger6667 Stranger6667 changed the title strawberry scalar of type JSON [FEATURE] Generating JSON nodes in GraphQL Jul 20, 2024
@Stranger6667 Stranger6667 added Priority: Low Cosmetic or low-impact changes Specification: GraphQL Specific to GraphQL Core: Data Generation Generating test data labels Jul 20, 2024
@devkral
Copy link
Contributor Author

devkral commented Jul 22, 2024

no

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core: Data Generation Generating test data Priority: Low Cosmetic or low-impact changes Specification: GraphQL Specific to GraphQL
Projects
None yet
Development

No branches or pull requests

2 participants