James Jeramiah
James Jeramiah

Reputation: 3

Setting Pisma, Graphql, typegraphql-prisma project in a serverless aws lambda project

I am trying to configure a project that users Prisma, Graphql and serverless aws lambda. I am to use typegraphql-prisma.

This is my serverless.ts file. This was created when I am initilizing the project with 'serverless create --template aws-nodejs-typescript --path serverless-graphql-api' command. I did some modifications to the original file in order to make serverless work as expected.

import type { AWS } from '@serverless/typescript';

import hello from '@functions/hello';

const serverlessConfiguration: AWS = {
  service: 'my-service',
  frameworkVersion: '3',
  plugins: ['serverless-offline', 'serverless-esbuild'],
  provider: {
    name: 'aws',
    runtime: 'nodejs14.x',
    apiGateway: {
      minimumCompressionSize: 1024,
      shouldStartNameWithService: true,
    },
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
      NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000',
    },
  },
  // import the function via paths
  functions: { hello },
  package: { individually: true },
  custom: {
    esbuild: {
      bundle: true,
      minify: false,
      sourcemap: true,
      exclude: ['aws-sdk'],
      target: 'node14',
      define: { 'require.resolve': undefined },
      platform: 'node',
      concurrency: 10,
    },
  },
};

module.exports = serverlessConfiguration;

This is my schema,prisma `

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "rhel-openssl-1.0.x"]
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

generator typegraphql {
  provider = "typegraphql-prisma"
}

model Post {
  id       String @id @default(auto()) @map("_id") @db.ObjectId
  slug     String @unique
  title    String
  body     String
  author   User   @relation(fields: [authorId], references: [id])
  authorId String @db.ObjectId
}

model User {
  id      String   @id @default(auto()) @map("_id") @db.ObjectId
  email   String   @unique
  name    String?
  address Address?
  posts   Post[]
}

// Address is an embedded document
type Address {
  street String
  city   String
  state  String
  zip    String
}

enum NotificationType {
  TYPE1
  TYPE2
}

When I excure 'npx prisma generate', resolvers are generated by typegraphql-prisma. What I want is to use this resolvers and Prisma Client to work with aws serverlss lambda. `

I follwed the steps in the documentation https://www.apollographql.com/docs/apollo-server/deployment/lambda/ but I am stuck at below problem. In documentation, serverlss.ts file is as follows. serverless.ts

import { ApolloServer } from '@apollo/server';
import { startServerAndCreateLambdaHandler, handlers } from '@as-integrations/aws-lambda';

const typeDefs = `#graphql
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

// This final export is important!
export const graphqlHandler = startServerAndCreateLambdaHandler(
  server,
  // We will be using the Proxy V2 handler
  handlers.createAPIGatewayProxyEventV2RequestHandler(),
);

Here, there are no asynchronous executions which return a promise. But when I am using typegraphql-prisma I have to build the Apollo server like this.

    const schema = await buildSchema({
        resolvers,
        validate: false,
    });

    const prisma = new PrismaClient();

    const server = new ApolloServer<Context>({
        schema, // from previous step
    });

Since I cannot use await in row modules, I cannot use the way specifcied in the documentation.

If my approach in wrong, please guide me to an correct approach to use Prisma, Graphql, typegraphql-prisma in a serverless aws lambda project.

Upvotes: 0

Views: 300

Answers (1)

James Jeramiah
James Jeramiah

Reputation: 3

There is a very simple solution for this. There is a synchronous method for building the schema which can be used in server.ts

modified server.ts

const schema = buildSchemaSync({
`  resolvers,
   validate: false,
});
const prisma = new PrismaClient();

const server = new ApolloServer({
    schema,
});

export const graphqlHandler = startServerAndCreateLambdaHandler(
    server,
    handlers.createAPIGatewayProxyEventV2RequestHandler(),
);

Upvotes: 0

Related Questions