Skip to main content
GraphQL parser for Python
=========================

This is an experimental, generic parser for the [GraphQL language][1] in Python. Consider it unstable since there's no spec for the language yey (that's why it's *experimental* ;)


Install and usage
-----------------

$ pip install graphql-python

And just `import graphql` to get started. This library mimics the standard `json` module, so there's a `dumps` and a `loads` function. Consider this query:

query = """
{
user(232) {
id,
name,
photo(size: 50) {
url,
width,
height
}
}
}
"""

objects = graphql.loads(query)
# objects has now:
[
{
"name": "user",
"params": 232,
"properties": [
{"name": "id"},
{"name": "name"},
{
"name": "photo",
"params": {"size": 50},
"properties": [
{"name": "url"},
{"name": "width"},
{"name": "height"}
]
},
]
}
]

To understand how `loads` works, let's split this query into small parts:

friends(user_id: 232).first(10) {
url,
name,
address
}

This query represents an *object* composed of:

- a name (`friends`)
- can be anything that matches the `r'`[a-zA-Z_][a-zA-Z0-9_/]*'` regex.
- some parameters (`user_id: 232`)
- can match literals (strings, numbers with or without signal, true, false and null) and pairs of values
- `(true)` will be loaded as `"params": True`. Likewise, `(232)` will be `"params": 232`.
- `(foo: "bar", bar: "baz")` will be loaded as `"params": {"foo": "bar", "bar": "baz"}`. Any valid identifier (the regex for *name*) can be used as an argument.
- some custom filters (`first(10)`)
- it's a sequence of identifiers followed by a list of parameters. Order is important, so for example, `.after(id: 243442).first(10)` will be loaded as:


"filters": [
("after", {"id": 243442}),
("first", 10)
]


- and a list of properties (`url, name, address`)... basically either an identifier or another nested object.


Some advices
------------

Right now, this parser is *strict* (at least until the spec is released, obviously). It'll yell at you for not using commas in the right places. For example:

# Will fail :(
graphql.loads("""
user(42) {
id,
name
}
""")

# Much better :) ... don't forget those { } in the beginning and end of the query
graphql.loads("""
{
user(42) {
id,
name
}
}
""")

# Will fail :(
graphql.loads("""
{
user(42) {
id,
name
}

company(2) {
address
}
}
""")

# Much better :) ... see that little comma right after the first object?
graphql.loads("""
{
user(42) {
id,
name
}, # <-- right here

company(2) {
address
}
}
""")


A note about performance & pyParsing
------------------------------------

I didn't test the numbers, just dropping [this link][2] for you to tell there's a way to improve pyParsing's performance.
This flag isn't enabled because it's global, so [YMMV][3].

[1]: https://facebook.github.io/react/blog/2015/05/01/graphql-introduction.html
[2]: http://stackoverflow.com/a/21371472
[3]: http://www.urbandictionary.com/define.php?term=ymmv

Release files for graphql-python 0.0.1

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

Source distribution (sdist)

Source distribution for graphql-python 0.0.1
File Size Uploaded
graphql-python-0.0.1.tar.gz 5.4 kB Details

Release files / graphql-python-0.0.1.tar.gz

Download URL graphql-python-0.0.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c436ee0a6cba12c55011a60753f08d047ad3f5e6706b8a7158040acf98031c8b
BLAKE2b-256 checksum
How to use checksums
5e6ab51deabe32550f3ebd2d3eeb337eed373c4b428b97f7c997ddf767c8eaea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

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