Skip to main content

Build codecov.io status for master branch Version Python versions Gitter License

Hypothesis strategies for GraphQL schemas, queries and data.

NOTE This package is experimental, some features are not supported yet.

Usage

There are a few strategies for different use cases.

  1. Schema generation - hypothesis_graphql.strategies.schemas()

  2. Query - hypothesis_graphql.strategies.queries(schema).

  3. Mutation - hypothesis_graphql.strategies.mutations(schema).

Lets take this schema as an example:

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!
}

Then strategies might be used in this way:

from hypothesis import given
from hypothesis_graphql import strategies as gql_st

SCHEMA = "..."  # the one above


@given(gql_st.queries(SCHEMA))
def test_query(query):
    ...
    # This query might be generated:
    #
    # query {
    #   getBooks {
    #     title
    #   }
    # }


@given(gql_st.mutations(SCHEMA))
def test_mutation(mutation):
    ...
    # This mutation might be generated:
    #
    # mutation {
    #   addBook(title: "H4Z\u7869", author: "\u00d2"){
    #     title
    #   }
    # }

Customization

To restrict the set of fields in generated operations use the fields argument:

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

It is also possible to generate custom scalars. For example, Date:

from hypothesis import strategies as st, given
from hypothesis_graphql import strategies as gql_st, nodes

SCHEMA = """
scalar Date

type Query {
  getByDate(created: Date!): Int
}
"""


@given(
    gql_st.queries(
        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_query(query):
    # Example:
    #
    #  { getByDate(created: "2000-01-01") }
    #
    ...

The hypothesis_graphql.nodes module includes a few helpers to generate various node types:

  • String -> graphql.StringValueNode

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

They exist because classes like graphql.StringValueNode can’t be directly used in map calls due to kwarg-only arguments.

Metadata

Release files for hypothesis-graphql 0.8.1

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.8.1
File Size Uploaded
hypothesis-graphql-0.8.1.tar.gz 14.5 kB Details

Built distribution (wheel)

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

Total release size: 31.1 kB

Release files / hypothesis-graphql-0.8.1.tar.gz

Download URL hypothesis-graphql-0.8.1.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2cf996531268268c2e0f62dd9589344fd04ed936fd81e918ef655d2db8aa6737
BLAKE2b-256 checksum
How to use checksums
1c6f1a71657b5d44933e33e29aff9bc9f340450463b9185e87b2379c0216df31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

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

Download URL hypothesis_graphql-0.8.1-py3-none-any.whl
Size 16.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3e35686bb9812743c68b0b476ee0988a211d09e4b35c48a8a0fe24a073e9fa6
BLAKE2b-256 checksum
How to use checksums
bfbadff1365bab99b138ec16257c5559efab1438e00f3e080db7654bfdb6764c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release history Release notifications | RSS feed

0.13.1

2 release files

0.13.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.2

2 release files

This release

0.8.1 This release

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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