Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/denisart/graphql-query
Complete Domain Specific Language (DSL) for GraphQL query in Python.
https://github.com/denisart/graphql-query
code-generation codegen dsl graphql graphql-query graphql-query-builder pydantic python query-builder query-generation query-generator
Last synced: 25 days ago
JSON representation
Complete Domain Specific Language (DSL) for GraphQL query in Python.
- Host: GitHub
- URL: https://github.com/denisart/graphql-query
- Owner: denisart
- License: mit
- Created: 2022-12-13T16:55:21.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-08-01T12:45:39.000Z (3 months ago)
- Last Synced: 2024-09-28T21:41:16.889Z (about 1 month ago)
- Topics: code-generation, codegen, dsl, graphql, graphql-query, graphql-query-builder, pydantic, python, query-builder, query-generation, query-generator
- Language: Python
- Homepage: https://denisart.github.io/graphql-query/
- Size: 118 KB
- Stars: 56
- Watchers: 3
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-pydantic - graphql-query - The package to build GraphQL queries from pydantic classes (Web)
- awesome-pydantic - graphql-query - The package to build GraphQL queries from pydantic classes (Web)
README
# graphql-query
[![tag](https://img.shields.io/github/v/tag/denisart/graphql-query)](https://github.com/denisart/graphql-query)
[![downloads](https://img.shields.io/pypi/dm/graphql-query)](https://pypi.org/project/graphql-query/)
[![last-commit](https://img.shields.io/github/last-commit/denisart/graphql-query/master)](https://github.com/denisart/graphql-query/commits/master)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/graphql-query)](https://pypi.python.org/pypi/graphql-query)
[![license](https://img.shields.io/github/license/denisart/graphql-query)](https://github.com/denisart/graphql-query/blob/master/LICENSE)---
**graphql_query** is a complete Domain Specific Language (DSL) for GraphQL query in Python. With this package
you can to- generate a correct GraphQL query string from a python classes;
- use and share similar Arguments, Variables and e.t.c between different queries;
- easily add new fields to your query;
- add Fragments and Directives to queries;
- generate **graphql_query** classes from pydantic data-model;The documentation for **graphql_query** can be found at [https://denisart.github.io/graphql-query/](https://denisart.github.io/graphql-query/).
## Quickstart
Install with pip
```bash
pip install graphql_query
```### Simple query
Code for the simple query
```graphql
{
hero {
name
}
}
```it is
```python
from graphql_query import Operation, Queryhero = Query(name="hero", fields=["name"])
operation = Operation(type="query", queries=[hero])print(operation.render())
"""
query {
hero {
name
}
}
"""
```The `render` method for the `graphql_query.Operation` object
just returns the final string with a query. Inside the `fields` array of the `graphql_query.Query` object
you can use- `str` (a field name);
- object of `graphql_query.Field` type;
- `graphql_query.Fragment` and `graphql_query.InlineFragment`.### Arguments, Variables and Directives
For generation of the following query
```graphql
query Hero($episode: Episode, $withFriends: Boolean!) {
hero(episode: $episode) {
name
friends @include(if: $withFriends) {
name
}
}
}
```we can use the following python code
```python
from graphql_query import Argument, Directive, Field, Operation, Query, Variableepisode = Variable(name="episode", type="Episode")
withFriends = Variable(name="withFriends", type="Boolean!")arg_episode = Argument(name="episode", value=episode)
arg_if = Argument(name="if", value=withFriends)hero = Query(
name="hero",
arguments=[arg_episode],
fields=[
"name",
Field(
name="friends",
fields=["name"],
directives=[Directive(name="include", arguments=[arg_if])]
)
]
)
operation = Operation(
type="query",
name="Hero",
variables=[episode, withFriends],
queries=[hero]
)
print(operation.render())
"""
query Hero(
$episode: Episode
$withFriends: Boolean!
) {
hero(
episode: $episode
) {
name
friends @include(
if: $withFriends
) {
name
}
}
}
"""
```You can find other examples in the documentation.