Skip to main content

hypothesis-graphql

Build Coverage Version Python versions Chat License

Generate GraphQL queries from your schema and test your server with them

hypothesis-graphql gives you Hypothesis strategies that build queries and mutations from a GraphQL schema. Each generated operation selects a random subset of fields at random depth and fills arguments with random values.

For a ready-made tool, try Schemathesis: it runs on top of this library and reports server crashes without any test code.

Usage

from_schema takes a schema and returns a strategy that yields queries and mutations as strings:

from hypothesis import given
from hypothesis_graphql import from_schema
import requests

# A schema string or a `graphql.GraphQLSchema` instance
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):
    # Generates operations like:
    #
    # {
    #   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

To generate only one kind of operation, use hypothesis_graphql.queries or hypothesis_graphql.mutations.

Customization

Pass fields to limit which root fields appear in generated operations:

@given(from_schema(SCHEMA, fields=["getAuthors"]))
def test_graphql(query):
    # Only `getAuthors` will be generated
    ...

Two arguments control generated strings:

  • allow_x00 (default True): set it to False to keep \x00 out of strings. Some web servers reject requests that contain it.
  • codec (default utf-8): generated strings only contain characters this codec can encode. Use "ascii" to stay in the ASCII range.
@given(from_schema(SCHEMA, allow_x00=False, codec="ascii"))
def test_graphql(query):
    assert "\0" not in query
    query.encode("ascii")

Set allow_null=False to keep null out of argument values, including nested input fields and list items.

Built-in scalars (Int, Float, String, ID, Boolean) work out of the box. For a custom scalar such as Date, pass a strategy that generates AST nodes for it:

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={"Date": st.dates().map(nodes.String)},
    )
)
def test_graphql(query):
    # Example:
    #
    #  { getByDate(created: "2000-01-01") }
    #
    ...

Without a strategy, hypothesis-graphql skips optional arguments of that type and raises hypothesis.errors.InvalidArgument if a required argument uses it.

hypothesis_graphql.nodes has helpers that build value nodes:

  • String -> graphql.StringValueNode (converts the value with str())
  • Float -> graphql.FloatValueNode
  • Int -> graphql.IntValueNode
  • Object -> graphql.ObjectValueNode
  • List -> graphql.ListValueNode
  • Boolean -> graphql.BooleanValueNode
  • Enum -> graphql.EnumValueNode
  • Null -> graphql.NullValueNode (a constant, not a function)

Use them in map calls: graphql.StringValueNode and friends accept keyword arguments only.

Negative Testing

Pass mode=Mode.NEGATIVE to generate operations your server must reject:

from hypothesis import given
from hypothesis_graphql import queries, Mode
import requests

SCHEMA = """
type Query {
    getUser(id: Int!, name: String!): User
}

type User {
    id: Int!
    name: String!
}
"""


@given(queries(SCHEMA, mode=Mode.NEGATIVE))
def test_server_rejects_invalid_queries(query):
    # Each query violates the schema in exactly one of these ways:
    # - a value of the wrong type (a string for an `Int`)
    # - a missing required argument
    # - an `Int` outside the 32-bit range
    # - `null` for a non-null argument
    # - an unknown enum value
    # - an input object without one of its required fields
    response = requests.post("http://127.0.0.1/graphql", json={"query": query})

    # The server must answer with an error status or an `errors` list
    if response.status_code == 200:
        assert "errors" in response.json()

queries, mutations, and from_schema all accept mode. If no argument in the schema allows any of these violations, negative mode raises InvalidArgument.

License

hypothesis-graphql is available under the MIT license. By contributing, you agree to license your contributions under it.

Metadata

Release files for hypothesis-graphql 0.13.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hypothesis-graphql 0.13.5
File Size Uploaded Provenance Actions
hypothesis_graphql-0.13.5.tar.gz 752.5 kB Without provenance Details for hypothesis_graphql-0.13.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hypothesis-graphql 0.13.5
File Interpreter ABI Platform Actions
hypothesis_graphql-0.13.5-py3-none-any.whl Python 3 none any Details for hypothesis_graphql-0.13.5-py3-none-any.whl

Total release size: 776.3 kB

Release files / hypothesis_graphql-0.13.5.tar.gz

Download URL hypothesis_graphql-0.13.5.tar.gz
Size 752.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d136615c0e11a4d50fd7212cbe69e9a78e2411aedcc945dba20f05df92e662f0
BLAKE2b-256 checksum
How to use checksums
cdd40ff754287dff35bb675b13ab46f8fd179f4d0bddec63f1369f15388c41b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.18.1 {"ci":true,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.14.8"},"installer":{"name":"hatch","version":"1.18.1"},"openssl_version":"OpenSSL 3.0.13 30 Jan 2024","python":"3.14.8","system":{"name":"Linux","release":"7.0.0-1017-azure"}} HTTPX2/2.16.0

Release files / hypothesis_graphql-0.13.5-py3-none-any.whl

Download URL hypothesis_graphql-0.13.5-py3-none-any.whl
Size 23.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7bf16b290fc5b870539e0181446cda1f2aadc3b233a8b475331abc1764fe4e40
BLAKE2b-256 checksum
How to use checksums
1154a7e95192bcb025b6dea99966069f8af46b11bac1fad7476b7ab5eee7a9f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.18.1 {"ci":true,"cpu":"x86_64","distro":{"id":"noble","libc":{"lib":"glibc","version":"2.39"},"name":"Ubuntu","version":"24.04"},"implementation":{"name":"CPython","version":"3.14.8"},"installer":{"name":"hatch","version":"1.18.1"},"openssl_version":"OpenSSL 3.0.13 30 Jan 2024","python":"3.14.8","system":{"name":"Linux","release":"7.0.0-1017-azure"}} HTTPX2/2.16.0

Security for hypothesis-graphql 0.13.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.13.5 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page