Hello, I've been deploying to serverless framework...
# orm-help
d
Hello, I've been deploying to serverless framework with apollo-server-lambda for awhile and everything is nice and snappy, I just added Prisma to the project with PrismaClient being added to the apollo server context and it's causing massive latency and what seems like stack overflows. Anyone seeing similar things recently?
n
Hey Dan 👋 Prisma Client is creating a connection to your database which usually adds a bit of overhead but it shouldn’t cause major latencies or any other issues. Can you maybe share a bit more about your project? Are there maybe lots of requests happening at the same time?
d
For sure! This is a greenfield project. I have been using prisma in production for awhile in a different project so I'm pretty familiar. It's a federated apollo graphql schema. However, the latency is in just one service below the gateway so it's not the gateway causing the issue. Here is all the code:
Copy code
import { ApolloServer } from 'apollo-server-lambda';
import { schema } from 'core';

const prisma = new PrismaClient();

export interface Context {
  prisma: PrismaClient;
}

export const createContext = (): Context => {
  return { prisma };
};

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

export const serverlessHandler = server.createHandler();
and the serverless file:
Copy code
org: dborstelmann
app: foo
service: bar
provider:
  name: aws
  runtime: nodejs14.x
  region: us-east-2
  timeout: 10
  environment:
    SERVICE_DATABASE_URL: ${self:custom.SERVICE_DATABASE_URL.${sls:stage}}
plugins:
  - serverless-offline
custom:
  SERVICE_DATABASE_URL:
    local: <postgresql://postgres:postgres@localhost/foobar_local>
    dev: blah
    test: blah
    prod: blah
  serverless-offline:
    httpPort: 4002
    lambdaPort: 5002
functions:
  graphql:
    handler: packages/${self:service}/dist/index.serverlessHandler
    events:
      - http:
          path: /api
          method: post
          cors: true
      - http:
          path: /api
          method: get
          cors: true
  package:
    patterns:
      - 'node_modules/.prisma/client/libquery_engine-rhel-*'
package:
  individually: true
  patterns:
    - '!node_modules/typescript'
    - '!node_modules/.bin/prisma*'
    - '!node_modules/.prisma/client/libquery_engine-*'
    - '!node_modules/@prisma/client/generator-build'
    - '!node_modules/@prisma/engines/introspection-engine-*'
    - '!node_modules/@prisma/engines/libquery_engine-*'
    - '!node_modules/@prisma/engines/migration-engine-*'
    - '!node_modules/@prisma/engines/prisma-fmt-*'
    - '!node_modules/prisma/libquery_engine-*'
Running the tsc compiled version with bland node works totally fine, but when I run it with serverless offline it feels like there is a stack overflow and it hangs for up to like 17s
Here is the schema, as you can see it's just a findMany on an empty database table, locally it takes like 20ms
Copy code
import { readFileSync } from 'fs';
import path from 'path';

import { buildSubgraphSchema } from '@apollo/federation';
import { gql, ApolloError } from 'apollo-server-lambda';
import { Context } from 'context';
import { Resolvers } from 'schema';

const typeDefs = gql(
  readFileSync(path.join(__dirname, '..', 'schema.graphql')).toString('utf-8'),
);

const resolvers: Resolvers = {
  Query: {
    locations: async (parent, args, context: Context) => {
      try {
        const locsFromDb = await context.prisma.location.findMany();
        return locsFromDb;
      } catch (e: unknown) {
        console.log('failed');
      }
    },
  },
};

export const schema = buildSubgraphSchema([{ resolvers, typeDefs }]);
I will also mention that it worked totally fine on lambda before I put Prisma in, just a blank apollo server. And adding prisma didn't show issues when running with node. But serverless on lambda blew it up
here are the prod deps btw:
Copy code
"dependencies": {
    "@apollo/federation": "^0.33.0",
    "@graphql-codegen/cli": "^2.2.1",
    "@graphql-codegen/typescript": "^2.2.3",
    "@graphql-codegen/typescript-resolvers": "^2.3.1",
    "@prisma/client": "^3.4.0",
    "apollo-server-lambda": "^3.4.0",
    "apollo-server": "^3.4.0",
    "graphql": "^15.6.0",
    "prisma": "^3.4.0"
  }