Skip to content

Using this in a serverless environment (e.g. apollo-lambda-server) #96

@darbio

Description

@darbio

How would I use this library in a lambda execution model?

I tried to implement it as follows, but on the second (and subsequent) calls to the endpoint, I get an error which states:

Schema must contain unique named types but contains multiple types named "Person"

I assume that this is because the schema is being re-built on every request, and therefore creating duplicated types?

handler.ts

import 'reflect-metadata';

import { graphqlLambda } from 'apollo-server-lambda';
import { buildSchema } from 'type-graphql';

import { PeopleController } from './controllers/people/peopleController';

export async function graphqlHandler(event, context, callback) {
    function callbackWithHeaders(error, output) {
        output.headers['Access-Control-Allow-Origin'] = '*';
        callback(error, output);
    }

    const schema = await buildSchema({
        resolvers: [PeopleController]
    });

    const handler = graphqlLambda({ schema: schema });
    return handler(event, context, callbackWithHeaders);
}

person.ts:

import { ID, Field, ObjectType } from 'type-graphql';

@ObjectType()
export class Person {
    @Field() name: string;
    @Field() creationDate: Date;
}

peopleController.ts

import { Arg, Query, Resolver } from 'type-graphql';
import { Person } from './person';

@Resolver()
export class PeopleController {
    items: Person[] = [
        {
            name: 'darbio',
            creationDate: new Date()
        }
    ];

    @Query(returns => [Person], { description: 'Get all the people' })
    async people(): Promise<Person[]> {
        return await this.items;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Question ❔Not future request, proposal or bug issueSolved ✔️The issue has been solved

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions