This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
/
index.ts
72 lines (66 loc) · 2.46 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { ApolloServer } from 'apollo-server-micro';
import { makeSchema } from 'nexus';
import path from 'path';
import { IncomingMessage } from 'http';
import { ports } from '@app/serverless/getEndpoint';
import { withCORS } from '@app/serverless/withCORS';
import { prisma } from '../../prisma/generated/prisma-client';
import * as schemaTypes from './schema';
import { Context } from './types';
import { getUser } from './getUser';
import { createModels } from './models';
const { PRISMA_ENDPOINT, PRISMA_SECRET, API_SECRET } = process.env;
if (!PRISMA_ENDPOINT || !PRISMA_SECRET || !API_SECRET)
throw Error(`Did you run 'yarn env dev'?`);
// Nullability
// https://blog.apollographql.com/designing-graphql-mutations-e09de826ed97
// https://blog.apollographql.com/using-nullability-in-graphql-2254f84c4ed7
// Remember
// For input arguments and fields, adding non-null is a breaking change.
// For output fields, removing non-null from a field is a breaking change.
// By id queries like web(id: ID!): Web! returns always non nullable value,
// because errors are passed as permissions exists or isWebCreatorOrAdmin etc.
const schema = makeSchema({
types: schemaTypes,
// All inputs are non nullable.
// Remember, after release, every new input must be nullable.
// Note payloads and errors needs nonNullDefaults: { output: false }.
nonNullDefaults: { input: true },
outputs: {
schema: path.join(__dirname, './schema.graphql'),
typegen: path.join(__dirname, './typegen.ts'),
},
typegenAutoConfig: {
// debug: true,
sources: [{ source: path.join(__dirname, './types.ts'), alias: 'types' }],
contextType: 'types.Context',
},
// backingTypeMap: {
// EmailAddress: 'string',
// DateTime: 'string',
// Max1024Chars: 'string',
// Max140Chars: 'string',
// URL: 'string',
// },
});
const server = new ApolloServer({
schema,
context: async ({ req }: { req: IncomingMessage }): Promise<Context> => {
if (req.headers.host == null) throw new Error('missing req.headers.host');
const user = await getUser(API_SECRET, prisma, req);
const models = createModels(prisma, user, req.headers.host);
return { models };
},
// Enable introspection and playground for production.
introspection: true,
playground: true,
});
const { IS_NOW } = process.env;
const handler = withCORS(
ports.api,
server.createHandler({
path: IS_NOW ? '/_api' : '/',
}),
);
// eslint-disable-next-line import/no-default-export
export default handler;