Api 获取 gatsby 在 GraphQL 中产生错误

Api fetch on gatsby producing error in GraphQL

这是我在 gatsby-node.js 中尝试的代码,使用 gatsby develop 与 graphql 交互...我正在尝试从区块链索引器获取数据以显示在我的网站上。

const fetch = require('node-fetch');

const NODE_TYPE = 'objkt';

exports.sourceNodes = async ({ actions, createContentDigest, createNodeId }) => {
  const { createNode } = actions;

  const response = await fetch('https://staging.api.tzkt.io/v1/bigmaps/523/keys?value.issuer=tz1V9ZviaGUWZjGx4U7cGYFEyUGyqpFnVGXx&active=true');
  const json = await response.json();
  const { results = [] } = json;

  const objkt = await Promise.all(results.map(async result => {
    const { url } = result;
    const objResponse = await fetch(url);
    return await objResponse.json();
  }));

  objkt.forEach((node, index) => {
    createNode({
      ...node,
      id: createNodeId(`${NODE_TYPE}-${node.id}`),
      parent: null,
      children: null,
      internal: {
        type: NODE_TYPE,
        content: JSON.stringify(node),
        contentDigest: createContentDigest(node)
      }
    });
  });
};

产生错误:

{
  "errors": [
    {
      "message": "Syntax Error: Expected Name, found \"}\".",
      "locations": [
        {
          "line": 4,
          "column": 3
        }
      ],
      "stack": [
        "GraphQLError: Syntax Error: Expected Name, found \"}\".",

data I'm trying to source

我很困惑为什么会发生这个错误...

解决方案:

const fetch = require("node-fetch")
 
const NODE_TYPE = `objkt`
 
exports.sourceNodes = async ({
  actions,
  createContentDigest,
  createNodeId,
}) => {
  const { createNode } = actions
 
  const response = await fetch(
    "https://staging.api.tzkt.io/v1/bigmaps/523/keys?value.issuer=tz1V9ZviaGUWZjGx4U7cGYFEyUGyqpFnVGXx&active=true"
  )
  const objkt = await response.json()
 
  objkt.forEach((node, index) => {
    createNode({
      ...node,
      id: createNodeId(`${NODE_TYPE}-${node.id}`),
      parent: null,
      children: [],
      internal: {
        type: NODE_TYPE,
        content: JSON.stringify(node),
        contentDigest: createContentDigest(node),
      },
    })
  })
}
 
exports.onPreInit = () => console.log("Loaded gatsby-node")

GraphQL 代码:

query MyQuery {
  objkt {
    value {
      issuer
      objkt_id
      objkt_amount
      xtz_per_objkt
    }
    internal {
      content
      contentDigest
    }
  }
}