2017-12-10 17 views
0

GraphQL 끝점에서 일부 데이터를 가져 오려고하지만 "계속 쿼리 문자열을 제공해야합니다."라는 오류 메시지가 계속 나타납니다. 쿼리 문자열을 제공한다고 확신합니다.이 오류는 어디서 발생 했습니까?GraphQL API에서 가져올 쿼리 문자열 제공

엔드 포인트는 다음과 같습니다이 경우 https://antserver-blocjgjbpw.now.sh/graphql

const query = `{ 
 
    ants { 
 
    name 
 
    color 
 
    length 
 
    weight 
 
    } 
 
}` 
 

 
document.getElementById("basicFetchButton").addEventListener("click",() => { 
 
\t fetch("https://antserver-blocjgjbpw.now.sh/graphql", { 
 
    \t method: 'POST', 
 
\t \t 'Content-Type': 'application/graphql', 
 
    body: JSON.stringify({query}) 
 
    }) 
 
    .then(res => res.json()) 
 
    .then(data => console.log(data)) 
 
})

답변

1

, 당신은 Content-Type: 'application/json'을 사용하고 싶습니다. application/graphql은 쿼리의 전체 본문을 쿼리로 요구하지만 쿼리 변수를 보낼 수 없으므로이 방법을 사용하지 않는 것이 좋습니다. 여기

전체 코드 예제입니다 : 나의 기사를 체크 아웃, 더 많은 예제를 들어

fetch('https://antserver-blocjgjbpw.now.sh/graphql', { 
    method: 'POST', 
    headers: { 'Content-Type': 'application/json' }, 
    body: JSON.stringify({ query }), 
}) 
    .then(res => res.json()) 
    .then(res => console.log(res.data)); 

4 simple ways to call a GraphQL API