fetch graphql with curl

어쩌다가?

  • api 설명서를 만들어달라고 해서...

query

curl \
  -X POST \
  -H "x-api-key: xxx-xxxx" \
  -H "Content-Type: application/json" \
  -d '{ "query": "query { listUsers { name } }" }' \
  https://localhost:5000/graphql

mutation with variables

curl \
  -X POST \
  -H "x-api-key: xxx-xxxx" \
  -H "Content-Type: application/json" \
  -d '{ "query": "mutation($user: UserInput!) { createUser(user: $user) { name location id } }" ,
        "variables": {
          "user": {
            "name": "Nader Dabit",
            "location": "United States",
            "id": "001"
          }
        }
      }' \
  https://localhost:5000/graphql

with Axios

const getLoginData = async () => {
  const query = `
    mutation loginEmail($email: String!, $passwd: String!) {
      loginEmail(email: $email, passwd: $passwd) {
        ok
        token
        error
        isConfirmed
      }
    }
  `
  const variables = { email: '[email protected]', passwd: 'test12!!!' }
  const headers = { 'Content-Type': 'application/json' }

  const loginEmailData = await axios.post('http://localhost:8000/graphqlsandbox', {
    query,
    variables,
    headers,
  })
  console.log('get loginEmailData: ', loginEmailData)
}

좋은 웹페이지 즐겨찾기