
Security News
The Nightmare Before Deployment
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.
hypothesis-graphql
Advanced tools
It is a Python library that provides a set of Hypothesis strategies that let you write tests parametrized by a source of examples. Generated queries have arbitrary depth and may contain any subset of GraphQL types defined in the input schema. They expose edge cases in your code that are unlikely to be found otherwise.
Schemathesis provides a higher-level interface around this library and finds server crashes automatically.
hypothesis-graphql provides the from_schema function, which takes a GraphQL schema and returns a Hypothesis strategy for
GraphQL queries matching the schema:
from hypothesis import given
from hypothesis_graphql import from_schema
import requests
# Strings and `graphql.GraphQLSchema` are supported
SCHEMA = """
type Book {
title: String
author: Author
}
type Author {
name: String
books: [Book]
}
type Query {
getBooks: [Book]
getAuthors: [Author]
}
type Mutation {
addBook(title: String!, author: String!): Book!
addAuthor(name: String!): Author!
}
"""
@given(from_schema(SCHEMA))
def test_graphql(query):
# Will generate samples like these:
#
# {
# getBooks {
# title
# }
# }
#
# mutation {
# addBook(title: "H4Z\u7869", author: "\u00d2"){
# title
# }
# }
response = requests.post("http://127.0.0.1/graphql", json={"query": query})
assert response.status_code == 200
assert response.json().get("errors") is None
It is also possible to generate queries or mutations separately with hypothesis_graphql.queries and hypothesis_graphql.mutations.
To restrict the set of fields in generated operations use the fields argument:
@given(from_schema(SCHEMA, fields=["getAuthors"]))
def test_graphql(query):
# Only `getAuthors` will be generated
...
You can customize the string generation with these arguments to from_schema:
allow_x00 (default True): Determines whether to allow the generation of \x00 bytes within strings. It is useful to avoid rejecting tests as invalid by some web servers.codec (default utf-8): Specifies the codec used for generating strings. It helps if you need to restrict the inputs to, for example, the ASCII range.@given(from_schema(SCHEMA, allow_x00=False, codec="ascii"))
def test_graphql(query):
assert "\0" not in query
query.encode("ascii")
It is also possible to generate custom scalars. For example, Date:
from hypothesis import strategies as st, given
from hypothesis_graphql import from_schema, nodes
SCHEMA = """
scalar Date
type Query {
getByDate(created: Date!): Int
}
"""
@given(
from_schema(
SCHEMA,
custom_scalars={
# Standard scalars work out of the box, for custom ones you need
# to pass custom strategies that generate proper AST nodes
"Date": st.dates().map(nodes.String)
},
)
)
def test_graphql(query):
# Example:
#
# { getByDate(created: "2000-01-01") }
#
...
The hypothesis_graphql.nodes module includes a few helpers to generate various node types:
String -> graphql.StringValueNodeFloat -> graphql.FloatValueNodeInt -> graphql.IntValueNodeObject -> graphql.ObjectValueNodeList -> graphql.ListValueNodeBoolean -> graphql.BooleanValueNodeEnum -> graphql.EnumValueNodeNull -> graphql.NullValueNode (a constant, not a function)They exist because classes like graphql.StringValueNode can't be directly used in map calls due to kwarg-only arguments.
The code in this project is licensed under MIT license.
By contributing to hypothesis-graphql, you agree that your contributions will be licensed under its MIT license.
FAQs
Hypothesis strategies for GraphQL queries
We found that hypothesis-graphql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.

Research
/Security News
Impostor NuGet package Tracer.Fody.NLog typosquats Tracer.Fody and its author, using homoglyph tricks, and exfiltrates Stratis wallet JSON/passwords to a Russian IP address.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.