A simple and pythonic way to query GraphQL endpoints without having to do any string manipulation.
Project description
Grafliq
A simple and pythonic way to query GraphQL endpoints without having to do any string manipulation.
Grafliq is a GraphQL client that provides a convenient way to handle GraphQL queries. Instead of building your query by manipulating and concatenating strings, you build your queries using function calls, eliminating all the complications and problems associated with string manipulation, and making maintenance an easy task.
Important note
Grafliq only supports the query
operation of GraphQL, there is no support
for mutations
and subscriptions
and there will be none in the foreseeable future.
Installing
Install using pip:
pip install grafliq
Example using function calls as GraphQL operations (recommended)
Let's say we want to have a GraphQL query with the following structure:
{
category(name: "household-appliances") {
electronics {
gaming_consoles(
fromReleaseDate: "2000-01-01"
fromPrice: "150"
discontinued: false
colors: [BLACK, WHITE, RED]
) {
brand
name
color
discontinued
release_date
price(currency: "euro")
model {
name
version
}
customer_rating(fromRatingDate: "2002-10-01") {
count
rates {
average(decimalCount: 2)
highest
lowest
}
}
}
}
}
}
We can generate the above query using the following Python code:
from datetime import date
from grafliq.builder import GraphQL
from grafliq.query import NestedField, Quote, NoQuote, CustomizableField
query = GraphQL(
endpoint='http://127.0.0.1:8080/graphql-api'
).category(
name='household-appliances'
).electronics(
).gaming_consoles(
'brand', 'name', 'color', 'discontinued', 'release_date',
CustomizableField('price', currency="euro"),
NestedField('model',
'name', 'version'),
NestedField('customer_rating',
'count',
NestedField('rates',
CustomizableField('average', decimalCount=2),
'highest', 'lowest'),
fromRatingDate=date(2002, 10, 1)),
fromReleaseDate=date(2000, 1, 1),
fromPrice=Quote(150),
discontinued=False,
colors=NoQuote(['BLACK', 'WHITE', 'RED'])
)
"""
To get the query as a string, you can either do this:
"""
query_string1 = str(query)
"""
Or you can call `.generate()` on the query object:
"""
query_string2 = query.generate()
"""
If you pass the `endpoint` to the `GraphQL` initializer, you can
call `.execute()` directly on the query object to call the remote API and get the result:
"""
result = query.execute()
"""
Note that if you have not passed the `endpoint` to the
`GraphQL` initializer, by calling the `.execute()` function, an error will be raised.
"""
As shown in the example above, any operation available in the remote GraphQL API
will be available as a function on the GraphQL
object.
Example using generic query
If you want to dynamically generate GraphQL queries (.ex in an automated script, from user input, ...), you can do this with the following example, which will generate exactly the same GraphQL query:
from datetime import date
from grafliq.builder import GraphQL
from grafliq.query import NestedField, Quote, NoQuote, CustomizableField
query = GraphQL(
endpoint='http://127.0.0.1:8080/graphql-api'
).query(
'category',
name='household-appliances'
).query(
'electronics'
).query(
'gaming_consoles',
'brand', 'name', 'color', 'discontinued', 'release_date',
CustomizableField('price', currency="euro"),
NestedField('model',
'name', 'version'),
NestedField('customer_rating',
'count',
NestedField('rates',
CustomizableField('average', decimalCount=2),
'highest', 'lowest'),
fromRatingDate=date(2002, 10, 1)),
fromReleaseDate=date(2000, 1, 1),
fromPrice=Quote(150),
discontinued=False,
colors=NoQuote(['BLACK', 'WHITE', 'RED'])
)
Contribute to Grafliq development
I appreciate any kind of contribution to the development of Grafliq, especially to add support for mutations and subscriptions.
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 grafliq-0.1.0.tar.gz
.
File metadata
- Download URL: grafliq-0.1.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a2e8b332f44df5786e010c0ece31bbf1b81aba020756531b839f7ae5e1e0ea1 |
|
MD5 | f7c88368c48dcb75b22a620b77afc1bb |
|
BLAKE2b-256 | 067e93c20aeb3c75c193af0a23f143b5d8ed96f397883d09cf7c6093cb3f872e |
File details
Details for the file grafliq-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: grafliq-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a823ec30e5d1816a31a7c2fb7c4a33e740f6c173c24d9005338acdc2c88ba60 |
|
MD5 | faff045e461210e10e0a8904870e7536 |
|
BLAKE2b-256 | 36761ce252fa6bd256186097477a7d8a687025d37ec180d88d6894107cd5c7ad |