Skip to content

Instantly share code, notes, and snippets.

@briggleman
Last active January 9, 2024 19:04
Show Gist options
  • Save briggleman/0b422351aa7bcb797c71af887fa75c5f to your computer and use it in GitHub Desktop.
Save briggleman/0b422351aa7bcb797c71af887fa75c5f to your computer and use it in GitHub Desktop.
example of setting up graphql with tartiflette and cors
import os
import logging
import logging.config
import aiohttp_cors
from aiohttp import web
from tart_test.helpers import config
from tartiflette_aiohttp import register_graphql_handlers
logging.config.dictConfig(config.loader("logging.yaml"))
log = logging.getLogger(__name__)
async def on_startup(app):
cors = aiohttp_cors.setup(app, defaults={"*": aiohttp_cors.ResourceOptions(allow_credentials=True, expose_headers="*", allow_headers="*", allow_methods=["GET", "POST", "OPTIONS"])})
for resource in app.router.resources():
cors.add(resource)
def run():
app = web.Application()
app.on_startup.append(on_startup)
web.run_app(
register_graphql_handlers(
app=app,
engine_sdl=f"{os.path.dirname(os.path.abspath(__file__))}/sdl",
engine_modules=[
"tart_test.resolvers.query",
"tart_test.resolvers.mutation",
"tart_test.resolvers.subscription",
"tart_test.directives.auth",
"tart_test.directives.rate_limiting"
],
subscription_ws_endpoint="/ws",
executor_http_endpoint="/graphql",
executor_http_methods=["POST"],
graphiql_enabled=True
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment