Skip to content

Latest commit

 

History

History
 
 

serverless

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Serverless example

This example presents how to deploy graphql schema based on decapi to serverless infrastructure like AWS lambda.

Schema is very simple:

query {
  hello(name: "Bob") # will resolve to 'Hello, Bob!'
}

As schema compiled by compileSchema is regular, normal GraphQLSchema, we can simply use it with apollo-server-lambda that will handle everything.

import { SchemaRoot, Query, compileSchema } from 'decapi'
import { graphqlLambda, graphiqlLambda } from 'apollo-server-lambda'

@SchemaRoot()
class MySchema {
  @Query()
  hello(name: string): string {
    return `hello, ${name}!`
  }
}

const schema = compileSchema(MySchema)

export function graphqlHandler(event, context, callback) {
  function callbackFilter(error, output) {
    output.headers['Access-Control-Allow-Origin'] = '*'
    callback(error, output)
  }
  const handler = graphqlLambda({ schema, tracing: true })
  return handler(event, context, callbackFilter)
}

export const graphiqlHandler = graphiqlLambda({ endpointURL: '/graphql' })