Dan Borstelmann
11/03/2021, 9:46 PMnikolasburk
Dan Borstelmann
11/04/2021, 5:40 PMimport { 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:
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-*'Dan Borstelmann
11/04/2021, 5:46 PMDan Borstelmann
11/04/2021, 5:47 PMDan Borstelmann
11/04/2021, 5:47 PMimport { 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 }]);Dan Borstelmann
11/04/2021, 5:59 PMDan Borstelmann
11/04/2021, 6:25 PM"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"
}Dan Borstelmann
11/05/2021, 5:59 AM