From d4bf69c1024a0e2790d200ce27a964ee852d78ff Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 25 Oct 2020 12:31:33 +0100 Subject: [PATCH] enable CORS --- .idea/vcs.xml | 6 ++++++ .idea/watcherTasks.xml | 25 +++++++++++++++++++++++++ Pipfile | 2 +- README.md | 2 +- app/main.py | 14 ++++++++++++-- app/schema/types/todo.py | 1 + 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 .idea/watcherTasks.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml new file mode 100644 index 0000000..902e9bf --- /dev/null +++ b/.idea/watcherTasks.xml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/Pipfile b/Pipfile index 757106b..2a03e66 100644 --- a/Pipfile +++ b/Pipfile @@ -4,7 +4,7 @@ url = "https://pypi.org/simple" verify_ssl = true [dev-packages] -yapf = "*" +black = "*" [packages] fastapi = "*" diff --git a/README.md b/README.md index 785bc05..c2f73ff 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Todo GraphQL +# Python GraphQL API Python GraphQL API template application diff --git a/app/main.py b/app/main.py index c0ef885..b785f78 100644 --- a/app/main.py +++ b/app/main.py @@ -1,11 +1,21 @@ import graphene from fastapi import FastAPI from starlette.graphql import GraphQLApp +from starlette.middleware.cors import CORSMiddleware from app.schema.mutations.mutations import Mutations from app.schema.queries.todo import TodoQuery +origins = ["*"] + app = FastAPI() +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) app.add_route( - "/", - GraphQLApp(schema=graphene.Schema(query=TodoQuery, mutation=Mutations))) + "/", GraphQLApp(schema=graphene.Schema(query=TodoQuery, mutation=Mutations)) +) diff --git a/app/schema/types/todo.py b/app/schema/types/todo.py index 3d81832..624b9ca 100644 --- a/app/schema/types/todo.py +++ b/app/schema/types/todo.py @@ -25,5 +25,6 @@ class ResponseField(graphene.ObjectType): is_success = graphene.Boolean(default_value=True) error_message = graphene.String() + class TodoResponseField(ResponseField): todo = graphene.Field(TodoType)