schemathesis
schemathesis copied to clipboard
[FEATURE] Generating JSON nodes in GraphQL
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'}.
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))
sadly still failing with: TypeError: Invalid AST Node: {'action': 'manage'}
I wonder what kind of magic the strawberry guys are using
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
@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
no