A Python library for making GraphQL requests easier!
Project description
gqlrequests - A Python library for creating GraphQL queries easier!
Define GraphQL types in Python as dataclasses, then use them to automatically build queries!
Note that these examples show what the end goal is, and that very few of these features have been developed yet!
Examples of how it will work:
from dataclasses import dataclass
import gqlrequests
@dataclass
class Episode:
name: str
length: float
@dataclass
class Character:
name: str
appearsIn: list[Episode]
print(gqlrequests.Schema(Character))
# type Character {
# name: String
# appearsIn: [Episode]
# }
#
print(gqlrequests.Query(Character))
# {
# name
# appearsIn {
# name
# length
# }
# }
print(gqlrequests.Query(Character, fields=["name"]))
# {
# name
# }
print(gqlrequests.Query(Character, indents=2)) # Default indent is 4
# {
# name
# appearsIn {
# name
# length
# }
# }
print(gqlrequests.QueryMethod("get_character", Character, args={"name": "Luke"}))
# get_character(name: "Luke") {
# name
# appearsIn {
# name
# length
# }
# }
appearsIn = gqlrequests.QueryMethod(
"appearsIn",
Episode,
args = {"minLength": 5}
)
print(gqlrequests.Query(
Character,
fields = [
"name",
appearsIn
]
))
# {
# name
# appearsIn(minLength: 5) {
# name
# length
# }
# }
Future possible implementations:
import gqlrequests
import asyncio
@dataclass
class Episode:
name: str
length: float
@dataclass
class Character:
name: str
appearsIn: list[Episode]
gqlclient = gqlrequests.Client(
api_endpoint="api.example.com/gql",
authorization="abcdefghijklmnopqrstuvwxyz"
)
character = gqlclient.query(gqlrequests.Query(Character))
assert isinstance(character, Character)
# Asynchronous queries
async def main():
gqlclient = gqlrequests.AsyncClient(
api_endpoint="api.example.com/gql",
authorization="abcdefghijklmnopqrstuvwxyz"
)
queries = asyncio.gather(
gqlclient.query(gqlrequests.Query(Character)),
gqlclient.query(gqlrequests.Query(Episode))
)
character, episode = await queries
assert isinstance(character, Character)
assert isinstance(episode, Episode)
# Or simply:
character = await gqlclient.query(gqlrequests.Query(Character))
asyncio.run(main())
"""Subscribing to a graphql websocket"""
import gqlrequests
import asyncio
@dataclass
class LiveViewers:
viewers: int
measurementTimeUnix: int
async def main():
gqlclient = gqlrequests.Client(
api_endpoint="api.example.com/gql",
authorization="abcdefghijklmnopqrstuvwxyz"
)
query = gqlrequests.Query(LiveViewers)
async with gqlclient.subscribe(query) as subscription:
async for data in subscription:
assert isinstance(data, LiveViewers)
print(data.viewers, data.measurementTimeUnix)
if data.viewers < 10: break
asyncio.run(main())
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
gqlrequests-0.0.7.tar.gz
(5.3 kB
view hashes)
Built Distribution
Close
Hashes for gqlrequests-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65af5c416b69dc71fe56e9857c4fc5bf6b26ba250e190e81475c496616bcd1f4 |
|
MD5 | 75357bc05304c8bef547461243dfdaa2 |
|
BLAKE2b-256 | 4f1d648872ab74236b79ae5a00fbc5cee4e3ff7795b08c6fd11957d93e507c02 |