Runs a Tartiflette GraphQL Engine through aiohttp
Project description
![Tartiflette aiohttp](docs/github-landing.png)
**tartiflette-aiohttp** is a wrapper of [aiohttp](https://github.com/aio-libs/aiohttp/) which includes the [Tartiflette GraphQL Engine](https://github.com/dailymotion/tartiflette), do not hesitate to take a look of the [Tartiflette project](https://github.com/dailymotion/tartiflette).
**Summary**
- [Usage](#usage)
- [Installation](#installation)
- [Installation dependencies](#installation-dependencies)
- [How to use](#how-to-use)
- [Use with built-in Tartiflette Engine](#use-with-built-in-tartiflette-engine)
- [Use with custom Tartiflette engine](#use-with-custom-tartiflette-engine)
## Usage
```python
# main.py
from aiohttp import web
from tartiflette import Resolver
from tartiflette_aiohttp import Application
@Resolver("Query.hello")
async def resolver_hello(parent, args, ctx, info):
return "hello " + args["name"]
sdl = """
type Query {
hello(name: String): String
}
"""
app = Application(
engine_sdl=sdl
)
web.run_app(app)
```
Save the file and start the server.
```bash
$ python main.py
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
```
Execute a request to your server
```
curl -v -d '{"query": "query { hello(name: "Chuck") }"}' -H "Content-Type: application/json" http://localhost:8080/graphql
```
## Installation
`tartiflette-aiohttp` is available on [pypi.org](https://pypi.org/project/tartiflette-aiohttp/).
```bash
pip install tartiflette-aiohttp
```
### Installation dependencies
As [Tartiflette](https://github.com/dailymotion/tartiflette) based its Executor engine on *[libgraphqlparser](https://github.com/graphql/libgraphqlparser)*. You'll need these following commands on your environment to use the library. `cmake`, `bison` and `flex`.
*MacOSX*
```bash
brew install cmake flex bison
```
*Ubuntu*
```bash
apt-get install cmake flex bison
```
## How to use
### Use with built-in Tartiflette Engine
The basic and common way to create an `aiohttp` instance is to use the `Application` class with the `engine_*` parameters which are forwarded to the built-in [tartiflette](https://github.com/dailymotion/tartiflette) engine instance.
```python
from aiohttp import web
from tartiflette_aiohttp import Application
sdl = """
type Query {
hello(name: String): String
}
"""
ctx = {
'user_service': user_service
}
app = Application(
engine_sdl=sdl,
engine_schema_name="default",
executor_context=user_service,
executor_http_endpoint='/graphql',
executor_http_methods=['POST', 'GET']
)
web.run_app(app)
```
**Parameters**:
* **engine_sdl**: Contains the [Schema Definition Language](https://graphql.org/learn/schema/)
- Could be a string which contains the SDL
- Could be an array of string, which contain the SDLs
- Could be a path of an SDL
- Could be an array of paths which contain the SDLs
* **engine_schema_name**: Name of the schema used the built-in engine.
* **executor_context**: Context which will be passed to each resolver. Be default, the context passed to each resolvers, will contain these properties.
- **req**: Request object from aiohttp
- **app**: Application object from aiohttp
* **executor_http_endpoint**: Endpoint where the GraphQL Engine will be attached, by default on `/graphql`
* **executor_http_methods**: HTTP Method where the GraphQL Engine will be attached, by default on **POST** and **GET**.
### Use with custom Tartiflette engine
In the case you already have a Tartiflette Engine instance, or, you do not want to use the built-in instance. You can pass an existing instance to the Application constructor.
```python
# main.py
from aiohttp import web
from tartiflette import Resolver, Engine
from tartiflette_aiohttp import Application
@Resolver("Query.hello")
async def resolver_hello(parent, args, ctx, info):
return "hello " + args["name"]
sdl = """
type Query {
hello(name: String): String
}
"""
engine = Engine(sdl)
ctx = {
'user_service': user_service
}
app = Application(
engine=engine,
executor_context=user_service,
executor_http_endpoint='/graphql',
executor_http_methods=['POST', 'GET']
)
web.run_app(app)
```
**Parameters**:
* **engine**: Tartiflette Engine instance
* **executor_context**: Context which will be passed to each resolver. Be default, the context passed to each resolvers, will contain these properties.
- **req**: Request object from aiohttp
- **app**: Application object from aiohttp
* **executor_http_endpoint**: Endpoint where the GraphQL Engine will be attached, by default on `/graphql`
* **executor_http_methods**: HTTP Method where the GraphQL Engine will be attached, by default on **POST** and **GET**.
**tartiflette-aiohttp** is a wrapper of [aiohttp](https://github.com/aio-libs/aiohttp/) which includes the [Tartiflette GraphQL Engine](https://github.com/dailymotion/tartiflette), do not hesitate to take a look of the [Tartiflette project](https://github.com/dailymotion/tartiflette).
**Summary**
- [Usage](#usage)
- [Installation](#installation)
- [Installation dependencies](#installation-dependencies)
- [How to use](#how-to-use)
- [Use with built-in Tartiflette Engine](#use-with-built-in-tartiflette-engine)
- [Use with custom Tartiflette engine](#use-with-custom-tartiflette-engine)
## Usage
```python
# main.py
from aiohttp import web
from tartiflette import Resolver
from tartiflette_aiohttp import Application
@Resolver("Query.hello")
async def resolver_hello(parent, args, ctx, info):
return "hello " + args["name"]
sdl = """
type Query {
hello(name: String): String
}
"""
app = Application(
engine_sdl=sdl
)
web.run_app(app)
```
Save the file and start the server.
```bash
$ python main.py
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
```
Execute a request to your server
```
curl -v -d '{"query": "query { hello(name: "Chuck") }"}' -H "Content-Type: application/json" http://localhost:8080/graphql
```
## Installation
`tartiflette-aiohttp` is available on [pypi.org](https://pypi.org/project/tartiflette-aiohttp/).
```bash
pip install tartiflette-aiohttp
```
### Installation dependencies
As [Tartiflette](https://github.com/dailymotion/tartiflette) based its Executor engine on *[libgraphqlparser](https://github.com/graphql/libgraphqlparser)*. You'll need these following commands on your environment to use the library. `cmake`, `bison` and `flex`.
*MacOSX*
```bash
brew install cmake flex bison
```
*Ubuntu*
```bash
apt-get install cmake flex bison
```
## How to use
### Use with built-in Tartiflette Engine
The basic and common way to create an `aiohttp` instance is to use the `Application` class with the `engine_*` parameters which are forwarded to the built-in [tartiflette](https://github.com/dailymotion/tartiflette) engine instance.
```python
from aiohttp import web
from tartiflette_aiohttp import Application
sdl = """
type Query {
hello(name: String): String
}
"""
ctx = {
'user_service': user_service
}
app = Application(
engine_sdl=sdl,
engine_schema_name="default",
executor_context=user_service,
executor_http_endpoint='/graphql',
executor_http_methods=['POST', 'GET']
)
web.run_app(app)
```
**Parameters**:
* **engine_sdl**: Contains the [Schema Definition Language](https://graphql.org/learn/schema/)
- Could be a string which contains the SDL
- Could be an array of string, which contain the SDLs
- Could be a path of an SDL
- Could be an array of paths which contain the SDLs
* **engine_schema_name**: Name of the schema used the built-in engine.
* **executor_context**: Context which will be passed to each resolver. Be default, the context passed to each resolvers, will contain these properties.
- **req**: Request object from aiohttp
- **app**: Application object from aiohttp
* **executor_http_endpoint**: Endpoint where the GraphQL Engine will be attached, by default on `/graphql`
* **executor_http_methods**: HTTP Method where the GraphQL Engine will be attached, by default on **POST** and **GET**.
### Use with custom Tartiflette engine
In the case you already have a Tartiflette Engine instance, or, you do not want to use the built-in instance. You can pass an existing instance to the Application constructor.
```python
# main.py
from aiohttp import web
from tartiflette import Resolver, Engine
from tartiflette_aiohttp import Application
@Resolver("Query.hello")
async def resolver_hello(parent, args, ctx, info):
return "hello " + args["name"]
sdl = """
type Query {
hello(name: String): String
}
"""
engine = Engine(sdl)
ctx = {
'user_service': user_service
}
app = Application(
engine=engine,
executor_context=user_service,
executor_http_endpoint='/graphql',
executor_http_methods=['POST', 'GET']
)
web.run_app(app)
```
**Parameters**:
* **engine**: Tartiflette Engine instance
* **executor_context**: Context which will be passed to each resolver. Be default, the context passed to each resolvers, will contain these properties.
- **req**: Request object from aiohttp
- **app**: Application object from aiohttp
* **executor_http_endpoint**: Endpoint where the GraphQL Engine will be attached, by default on `/graphql`
* **executor_http_methods**: HTTP Method where the GraphQL Engine will be attached, by default on **POST** and **GET**.
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
Close
Hashes for tartiflette-aiohttp-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9dba0ca229238aa7d1a4b0d1fde9e0470068ba810225e4d1d3eb18bb6b8c9f6 |
|
MD5 | 9c3889157f74cb4a9289ccb0ba82013a |
|
BLAKE2b-256 | 6bbf7822403b29bbb4b293b791f41e751e4f85e91d2c0c9cac023cb9c682743f |
Close
Hashes for tartiflette_aiohttp-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cffca7376b239ed9d6b9ebecb32265525337d92b04ccf7c33ace30ab22b8d57a |
|
MD5 | 0d97090aebf19062d1b21da7dc75f718 |
|
BLAKE2b-256 | 0005dcd486739d0421fc8a8bbd6d1e48f03ff914dc8eea166ae5b24f9bdcdf92 |