Skip to main content

Hypothesis strategies for GraphQL schemas and queries

Project description

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
import graphql

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(lambda v: graphql.StringValueNode(value=str(v)))
        },
    )
)
def test_query(query):
    # Example:
    #
    #  { getByDate(created: "2000-01-01") }
    #
    ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hypothesis-graphql-0.7.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

hypothesis_graphql-0.7.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file hypothesis-graphql-0.7.0.tar.gz.

File metadata

  • Download URL: hypothesis-graphql-0.7.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for hypothesis-graphql-0.7.0.tar.gz
Algorithm Hash digest
SHA256 329c952b29b7ad0e12b7ea546c9f162e12163b2e2a8977f98f65e163699d5e88
MD5 30ef2e269ed30e604504268a9c2fc033
BLAKE2b-256 db5a68c6dda0aa20810026fee5ddacb125f0ad63b5bebeb2cc7b0f3493e1f315

See more details on using hashes here.

File details

Details for the file hypothesis_graphql-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hypothesis_graphql-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31130eaaa0ea790e4b30d3bb141ad9687b001b6e5404990250624e93d3c453b1
MD5 370c3b7136889c2d2b21737f75072571
BLAKE2b-256 c4aa0c510bd6a3706475dac60bbfa5c6854c5474d12c532fe4f39641473173a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page