aws-lambda - TypeGraphQl:使用 Netlify 函数/AWS Lambda
问题描述
经过一天的工作,我终于能够让 TypeQL 与 Netlify Functions / AWS Lambda 一起工作,查看文档和示例,最后绝望的蛮力。
我在这里为其他人分享我的工作代码(或供我自己的 :P 将来参考),因为它包含一些违反直觉的关键字用法。
正常进近
使用简单示例时,我不断遇到的错误是:
Your function response must have a numerical statusCode. You gave: $ undefined
我当然搜索了这些问题,但没有一个建议的解决方案对我有用。
解决方案
工作代码
import 'reflect-metadata'
import { buildSchema } from 'type-graphql'
import { ApolloServer } from 'apollo-server-lambda'
import { RecipeResolver } from 'recipe-resolver'
async function lambdaFunction() {
const schema = await buildSchema({
resolvers: [RecipeResolver],
})
const server = new ApolloServer({
schema,
playground: true,
})
// !!! NOTE: return (await ) server.createHandler() won't work !
exports.handler = server.createHandler()
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! NOTE: weird but only way to make it work with
// AWS lambda and netlify functions (netlify dev)
// also needs a reload of the page (first load of playground won't work)
lambdaFunction()
// exports.handler = lambdaFunction wont work
// export { lambdaFunction as handler } wont work
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
我也从这个简单的例子中得到了一些反射错误
Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for argument named 'title' of 'recipe' of 'RecipeResolver
所以我必须弄清楚如何将显式类型添加到@Arg
:
// previous:
// recipe(@Arg('title') title: string)
// fixed:
recipe( @Arg('title', (type) => String) title: string
推荐阅读
- google-cloud-platform - 没有 gcloud 或任何库/客户端的云 dns api 身份验证?
- python - 如何将钟摆转换为 datetime.datetime 类型?
- typescript - 热重载中断 WebGL2RenderingContext
- java - 尽管删除了“0”值的代码,为什么列表中还剩下一个“0”值?
- wordpress - Wordpress wp_options:“家”问题
- java - Arquillian java.lang.IllegalStateException:启动测试 WebContext 身份验证时出错
- c# - 我如何在 C# 中获取 Steam 应用程序的 URL
- wcf - 使用 DNS 通过 Internet 托管 WCF 服务
- postgresql - 输入到 postgres 数据库的数据以某种方式被更改?
- android - Android 相当于 textViewShouldBeginEditing