A Python library for making GraphQL requests easier!
Project description
gqlrequests - Create requests to GraphQL APIs with no strings attached 😉
Define GraphQL types in Python as classes or pydantic classes, then use them to automatically build queries. Or even simpler; gqlrequests will automatically build the classes for you given the api endpoint by using introspection! (Now that's awesome). You no longer need to define your requests as multiline strings (hence no strings attached).
Examples of currently working features:
import gqlrequests
class Episode(gqlrequests.QueryBuilder):
name: str
length: float
class Character(gqlrequests.QueryBuilder):
name: str
appearsIn: list[Episode]
print(Character().build())
# {
# name
# appearsIn {
# name
# length
# }
# }
print(Character(fields=["name"]).build())
# {
# name
# }
print(Character().build(indents=2)) # Default indent is 4
# {
# name
# appearsIn {
# name
# length
# }
# }
getCharacter = Character(func_name="getCharacter")
print(getCharacter(name="Luke").build())
# getCharacter(name: "Luke") {
# name
# appearsIn {
# name
# length
# }
# }
episode_func = Episode(func_name="appearsIn")
characters_query = Character()
characters_query.appearsIn = episode_func(minLength=5)
print(characters_query.build())
# {
# name
# appearsIn(minLength: 5) {
# name
# length
# }
# }
from pydantic import BaseModel
class ExampleModel(BaseModel):
age: int
name: str
ExampleQueryBuilder = gqlrequests.from_pydantic(ExampleModel)
print(ExampleQueryBuilder().build())
Edge cases
Some attributes are reserved keywords in Python, such as from
, is
and not
. These cannot be referenced to
by property like this: some_query_result.from
. This can be circumvented by adding leading or trailing underscores,
then passing the strip_underscores
argument to the build method.
class Time(gqlrequests.QueryBuilder):
_from: int
_to: int
value: float
print(Time().build(strip_underscores=True))
# {
# from
# to
# value
# }
Other features that are not yet implemented:
print(Character)
# type Character {
# name: String
# appearsIn: [Episode]
# }
#
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gqlrequests-0.0.11.tar.gz
.
File metadata
- Download URL: gqlrequests-0.0.11.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c5f1dbef20733d0eea1001f0361bcfb80d6c047e28a78763bc456e5d25a14b9 |
|
MD5 | 45cfb7b99ddfbc6fce8f0988bfbb0f6e |
|
BLAKE2b-256 | db31cc463d95662f0c7bdaca5d3d7ca193b91eef1befbd7647e72ca737acf922 |
File details
Details for the file gqlrequests-0.0.11-py3-none-any.whl
.
File metadata
- Download URL: gqlrequests-0.0.11-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bec5d8baaef5387d6a996d5eabbfbad9e609babc8900fa4c883806ff089b1c2 |
|
MD5 | b317d59563e329caf8208b72e41710ee |
|
BLAKE2b-256 | caf5ebcc4d05acb10563c7e4dd75d96d82402b3b9d4b6fa678b8873180f7e9f7 |