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

我共享适用于我的的代码

// File: graphql.ts
import 'reflect-metadata'
import { buildSchema } from 'type-graphql'
import { ApolloServer } from 'apollo-server-lambda'
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core'
import { RecipeResolver } from './recipe-resolver'
export const createHandler = async function(){
const schema = await buildSchema({
resolvers: [RecipeResolver],
})
const server = new ApolloServer({
schema,
introspection: true,
plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
})
return server.createHandler()
}
export const handler = async function(event, context, callback) {
const graphqlHandler = await createHandler()
return await graphqlHandler(event, context, callback)
}
// Lambda: graphql.handler
  • 节点16.x
  • 类型graphql^1.1.1
  • graphql^15.3.0
  • apollo服务器lambda:^3.10.2

相关内容

  • 没有找到相关文章

最新更新