consolidate CRUD operations

This commit is contained in:
Ruidy 2020-10-30 12:28:06 +01:00
parent d4bf69c102
commit 0049fe0eee
9 changed files with 40 additions and 17 deletions

View file

@ -1,4 +1,5 @@
import graphene import graphene
import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from starlette.graphql import GraphQLApp from starlette.graphql import GraphQLApp
from starlette.middleware.cors import CORSMiddleware from starlette.middleware.cors import CORSMiddleware
@ -19,3 +20,7 @@ app.add_middleware(
app.add_route( app.add_route(
"/", GraphQLApp(schema=graphene.Schema(query=TodoQuery, mutation=Mutations)) "/", GraphQLApp(schema=graphene.Schema(query=TodoQuery, mutation=Mutations))
) )
if __name__ == "__main__":
uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)

View file

@ -12,4 +12,4 @@ class CreateTodo(graphene.Mutation):
def mutate(self, info, title: str): def mutate(self, info, title: str):
todo = create_todo(title) todo = create_todo(title)
return CreateTodo(todo=todo) return CreateTodo(todo=todo)

View file

@ -12,4 +12,4 @@ class DeleteTodo(graphene.Mutation):
def mutate(self, info, todo_id: str): def mutate(self, info, todo_id: str):
todo = delete_todo(todo_id) todo = delete_todo(todo_id)
return DeleteTodo(todo=todo) return DeleteTodo(todo=todo)

View file

@ -13,4 +13,4 @@ class UpdateTodo(graphene.Mutation):
def mutate(self, info, todo: Todo): def mutate(self, info, todo: Todo):
res = update_todo(todo.todo_id, todo.__dict__) res = update_todo(todo.todo_id, todo.__dict__)
return UpdateTodo(todo=res) return UpdateTodo(todo=res)

View file

@ -1,9 +1,6 @@
from typing import List
import graphene import graphene
from app.models.todo import Todo from app.schema.types.todo import TodoResponseField, TodoListResponseField
from app.schema.types.todo import TodoType, TodoResponseField
from app.usecases import read_all_todos, read_todo_by_id from app.usecases import read_all_todos, read_todo_by_id
@ -11,18 +8,29 @@ class TodoQuery(graphene.ObjectType):
""" """
Defines the query and how to interact with Defines the query and how to interact with
""" """
list_todos = graphene.Field(graphene.List(TodoType))
def resolve_list_todos(self, info) -> List[Todo]: list_todos = graphene.Field(TodoListResponseField)
return read_all_todos()
get_todo = graphene.Field(TodoResponseField, def resolve_list_todos(self, info) -> TodoListResponseField:
todo_id=graphene.String(required=True)) try:
todos = read_all_todos()
is_success = True
error_message = None
except Exception as e:
error_message = str(e)
is_success = False
todos = None
return TodoListResponseField(
todos=todos, is_success=is_success, error_message=error_message
)
get_todo = graphene.Field(TodoResponseField, todo_id=graphene.String(required=True))
def resolve_get_todo(self, info, todo_id: str) -> TodoResponseField: def resolve_get_todo(self, info, todo_id: str) -> TodoResponseField:
todo, is_success = read_todo_by_id(todo_id) todo, is_success = read_todo_by_id(todo_id)
error_message = "This element does not exist." if not is_success else None error_message = "This element does not exist." if not is_success else None
return TodoResponseField(todo=todo, return TodoResponseField(
is_success=is_success, todo=todo, is_success=is_success, error_message=error_message
error_message=error_message) )

View file

@ -28,3 +28,11 @@ class ResponseField(graphene.ObjectType):
class TodoResponseField(ResponseField): class TodoResponseField(ResponseField):
todo = graphene.Field(TodoType) todo = graphene.Field(TodoType)
class TodoListResponseField(ResponseField):
todos = graphene.Field(graphene.List(TodoType))
class CreateTodoResponseField(ResponseField):
todo = graphene.Field(TodoType)

View file

@ -2,3 +2,4 @@ from .create_todo import create_todo
from .delete_todo import delete_todo from .delete_todo import delete_todo
from .read_all_todos import read_all_todos from .read_all_todos import read_all_todos
from .read_todo_by_id import read_todo_by_id from .read_todo_by_id import read_todo_by_id
from .update_todo import update_todo

View file

@ -1,6 +1,7 @@
from app.models.todo import Todo
from app.repositories.todos import todo_exists, remove_todo from app.repositories.todos import todo_exists, remove_todo
def delete_todo(todo_id: str) -> None: def delete_todo(todo_id: str) -> Todo:
if todo_exists(todo_id): if todo_exists(todo_id):
return remove_todo(todo_id) return remove_todo(todo_id)

View file

@ -5,4 +5,4 @@ from app.repositories.todos import edit_todo, todo_exists
def update_todo(todo_id: str, data) -> Todo: def update_todo(todo_id: str, data) -> Todo:
if todo_exists(todo_id): if todo_exists(todo_id):
return edit_todo(todo_id, data) return edit_todo(todo_id, data)
return None return None