Skip to main content

robotframework-graphql

GraphQL test library for Robot Framework.

Keyword documentation

Why not just RequestsLibrary

A GraphQL endpoint is one HTTP POST, so a general HTTP library can send one. What it cannot do is tell whether the operation worked.

A server answering application/json returns HTTP 200 even when the operation failed, and reports the failure in an errors array in the body. This is what the GraphQL over HTTP specification calls for. A suite built on Status Should Be 200 therefore passes on a request that returned nothing at all:

# Passes even when the server returned {"data": null, "errors": [{"message": "..."}]}
${response}    POST On Session    api    /graphql    json=${body}
Status Should Be    200    ${response}

Every keyword here that sends an operation checks the errors array and fails on it, including the partial case where data came back alongside errors because a nullable field failed. That is the default, not something a suite has to remember.

The second problem is syntax. Robot Framework collapses runs of spaces inside a cell, so a query written across several lines breaks. The usual workaround is a .json file holding the query as one escaped line, which is neither readable nor reviewable. This library takes queries from real .graphql files.

Features

  • Fails on GraphQL errors by default, including partial data, whatever the status code was
  • Queries from .graphql files, by path or by name under a query directory
  • Several named operations per document, chosen by name
  • Inline queries as a list of lines, which survives Robot Framework's whitespace handling
  • Queries parsed before they are sent, so a syntax error names its line and column
  • Response getters with assertion engine operators, as in Get Graphql Data ${response} user.name == Alice
  • Error assertions that speak GraphQL: path, extensions.code, partial data
  • A retrying Check Query Result for read models that are filled in asynchronously
  • Schema introspection: list an endpoint's queries and mutations, and assert on deprecations
  • Queries checked against the endpoint's own schema before they are sent, so a misspelled field is reported with its line and column instead of being sent and rejected
  • Schema drift detection: snapshot the schema as reviewable SDL and assert nothing broke since
  • Session pool with aliases, sharing one connection pool per endpoint
  • Interop: an existing requests.Session can be handed in, so cookies and adapters configured elsewhere are reused

Installation

pip install robotframework-graphql

Requirements

Requirement Version
Python 3.10 or later
Robot Framework 6.1.1 or later, below 8
gql 4.x, with the requests transport
graphql-core 3.2.x
robotframework-assertion-engine 5.x

Importing

*** Settings ***
Library    GraphQLLibrary    query_path=${CURDIR}/queries
  • query_path: directory that queries given by file name are looked up in.
  • validate_queries: whether queries are checked locally before they are sent. Default true.
  • validate_against_schema: whether queries are also checked against the endpoint's own schema before they are sent. Default true. The schema is introspected once per endpoint and held. Where introspection is disabled the check is skipped with a warning rather than failing, so a server that will not describe itself is still testable.
# Caught before any request is sent, naming the line and column:
${response}    Execute Query    { user(id: "1") { nickname } }
# ValueError: The query does not match the schema. 1 problem(s) found:
# - Cannot query field 'nickname' on type 'User'. Did you mean 'name'? (line 1, column 19)

Usage

*** Settings ***
Library           GraphQLLibrary    query_path=${CURDIR}/queries
Suite Teardown    Delete All Graphql Sessions

*** Test Cases ***
A User Can Be Read Back
    Create Graphql Session    https://api.example.com/graphql    token=${API_TOKEN}
    ${variables}    Create Dictionary    id=1
    ${response}     Execute Query    get_user.graphql    variables=${variables}
    Get Graphql Data    ${response}    user.name    ==    Alice

A Denied Field Is Reported With Its Path And Code
    ${response}    Execute Query    get_user.graphql    expect_errors=True
    Response Should Have Partial Data    ${response}
    Error Should Exist At Path    ${response}    user.avatar    code=FORBIDDEN

Writing queries

Three forms, all accepted wherever a query is:

# A file, under query_path or as a path
${response}    Execute Query    get_user.graphql

# One document, several named operations
${response}    Execute Query    user_document.graphql    operation_name=GetUsers

# Inline, as a list of lines
@{lines}       Create List    query {    ${SPACE*4}ping    }
${response}    Execute Query    ${lines}

The query reaching the server is the parsed document printed back out, so it arrives in the standard layout rather than exactly as it was written.

Sharing a session with RequestsLibrary

${http_session}    Evaluate    __import__('requests').Session()
Create Graphql Session    ${GRAPHQL_URL}    http_session=${http_session}

The supplied session is left open when the GraphQL session is deleted, because this library did not open it.

Deliberately not wrapped

Subscriptions, file uploads, request batching and persisted queries. Schema introspection covers object and interface fields, so deprecated input fields and enum values are not reported. Drift detection compares types and fields, not the built-in directives. Execute Raw Request sends an operation with no checking at all, for cases this library does not model.

Development

poetry install
poetry run pytest utest
./run_atest.sh                     # starts the test server, runs the acceptance suites
poetry run ruff check . && poetry run mypy && poetry run robocop check atest

License

MIT

Download files

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

Source Distribution

robotframework_graphql-0.2.0.tar.gz (29.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

robotframework_graphql-0.2.0-py3-none-any.whl (35.7 kB view details)

Uploaded Python 3

File details

Details for the file robotframework_graphql-0.2.0.tar.gz.

File metadata

  • Download URL: robotframework_graphql-0.2.0.tar.gz
  • Upload date:
  • Size: 29.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for robotframework_graphql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c4d858f43c96d206b22eda2aad7c621bdcd2a08e1b074ca44975272e6e5fc09f
MD5 8d074d1ed137513d5730b6462b15d049
BLAKE2b-256 e031f37199cdf88ffb4181c28a92fc890ddfd66251a377d5123dbda1536f5251

See more details on using hashes here.

Provenance

The following attestation bundles were made for robotframework_graphql-0.2.0.tar.gz:

Publisher: release.yml on MobyNL/robotframework-graphqllibrary

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file robotframework_graphql-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for robotframework_graphql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 101fc959ce26e470f6130b01690651bedd2eeb6da25b9eb0cf3830a921a86315
MD5 a94f52569fb6f9e2ad94e71fa9d22273
BLAKE2b-256 26c2448e4636722bcc1c8ccf17ff821436fd764acf9aebd0510d179b69e028cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for robotframework_graphql-0.2.0-py3-none-any.whl:

Publisher: release.yml on MobyNL/robotframework-graphqllibrary

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.0

2 files

Supported by

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