首页 > 解决方案 > Gatsby 源 API 服务器转换属性名称并不允许 GraphQL 查询

问题描述

Gatsby Source API 服务器运行良好。在我的 gatsby-config 中有这个:

    {
        resolve: 'gatsby-source-apiserver',
        options: {
            typePrefix: 'community_education__',
            url: `https://spreadsheets.google.com/feeds/list/1DLAVN3q758sPohCFeZlVSVRZKXzEser1SIsQnH2mvrw/ogwtdyp/public/basic?hl=en_US&alt=json`,
            method: 'get',
            name: `classes`,
            entityLevel: `feed.entry`,
            schemaType: classType,
            localSave: true,
            path: `${__dirname}/api/`,
            verboseOutput: true,
        }
    }

它正确地拉下我想要的数据。问题是我正在使用的 API (google) 带回了以下属性:

{
    "title": {
      "type": "text",
      "$t": "Computer Coding for Kids"
    },
    "content": {
      "type": "text",
      "$t": "district: Hopkins, days: Mon - Thurs, startdate: 6/11/2018, enddate: 6/14/2018, time: 9am - Noon, grades: 3rd, 4th, 5th, 6th Grades, description: Learn how to code through playing games and having fun! We'll learn the fundamentals of loops, if statements, and variables as we learn the blockly and python computer languages!  Please come with internet navigation skills (basic typing and mouse control) and a passion to work hard and have fun!, link: https://hopkins.ce.eleyo.com/course/6064/youth-summer-2018/computer-coding-for-kids"
    }
  }

当我进行 GraphQL 查询时,可用的属性是type并且alternative__t实际上是可以的,除了alternative__t总是返回null.

这个查询:

{
  allCommunityEducationClasses {
    totalCount
    edges {
      node {
        title {
          type
          alternative__t
        }
        content {
          type
          alternative__t
        }
      }
    }
  }
}

返回此结果:

    {
      "node": {
        "title": {
          "type": "text",
          "alternative__t": null
        },
        "content": {
          "type": "text",
          "alternative__t": null
        }
      }
    },
    {
      "node": {
        "title": {
          "type": "text",
          "alternative__t": null
        },
        "content": {
          "type": "text",
          "alternative__t": null
        }
      }
    },

我知道问题出在$t,如果它是我自己的 API 并且我可以更改响应名称属性,我会的,但这是 google API。我可以以某种方式重命名该属性吗?有没有办法使用转义字符来查询$t

标签: graphqlgatsby

解决方案


似乎图书馆正在检查特殊字符,但不检查$. 我可以通过将 in 更改为在我的代码中修复它,.replace.replace(/-|__|:|\$|\.|\s/g, '_');解决normalize.js了我的问题。

我也提出了拉取请求。https://github.com/thinhle-agilityio/gatsby-source-apiserver/pull/1


推荐阅读