【问题标题】:Fetch data from api with graphql [duplicate]使用graphql从api获取数据[重复]
【发布时间】:2021-03-21 18:07:15
【问题描述】:

我正在尝试连接到一个网站,他们给了我:

Url: https://api.demo...
headers:"x-api-key" and "x-api-user"

我不知道如何与他们建立联系,我尝试了以下代码:

const query = ` 
query {
   some query here
    }
`;
const url = ``

const opts = {
  method: "POST",
  headers: { "x-api-key": ,
    "x-api-user": , },
  body: JSON.stringify({ query })
};

这是正确的方法吗?当我运行它时npm start=> App Crashes我是 javascript 新手,我什至不知道如何在 google 中进行搜索,有人可以指导我使用教程、链接或以正确的方式回复吗?

感谢您的理解

【问题讨论】:

    标签: javascript node.js graphql graphql-js


    【解决方案1】:

    您可以使用 fetch 调用来做到这一点。

    const query = `query {
      Some query
    }`;
     
    fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: JSON.stringify({
        query
      })
    })
      .then(r => r.json())
      .then(data => console.log('data returned:', data));
    

    【讨论】:

    • 这将引发引用错误,因为未定义 fetch
    • @Quentin 你解决了我的问题吗?请这不一样...我应该把我的凭据放在哪里?
    • @Quentin 你能告诉我吗,我没有回复
    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多