* 혹시 오류가 나거나 질문이 있으면 아래에 댓글을 달아주세요(오류코드 포함)
GraphQL로 조회하는 코드를 확인하려면 아래 링크를 클릭
https://zeuskwon-ds.tistory.com/75?category=1033905
[GraphQL] GraphQL로 API 구현 예제 with python - 조회
GraphQL 라이브러리 GraphQL 자체는 쿼리 언어이기 떄문에 구체화할 수있는 언어와 라이브러리가 필요 GraphQL의 라이브러리는 아래 링크를 참고하면 된다. https://graphql.org/code/ GraphQL Code Libraries, To..
zeuskwon-ds.tistory.com
* 생성코드는 조회코드에 이어서 작성합니다 위 링크에 있는 코드대로 셋팅되어있는지 확인
GraphQL 예제 - 생성(Create)기능
기존 REST API로 구현된 방법으로는 json타입 데이터를 POST요청으로 처리했었다.
이 기능을 GraphQL로 구현해 봄
- 기능 : Model테이블 데이터 생성
 
* 아래에 있는 코드는 기존 코드 그대로 두고 추가하면 됨
1. 스키마에 Mutation기능 추가
▷ schema.graphql
schema {
    query: Query
    mutation: Mutation
}
type Mutation {
    createModel(
        model_id: String!, 
        model_nm: String!, 
        model_type: String, 
        model_cmmt: String,
        rgst_dt: String, 
        rgst_time: String, 
        rgstr_nm: String, 
        del_yn: Boolean
    ): ModelResult!
}
2. Mutation Resolver 추가
▷ mutations.py
# mutations.py
from datetime import date
from ariadne import convert_kwargs_to_snake_case
from api import db
from api.models import Model
@convert_kwargs_to_snake_case
def create_model_resolver(obj, info, model_id, model_nm, model_type, model_cmmt, rgst_dt, rgst_time, rgstr_nm, del_yn):
    try:
        today = date.today()
        model = Model(
            model_id=model_id,model_nm=model_nm, model_type=model_type, model_cmmt=model_cmmt, rgst_dt=rgst_dt, rgst_time=rgst_time, rgstr_nm=rgstr_nm, del_yn=del_yn
        )
        db.session.add(model)
        db.session.commit()
        payload = {
            "success": True,
            "model": model.to_dict()
        }
    except ValueError:  # date format errors
        payload = {
            "success": False,
            "errors": [f"Incorrect date format provided. Date should be in "
                       f"the format dd-mm-yyyy"]
        }
    return payload
3. app 코드 추가
▷ app.py
from api.mutations import create_model_resolver
mutation = ObjectType("Mutation")
mutation.set_field("createModel", create_model_resolver)
schema = make_executable_schema(
    type_defs, query, mutation, snake_case_fallback_resolvers
)
- 실행
 
flask run
- 확인
 
http://127.0.0.1:5000/graphql 로 접속해서 데이터 Create 쿼리 전송
mutation {
  createModel(
    model_id: "test2", 
    model_nm:"v2",
    model_type:"RNN",
    model_cmmt:"zeustest",
    rgst_dt:"06/22",
    rgst_time:"04:56",
    rgstr_nm:"zeus",
    del_yn:false
    ) {
    model {
      model_id
      model_nm
      model_type
      model_cmmt
      rgst_dt
      rgst_time
      rgstr_nm
      del_yn 
    }
  }
}
아래 이미지처럼 입력쿼리에 대한 데이터가 생성되었다.


아래 링크를 통해서 삭제 코드 구현 가능
https://zeuskwon-ds.tistory.com/77?category=1033905
[GraphQL] GraphQL로 API 구현 예제 with python - 삭제
* 혹시 오류가 나거나 질문이 있으면 아래에 댓글을 달아주세요(오류코드 포함) GraphQL로 생성하는 코드를 확인하려면 아래 링크를 클릭 https://zeuskwon-ds.tistory.com/76?category=1033905 [GraphQL] GraphQL..
zeuskwon-ds.tistory.com
'BackEnd' 카테고리의 다른 글
| [GraphQL] GraphQL기술 회고 및 결론 (0) | 2022.06.30 | 
|---|---|
| [GraphQL] GraphQL로 API 구현 예제 with python - 삭제 (0) | 2022.06.30 | 
| [GraphQL] GraphQL로 API 구현 예제 with python - 조회 (0) | 2022.06.30 | 
| [GraphQL] GraphQL의 구조 및 쿼리 (0) | 2022.06.30 | 
| [GraphQL] GraphQL의 개념과 장단점 2 (0) | 2022.06.27 |