• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    해결됨

section27 quiz 질문입니다.

23.06.05 15:42 작성 조회수 285

0

알려주신데로 한거 같은데

반응이 성공적이지 않았다 400 상태 코드를 받았다고 alert창에 뜨는데.. 어떤게 문제 인지 잘 모르겠네요 ㅠㅠ

 

네트워크 페이로드를 보면 다 잘 들어간거같은데...

페이로드랑 코드입니다.

  1. {operationName: "createProduct",…}

    1. operationName: "createProduct"

    2. query: "mutation createProduct($seller: String, $createProductInput: CreateProductInput!) {\n createProduct(seller: $seller, createProductInput: $createProductInput) {\n id\n _typename\n }\n}"

    3. variables: {seller: "김갑수", createProductInput: {pName: "안경", contents: "멋진 안경", price: "15000"}}

      1. createProductInput: {pName: "안경", contents: "멋진 안경", price: "15000"}

        1. contents: "멋진 안경"

        2. pName: "안경"

        3. price: "15000"

      2. seller: "김갑수"

import { gql, useMutation } from "@apollo/client";
import { useState } from "react";

const CREATE_PRODUCT = gql`
  mutation createProduct(
    $seller: String
    $createProductInput: CreateProductInput!
  ) {
    createProduct(seller: $seller, createProductInput: $createProductInput) {
      _id
    }
  }
`;

export default function Boards_05_quiz() {
  //js
  const [seller, setSeller] = useState("");
  const [pName, setPname] = useState("");
  const [contents, setContents] = useState("");
  const [price, setPrice] = useState("");

  const [createProduct] = useMutation(CREATE_PRODUCT);

  const onChangeSeller = (event) => {
    setSeller(event.target.value);
  };
  const onChangePname = (event) => {
    setPname(event.target.value);
  };
  const onChangeContents = (event) => {
    setContents(event.target.value);
  };
  const onChangePrice = (event) => {
    setPrice(event.target.value);
  };

  const onClickHandler = async () => {
    try {
      const result = await createProduct({
        variables: {
          seller,
          createProductInput: {
            pName,
            contents,
            price,
          },
        },
      });
      console.log(result);
    } catch (error) {
      alert(error.message);
    }
  };

  return (
    //html
    <div>
      <input
        type="text"
        placeholder="판매자명을 입력해주세요"
        onChange={onChangeSeller}
      ></input>
      <input
        type="text"
        placeholder="상품명을 입력해주세요"
        onChange={onChangePname}
      ></input>
      <input
        type="text"
        placeholder="삼품내용을 입력해주세요"
        onChange={onChangeContents}
      ></input>
      <input
        type="text"
        placeholder="상품가격을 입력해주세요"
        onChange={onChangePrice}
      ></input>
      <button onClick={onClickHandler}>상품등록</button>
    </div>
  );
}

답변 1

답변을 작성해보세요.

1

안녕하세요! 우혁님!

API를 요청하기 위해선, 이미 백엔드 개발자분께서 만들어주신 API명세서를 그대로 따라주셔야합니다!

아래는 playground에서 확인할 수 있는 API 명세서에요!

image

위 API 명세서를 확인해보면, pName, contents 등의 필드는 없는 것으로 확인이 되네요!
API명세서를 보면서 만들어 주세요!

지우혁님의 프로필

지우혁

질문자

2023.06.07

  1. 0: "GraphQLError: Variable \"$createProductInput\" got invalid value \"15000\" at \"createProductInput.price\"; Int cannot represent non-integer value: \"15000\""

  2. 1: " at /newbizcode_backend_common/node_modules/graphql/execution/values.js:116:15"

  3. 2: " at coerceInputValueImpl (/newbizcode_backend_common/node_modules/graphql/utilities/coerceInputValue.js:132:9)"

  4. 3: " at coerceInputValueImpl (/newbizcode_backend_common/node_modules/graphql/utilities/coerceInputValue.js:106:34)"

  5. 4: " at coerceInputValueImpl (/newbizcode_backend_common/node_modules/graphql/utilities/coerceInputValue.js:56:14)"

  6. 5: " at coerceInputValue (/newbizcode_backend_common/node_modules/graphql/utilities/coerceInputValue.js:39:10)"

  7. 6: " at loop (/newbizcodebackend_common/node_modules/graphql/execution/values.js:109:69)"

  8. 7: " at coerceVariableValues (/newbizcode_backend_common/node_modules/graphql/execution/values.js:121:16)"

  9. 8: " at getVariableValues (/newbizcode_backend_common/node_modules/graphql/execution/values.js:50:19)"

  10. 9: " at buildExecutionContext (/newbizcode_backend_common/node_modules/graphql/execution/execute.js:205:61)"

  11. 10: " at executeImpl (/newbizcode_backend_common/node_modules/graphql/execution/execute.js:103:20)"

  12. locations: [{line: 1, column: 41}]

    1. 0: {line: 1, column: 41}

      1. column: 41

      2. line: 1

  13. message: "Variable \"$createProductInput\" got invalid value \"15000\" at \"createProductInput.price\"; Int cannot represent non-integer value: \"15000\""

이런 에러가 나서 혹시나 setPrice 즉 입력받은 price가 int 가 아닌 string으로 들어가서 에러가 발생 하는걸까요??

네 맞아요! 우혁님!
보여주신 에러 메시지에 이런 문장이 들어있네요!^^

Int cannot represent non-integer value: \"15000\""