graphql - Javascript 中的 GraphQL 查询
问题描述
我目前正在尝试将我的 Postman 查询转移到生产中。我在这里有这个查询。
我想将此添加到作为 GH Action 运行的节点包中。
const core = require("@actions/core");
const github = require("@actions/github");
const { graphql, buildSchema } = require('graphql');
const run = async () => {
/** The token used to access Gatsby Cloud */
const gatsbyToken = core.getInput("gatsby-token");
/** The ID of the site to modify */
const siteID = core.getInput("gatsby-site-id");
/** The GraphQL schema used to query the existing ENV */
const schema = buildSchema(`
query AllEnvironmentVariablesForSite($id: UUID!) {
buildEnvironmentVariablesForSite: environmentVariablesForSite(
id: $id
runnerType: BUILDS
) {
key
value
truncatedValue
}
previewEnvironmentVariablesForSite: environmentVariablesForSite(
id: $id
runnerType: PREVIEW
) {
key
value
truncatedValue
}
}
`);
/** The variables associated to the GraphQL schema above */
const variables = {
id: siteID
}
console.log( { variables } )
try {
/** The existing ENV in Gatsby Cloud */
const existing = await fetch( 'https://api.gatsbyjs.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${gatsbyToken}`
},
body: JSON.stringify( { query, variables } )
} )
} catch ( error ) {
core.setFailed( error.message )
}
}
run()
当我在本地运行它时,我收到 UUID is an unknown type: 的错误Unknown type "UUID".
。
我该如何解决这个问题?
解决方案
这样做的问题是查询应该格式化为字符串。该问题的查询格式与 Express 服务器中的格式相同。
...
/** The GraphQL schema used to query the existing ENV */
const query = `
query AllEnvironmentVariablesForSite($id: UUID!) {
buildEnvironmentVariablesForSite: environmentVariablesForSite(
id: $id
runnerType: BUILDS
) {
key
value
truncatedValue
}
previewEnvironmentVariablesForSite: environmentVariablesForSite(
id: $id
runnerType: PREVIEW
) {
key
value
truncatedValue
}
}`
...
try {
/** The existing ENV in Gatsby Cloud */
const existing = await fetch( 'https://api.gatsbyjs.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${gatsbyToken}`
},
body: JSON.stringify( { query, variables } )
} )
const existingJSON = await existing.json()
console.log( existingJSON.data )
} catch ( error ) {
core.setFailed( error.message )
}
}
推荐阅读
- python - Selenium Chrome (Python):浏览器冻结 - 在这种情况下超时并退出浏览器
- c# - 统一。尝试将带有事件的按钮动态添加到网格 => 事件索引卡在索引 2
- javascript - 反转对象数组?
- events - Cytoscape 事件目标未定义
- python - 从 3.6 更新到 Python 3.7,但每当我尝试导入以前安装的 Python3 包(例如 networkx)时都会出现导入错误
- java - 在 Java 中对 ArrayList 进行排序
- php - 无法在 laravel 中插入数据
- r - 如何在数据框中找到由 6 个或更多 1 连接的 1 的数量?
- firebase - 对我的应用程序的 Firebase 规则感到困惑
- lisp - 如何在 common lisp 中递归地附加列表?