mirror of
https://github.com/rjNemo/graphql_python_template
synced 2026-06-06 02:26:47 +00:00
consolidate CRUD operations
This commit is contained in:
parent
d4bf69c102
commit
0049fe0eee
9 changed files with 40 additions and 17 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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)
|
)
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue