Skip to content

Commit

Permalink
feat(api): Even better
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien 'Bubu' Busi committed Dec 7, 2018
1 parent 86e4ef4 commit f6552c6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 98 deletions.
55 changes: 26 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# main.py
from aiohttp import web
from tartiflette import Resolver
from tartiflette_aiohttp import GraphqlHandlers
from tartiflette_aiohttp import register_graphql_handlers

@Resolver("Query.hello")
async def resolver_hello(parent, args, ctx, info):
Expand All @@ -29,11 +29,12 @@ sdl = """
}
"""

app = web.Application()

GraphqlHandlers.register_app(webapp=app, engine_sdl=sdl)

web.run_app(app)
web.run_app(
register_graphql_handlers(
web.Application(),
engine_sdl=sdl
)
)
```

Save the file and start the server.
Expand Down Expand Up @@ -79,7 +80,7 @@ The basic and common way to create an `aiohttp` instance is to use the `Applicat

```python
from aiohttp import web
from tartiflette_aiohttp import Application
from tartiflette_aiohttp import register_graphql_handlers

sdl = """
type Query {
Expand All @@ -91,18 +92,16 @@ ctx = {
'user_service': user_service
}

app = web.Application()

GraphqlHandlers.register_app(
webapp=app,
engine_sdl=sdl,
engine_schema_name="default",
executor_context=ctx,
executor_http_endpoint='/graphql',
executor_http_methods=['POST', 'GET']
web.run_app(
register_graphql_handlers(
app=web.Application(),
engine_sdl=sdl,
engine_schema_name="default",
executor_context=ctx,
executor_http_endpoint='/graphql',
executor_http_methods=['POST', 'GET']
)
)

web.run_app(app)
```

**Parameters**:
Expand All @@ -127,7 +126,7 @@ In the case you already have a Tartiflette Engine instance, or, you do not want
# main.py
from aiohttp import web
from tartiflette import Resolver, Engine
from tartiflette_aiohttp import GraphqlHandlers
from tartiflette_aiohttp import register_graphql_handlers

@Resolver("Query.hello")
async def resolver_hello(parent, args, ctx, info):
Expand All @@ -145,17 +144,15 @@ ctx = {
'user_service': user_service
}

app = web.Application()

GraphqlHandlers.register_app(
webapp=app,
engine=engine,
executor_context=ctx,
executor_http_endpoint='/graphql',
executor_http_methods=['POST', 'GET']
web.run_app(
register_graphql_handlers(
app=web.Application(),
engine=engine,
executor_context=ctx,
executor_http_endpoint='/graphql',
executor_http_methods=['POST', 'GET']
)
)

web.run_app(app)
```

**Parameters**:
Expand Down
2 changes: 0 additions & 2 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ max-public-methods=30

max-attributes=15

max-parents=8

[FORMAT]
max-line-length=80

Expand Down
73 changes: 71 additions & 2 deletions tartiflette_aiohttp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,72 @@
from tartiflette_aiohttp.graphqlhandlers import GraphqlHandlers
from functools import partial
from typing import List

__all__ = ["GraphqlHandlers"]
from tartiflette import Engine
from tartiflette_aiohttp._handler import Handlers


def register_graphql_handlers(
app,
engine_sdl: str = None,
engine_schema_name: str = "default",
executor_context: dict = None,
executor_http_endpoint: str = "/graphql",
executor_http_methods: List[str] = None,
engine: Engine = None,
):
"""register a Tartiflette Engine to an app
Pass a SDL or an already initialized Engine, not both, not neither.
Keyword Arguments:
app {aiohttp.web.Application} -- The application to register to.
engine_sdl {str} -- The SDL defining your API (default: {None})
engine_schema_name {str} -- The name of your sdl (default: {"default"})
executor_context {dict} -- Context dict that will be passed to the resolvers (default: {None})
executor_http_endpoint {str} -- Path part of the URL the graphql endpoint will listen on (default: {"/graphql"})
executor_http_methods {list[str]} -- List of HTTP methods allowed on the endpoint (only GET and POST are supported) (default: {None})
engine {Engine} -- An already initialized Engine (default: {None})
Raises:
Exception -- On bad sdl/engine parameter combinaison.
Exception -- On unsupported HTTP Method.
Return:
The app object.
"""

if (not engine_sdl and not engine) or (engine and engine_sdl):
raise Exception(
"an engine OR an engine_sdl should be passed here, not both, not none"
)

if not executor_context:
executor_context = {}

executor_context["app"] = app

if not executor_http_methods:
executor_http_methods = ["GET", "POST"]

if not engine:
engine = Engine(engine_sdl, engine_schema_name)

app["ttftt_engine"] = engine

for method in executor_http_methods:
try:
app.router.add_route(
method,
executor_http_endpoint,
partial(
getattr(Handlers, "handle_%s" % method.lower()),
executor_context,
),
)
except AttributeError:
raise Exception("Unsupported < %s > http method" % method)

return app


__all__ = ["register_graphql_handlers"]
65 changes: 0 additions & 65 deletions tartiflette_aiohttp/graphqlhandlers.py

This file was deleted.

0 comments on commit f6552c6

Please sign in to comment.