Skip to main content

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.

Metadata

Release files for grafliq 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grafliq 0.1.0
File Size Uploaded
grafliq-0.1.0.tar.gz 10.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for grafliq 0.1.0
File Interpreter ABI Platform
grafliq-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.7 kB

Release files / grafliq-0.1.0.tar.gz

Download URL grafliq-0.1.0.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3a2e8b332f44df5786e010c0ece31bbf1b81aba020756531b839f7ae5e1e0ea1
BLAKE2b-256 checksum
How to use checksums
067e93c20aeb3c75c193af0a23f143b5d8ed96f397883d09cf7c6093cb3f872e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / grafliq-0.1.0-py3-none-any.whl

Download URL grafliq-0.1.0-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a823ec30e5d1816a31a7c2fb7c4a33e740f6c173c24d9005338acdc2c88ba60
BLAKE2b-256 checksum
How to use checksums
36761ce252fa6bd256186097477a7d8a687025d37ec180d88d6894107cd5c7ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page