调用graphQL服务器突变到axios url

时间:2019-01-29 01:42:13

标签: javascript reactjs graphql axios

我目前在客户端使用GraphQL Server和axios。 我想知道如何在我的axios上调用这个graphQL

mutation {
 createUser(email: "hello@gmail.com") {
   email
 }
}

我怎么这样称呼它?

const res = await axios.get('http://localhost:5000/public?query={users{email}');

1 个答案:

答案 0 :(得分:0)

您需要使用post方法将查询/突变发送到GraphQL服务器:

axios({
  // Of course the url should be where your actual GraphQL server is.
  url: 'http://localhost:5000/graphql',
  method: 'post',
  data: {
      query: `
        mutation createUser($email: email){
            createUser(email: $email) {
               email
            }
        }`,
     variables: {
        email: "hello@gmail.com"
     }
  }
}).then((result) => {
    console.log(result.data)
});