【问题标题】:GraphQL post request in axiosaxios 中的 GraphQL 发布请求
【发布时间】:2019-03-19 20:59:45
【问题描述】:

我对 GraphQL 有疑问。我想向我的服务器发送 axios.post 请求。我可以在邮递员中做到这一点:

{
    "query":"mutation{updateUserCity(userID: 2, city:\"test\"){id name age city knowledge{language frameworks}}} "
}

在graphiql中:

mutation {
  updateUserCity(userID: 2, city: "test") {
    id
    name
    age
    city
    knowledge {
      language
      frameworks
    }
  }
}

但不能在我的代码中做到这一点:((这是我的代码 sn-p:

const data = await axios.post(API_URL, {
  query: mutation updateUserCity(${ id }: Int!, ${ city }: String!) {
    updateUserCity(userID: ${ id }, city: ${ city }){
      id
      name
      age
      city
      knowledge{
        language
        frameworks
      }
    }
  }
}, {
    headers: {
      'Content-Type': 'application/json'
    }
  })

我的代码有什么问题?

【问题讨论】:

标签: reactjs graphql axios


【解决方案1】:

对于 WordPress GQL https://docs.wpgraphql.com/getting-started/posts/ 此刻,这似乎对我有用 Nuxt

async asyncData() {
const data = {
  query: `query GET_POSTS($first: Int) {
    posts(first: $first) {
      edges {
        node {
          postId
          title
          excerpt
          date
          content
          author {
            node {
              username
            }
          }
        }
      }
    }
  }`,
  variables: {
    first: 5
  }
}

const options = {
  method: 'POST',
  headers: { 'content-type': 'application/json' },
  data: data,
  url: 'http://localhost/graphql'
};
    
try {
  const response = await axios(options);

  console.log(response.data.data.posts.edges);
  console.log(response.data.data.posts.edges.length);
} catch (error) {
  console.error(error);
}

},

【讨论】:

    【解决方案2】:

    我喜欢使用下面的语法,它类似于公认的答案,但更明确。

    请注意,variables 对象嵌套在 data 对象内,并且是 query 对象的兄弟。

    const data = await axios({
      url: API_URL,
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
        // any other headers you require go here
      },
      data: {
        query: `
          mutation updateUserCity($id: Int!, $city: String!) {
            updateUserCity(userID: $id, city: $city){
              id
              name
              age
              city
              knowledge{
                language
                frameworks
              }
            }
          }
        `,
        variables: {
          id: 2,
          city: 'Test'
        }
      }
    });
    

    【讨论】:

      【解决方案3】:

      我想和你分享我的简单工作示例

                  let id = "5c9beed4a34c1303f3371a39";
                  let body =  { 
                      query: `
                          query {
                              game(id:"${id}") {
                                  _id
                                  title
                              }
                          }
                      `, 
                      variables: {}
                  }
                  let options = {
                      headers: {
                          'Content-Type': 'application/json'
                      }
                  }
                  axios.post('http://localhost:3938/api/v1/graphql',body, options)
                      .then((response)=>{
                          console.log(response);
                      });
      

      【讨论】:

      • 这不是应该的方式。 GraphQL 有一个专用的变量接口,当输入包含引号时,这很容易中断。
      • 这对我有用,只是做了一些修改。谢谢!
      • 对变量使用字符串文字是在滥用graphql,远不安全/可读/可管理
      【解决方案4】:

      要在请求中传递的query 参数的值必须是字符串,并且传递给 GraphQL 查询的变量名称应以 $ 为前缀。您已将字符串文字用于请求中的变量。此外,可以使用variables 键在发布请求中传递变量。

      将您的代码更改为以下内容应该可以使其正常工作:

      const data = await axios.post(API_URL, {
        query: `mutation updateUserCity($id: Int!, $city: String!) {
          updateUserCity(userID: $id, city: $city){
            id
            name
            age
            city
            knowledge{
              language
              frameworks
            }
          }
        }`,
        variables: {
          id: 2,
          city: 'Test'
        }
      }, {
          headers: {
            'Content-Type': 'application/json'
          }
        })
      

      【讨论】:

      • 我必须将花括号包裹在反引号中的变量中以使其工作
      • 如果参数是自定义类型怎么办?那怎么办呢?
      • @DevAKS:我不明白您所说的自定义类型是什么意思。你能分享一下例子吗?
      • @Raeesaa 您在突变中接受参数作为 Int 和 String 变量,因此假设您有用户定义的类型,例如 - Location {lat: Int, lng: Int}
      【解决方案5】:

      您似乎正在尝试将变量传递到查询中。除非我错了,否则您在 axios 调用中的内容与其他内容不同。

      const data = await axios.post(API_URL, {
        query: `
          updateUserCity(userID: ${id}, city:${city}){
            id
            name
            age
            city
            knowledge{
              language
              frameworks
            }
          }
        `
      }, {
        headers: {
          'Content-Type': 'application/json'
        }
      });
      

      我相信这应该可行,假设您在此调用之前定义了变量 idcity

      【讨论】:

      • 这不是应该的方式。 GraphQL 有一个专用的变量接口,当输入包含引号时,这很容易中断。
      猜你喜欢
      • 2022-12-10
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 2019-02-25
      • 2020-11-19
      • 2021-09-01
      相关资源
      最近更新 更多