Skip to main content

non intrusive python graphql client library wrapped around pydantic

Project description

ql (in development)

Graphql client library, wrapped around pydantic classes for typing validation, provide simple, safe and dynamic way to query data from a graphql api.

using pydantic for creating python objects from rest api is common, it is easy and it has type validation, so why not do that also for graphql apis?

features:

  • python objects to valid graphql string
  • http send and recv information
  • scalar http responses

Query examples

simple query

import ql
from pydantic import BaseModel


@ql.model
class Point(BaseModel):
  x: int
  y: int


q = ql.query(
  (Point, (
    ql._(Point).x,
    ql._(Point).y
  ))
)
print(q)

query{Point{x,y}}

different query names then what defined

import ql
from pydantic import BaseModel, Field


@ql.model(query_name="Person")
class Adult(BaseModel):
  name: Annotation[str, ql.metadata(query_name="first_name")]

q = ql.query((Adult,(ql._(Adult).name,))
print(q)

query{Person{first_name}}

smart implements + nested query + inline fragment

import ql
from pydantic import BaseModel

@ql.model
class Human(BaseModel):
  first_name: str
  last_name: str

@ql.model
class Female(Human):
  pregnant: bool

@ql.model
class Male(Human):
  pass

print(ql.implements(Human))  # what does `Human` implement
q = ql.query(
    (Human, (
        ql._(Human).first_name,
        (ql.on(Female), (
            ql._(Female).pregnant,
        ))
    ))
)
print(q)

frozenset({<class '__main__.Human'>})
query{Human{first_name,...on Female{pregnant,__typename},__typename}}

query with http

import ql
import requests
from pydantic import BaseModel

ql.http.set_request_func(lambda q: requests.get(...).json())

# define models ...

response = ql.query_response(
  (Point, (
     ql._(Point).x,
     ql._(Point).y
  ))
)
print(response)

{"data": {"point": "x": 50, "y": -50}}

query and scalar response

import ql
import requests
from pydantic import BaseModel

ql.http.set_request_func(lambda q: requests.get(...).json())

@ql.model
class Point(BaseModel):
  x: int
  y: int

scalared = ql.query_response_scalar(
 (Point, (
   ql._(Point).x,
   ql._(Point).y
  ))
)
print(scalared)

{"point": Point(x=50, y=-50)}

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

pydantic_graphql-0.1.1a0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

pydantic_graphql-0.1.1a0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_graphql-0.1.1a0.tar.gz.

File metadata

  • Download URL: pydantic_graphql-0.1.1a0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.10.3-arch1-2

File hashes

Hashes for pydantic_graphql-0.1.1a0.tar.gz
Algorithm Hash digest
SHA256 fa5f1774f73d2449d04407b2ce3a69be102e0e417c3669fab5a97273a083d9d1
MD5 652f6a1efc0660c86252d812279ac90c
BLAKE2b-256 5922bd17d255fc6d18408777842552b52bed71225832f55386f1e3f071077330

See more details on using hashes here.

File details

Details for the file pydantic_graphql-0.1.1a0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_graphql-0.1.1a0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.10.3-arch1-2

File hashes

Hashes for pydantic_graphql-0.1.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d2d442a02fbd786eccf0cd2c1fcd8696d019dc1e2009c44d1315344e32a34bc
MD5 7ea1c9a56f6d34c1a8ab7a192a5ba62f
BLAKE2b-256 5de9162982a8a42a8242ca7a91e0a81920904b8e6d5c2c8d57cdef4433e72211

See more details on using hashes here.

Supported by

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