node.js - 如何在 Graphql 解析器中侦听类似 REST 的端点?
问题描述
在成功集成第 3 方 REST Oauth Api 后,现在我正在尝试将 api 逻辑转换为 graphql 查询,但我有点坚持如何最好地构建查询。
从 REST 的角度来看,我有这样的东西可以 100% 工作
app.get("/auth/redirect", async (req: Request, res:Response) => {
const code = req.query.code
const { access_token, account_id } = await getTokens({
code,
clientId,
clientSecret,
})
try{
const result = await axios
.get(
`https://api.specificservice.dev/helloworld/id/v1/accounts?accountId=${account_id}`,
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Bearer ${access_token}`,
},
}
)
console.log('Fetching user..', result.data)
return result.data
}
catch (error: any) {
let message = error.result.data
console.log(message)
return message
}
})
来自 Graphql,我有一个这样的草图。
getPlatform: async (_: any, _args: any, { req }: any) => {
const clientId = "test"
const clientSecret = "test"
const code = req.query.code
const { access_token, account_id } = await getTokens({
code,
clientId,
clientSecret,
})
try{
const result = await axios
.get(
`https://api.specificservice.dev/helloworld/id/v1/accounts?accountId=${account_id}`,
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Bearer ${access_token}`,
},
}
)
console.log('Fetching user..', result.data)
return result.data
}
catch (error: any) {
let message = error.result.data
console.log(message)
return message
}
},
我如何重构它以适应我在其余端点上尝试做的事情
解决方案
推荐阅读
- java - 无法在 Android 动态功能模块中运行检测测试?
- css - 自定义 CSS 到 rmarkdown 块以更改图形的宽度
- ruby - 使用 Ruby bot.rb:4:in 的高音机器人
': main:Object (NameError) 的未定义局部变量或方法 `twitter' - java - 使用 JENA 创建模型
- javascript - 警报处于活动状态时如何专注于选项卡?
- java - Java Spring可以在运行时添加或删除@Component吗?
- c++ - main 中 line 的等效语句是什么?
- c# - Telerik 下拉列表未正确填充
- java - 如何从哪个 JAR 中找到类加载器正在尝试加载特定类?
- c# - UserControl 中的 ItemsSource 绑定不起作用