schemathesis icon indicating copy to clipboard operation
schemathesis copied to clipboard

[FEATURE] Generating JSON nodes in GraphQL

Open devkral opened this issue 1 year ago • 5 comments

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'}.

devkral avatar Mar 18 '24 17:03 devkral

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))

Stranger6667 avatar Mar 18 '24 20:03 Stranger6667

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

I wonder what kind of magic the strawberry guys are using

devkral avatar Mar 19 '24 07:03 devkral

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 avatar Mar 19 '24 08:03 Stranger6667

@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 avatar Jul 20 '24 13:07 Stranger6667

no

devkral avatar Jul 22 '24 02:07 devkral