GraphQL Testing Strategies, sgqlc Python Client, and the Custom Graphy Framework

This article explains how to test GraphQL APIs by illustrating request/response examples, outlining testing approaches, reviewing the official sgqlc Python client, and presenting a self‑developed GraphQL testing framework called graphy with its design and usage details.

Byte Quality Assurance Team
Byte Quality Assurance Team
Byte Quality Assurance Team
GraphQL Testing Strategies, sgqlc Python Client, and the Custom Graphy Framework

Building on a previous introduction to GraphQL concepts, this article demonstrates how to test GraphQL interfaces, showcases the official Python client sgqlc, and describes a custom testing framework named graphy.

Example request and response

Request:

query {
  MGetMainAccelerDomain(ids: ["1"]) {
    id
    name
    core {
      business_type
      is_main
      owner
    }
    distribute {
      items {
        domain {
          id
          name
        }
      }
    }
  }
}

Response:

{
  "data": {
    "MGetMainAccelerDomain": [
      {
        "id": "1",
        "name": "www.baidu.com",
        "core": {
          "business_type": "",
          "is_main": false,
          "owner": ""
        },
        "distribute": {
          "items": []
        }
      }
    ]
  }
}

The article notes that GraphQL requests act like a special closure language while responses are standard JSON, and that the request structure mirrors the response structure.

GraphQL testing ideas

Test each entry point individually.

Ideally cover all fields; testing full depth is often impractical and should be scoped to real scenarios.

After endpoint‑level tests, perform end‑to‑end multi‑interface tests.

Performance testing should vary field count, array length, and depth based on real traffic.

Desired features of a GraphQL testing framework

Automatic generation of model classes via GraphQL introspection.

Model classes that can both build requests and hold responses.

Auto‑populate all fields with configurable depth and ability to prune fields.

Automatic generation of validation scripts for full‑field checks.

sgqlc: the official Python client

sgqlc can generate model classes from a schema and construct queries programmatically. Example usage:

from sgqlc.operation import Operation
from github_schema import github_schema as schema

op = Operation(schema.Query)
issues = op.repository(owner=owner, name=name).issues(first=100)
issues.nodes.number()
issues.nodes.title()
issues.page_info.__fields__('has_next_page')
issues.page_info.__fields__(end_cursor=True)
print(op)

data = endpoint(op)
repo = (op + data).repository
for issue in repo.issues.nodes:
    print(issue)

Limitations mentioned include lack of proper Python type hints for generated models and difficulty deserializing response JSON into objects.

graphy: a custom GraphQL testing framework

The author created graphy to address sgqlc’s shortcomings. Example model generation command:

python3 graphy/graphy_gen.py "http://10.227.10.13:7794/graphql" http_test/graphy_test/example.py

Generated model classes define _fields dictionaries and constructors, enabling IDE hints. Example snippets:

class Query(graphy.Query):
    _description = "xxx"
    _fields = {
        "FilterSubDomain": "_listof_SubDomain",
        "GetSubDomain": "SubDomain"
    }
    def __init__(self, schema=None, depth=None):
        self.FilterSubDomain : List[SubDomain]
        self.GetSubDomain : SubDomain
        return super().__init_(schema=schema, depth=depth)

class AuthInfo(graphy.Node):
    _description = "认证信息"
    _fields = {
        "id": "int",
        "name": "str",
        "description": "str",
        "params": "_listof_AuthParam",
    }
    def __init__(self, args=None):
        self.id : int
        self.name : str
        self.description : str
        self.params : List[AuthParam]
        return super().__init_(args=args)

Requests are built from Query or Mutation instances, with methods like _g() to print the GraphQL string, _d() to set depth, _f() to force fields, and _a() to set arguments. Example:

query = Query(schema="http_test.graphy_test.example_schema", depth=1)
query.MGetMainDomain({"ids": [79417]})
print(query._g())

Responses are automatically deserialized back into the same object hierarchy, allowing seamless access to returned data.

Implementation notes

Graphy stores hidden attributes (prefixed with underscore) for topology state and overrides __getattribute__ to hide them.

Visible attributes represent child nodes, enabling recursive traversal until primitive types (STRING, INT, etc.) are reached.

Future work includes polishing the code, publishing it as a public package, and adding support for GraphQL features such as aliases, fragments, and directives.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

APIGraphQLgraphysgqlc
Byte Quality Assurance Team
Written by

Byte Quality Assurance Team

World-leading audio and video quality assurance team, safeguarding the AV experience of hundreds of millions of users.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.