MichalLytek / MichalLytek/type-graphql

docs: Usage with Netlify Functions/AWS Lambda

Open
#885 0 comments 0 reactions 0 assignees View on GitHub
Community :family_man_girl: Documentation :book:
Dominant language
TypeScript
Stars
8.1k
Forks
672
PR merge metrics
No merged PRs in 30d

Description

I was finally able to get TypeQL working with Netlify Functions / AWS Lambda after a day or work, going over the docs and examples, and in the end desperate brute force.

I'm sharing my working code here for others to help (or for future reference of my own :P ) as it contains some counterintuitive keyword usage.

## Normal Approach
The error I kept getting when using the [simple example](https://github.com/MichalLytek/type-graphql/tree/v1.1.1/examples/simple-usage) was:
```bash
Your function response must have a numerical statusCode. You gave: $ undefined
```
I searched of course in the issues, but none of the suggested solutions worked for me.

## Working Code

```typescript
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
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

```

Also I got some reflection errors from the simple example
```bash
Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for argument named 'title' of 'recipe' of 'RecipeResolver
```

So I had to figure out how to add explicit type to `@Arg`:
```ts
// previous:
// recipe(@Arg('title') title: string)
// fixed:
recipe( @Arg('title', (type) => String) title: string
```

Hope it helps, and maybe something to add to the docs for a Integrations / 'Netlify Functions/Lambda' page.

Contributor guide

Open the contributing guide

Research direction

Start with the linked examples/simple-usage example and compare it with the working TypeScript snippet in this issue. Document the Netlify Functions/AWS Lambda handler setup, the required explicit @Arg type, and any first-load caveat in an integrations or equivalent documentation page. Done means a reader can follow the page to run the example successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, graphql, typescript
Domain
api, cloud, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.