【发布时间】:2022-01-16 05:09:35
【问题描述】:
我正在尝试通过使用 fetch 方法来获取架构来发出 GraphQL 请求。 在我的设置中的 AWSAppSync 上,我看到了 API URL 和 API ID,但我不知道如何正确配置我的 fetch POST 请求以获取包含我需要的数据的响应。
这是我尝试使用的代码:
fetch('https://qnwb7jaoxdyky6egh4.appsync-api.us-east-1.amazonaws.com/graphql', {
method: 'POST',
headers: {
Authorization: 'Basic bnwak7alhw52rlioy', <----- this is API ID from AWS AppSync settings
'Content-Type': 'application/json'
},
body: JSON.stringify({
variables: {},
query: `
{
__schema {
types {
kind
name
possibleTypes {
name
}
}
}
}
`,
}),
})
.then((result) => result.json())
.then((result) => {
debugger; <--------- here I'm expecting to see the response data, but I see an error "UnauthorizedException"
// here we're filtering out any type information unrelated to unions or interfaces
const filteredData = result.data.__schema.types.filter(
(type) => type.possibleTypes !== null,
);
result.data.__schema.types = filteredData;
fs.writeFile('./fragmentTypes.json', JSON.stringify(result.data), (err) => {
if (err) {
console.error('Error writing fragmentTypes file', err);
} else {
console.log('Fragment types successfully extracted!');
}
});
});
我正在使用 AWS AppSync 设置中的 API URL 和 API ID。
我不知道为什么,但我收到一条错误消息:“消息:“无法解析 JWT 令牌。”
【问题讨论】:
标签: javascript reactjs amazon-web-services graphql aws-appsync