1
2import { lexicographicSortSchema, printSchema } from "graphql";
3
4export const pothosSchema = builder.toSchema();
5
6
7export const pothosSchemaString = printSchema(lexicographicSortSchema(pothosSchema));
8
9
10
11import express from 'express';
12import { createYoga } from 'graphql-yoga';
13import { fromNodeHeaders } from '@whatwg-node/server';
14import { auth } from './auth';
15import { prisma } from './prismaClient';
16import { PothosBuilderGenericType, builder, pothosSchema, pothosSchemaString } from './graphql/builder';
17
18
19const app = express();
20const port = process.env.PORT || 4000;
21
22
23const yoga = createYoga<{
24 req: express.Request;
25 res: express.Response;
26}>({
27
28 renderGraphiQL: () => {
29
30 return `
31 <!DOCTYPE html>
32 <html lang="en">
33 <body style="margin: 0; overflow-x: hidden; overflow-y: hidden">
34 <div id="sandbox" style="height:100vh; width:100vw;"></div>
35 <script src="https://embeddable-sandbox.cdn.apollographql.com/_latest/embeddable-sandbox.umd.production.min.js"></script>
36 <script>
37 new window.EmbeddedSandbox({
38 target: "#sandbox",
39 initialEndpoint: "http://localhost:${port}/graphql", // Dynamic port
40 });
41 </script>
42 </body>
43 </html>`;
44 },
45 schema: pothosSchema,
46
47 context: async (ctx): Promise<PothosBuilderGenericType['Context']> => {
48 try {
49 const session = await auth.api.getSession({
50 headers: fromNodeHeaders(ctx.req.headers),
51 });
52 if (!session?.user) {
53 return {
54 currentUser: undefined,
55 };
56 }
57
58 return {
59 currentUser: {
60 id: session.user.id,
61 email: session.user.email ?? undefined,
62 name: session.user.name ?? undefined,
63 },
64 };
65 } catch (error) {
66 console.error("Error resolving context:", error);
67 return { currentUser: undefined };
68 }
69 },
70 graphiql: true,
71 logging: true,
72 cors: true,
73});
74
75
76
77app.use(yoga.graphqlEndpoint, yoga);
78
79
80builder.queryType({
81 fields: (t) => ({
82 hello: t.string({
83 resolve: () => "Hello world!",
84 }),
85
86 }),
87});
88
89
90
91
92app.listen(port, () => {
93 console.log(`🚀 Server ready at http://localhost:${port}/graphql`);
94 console.log(`🚀 GraphQL Playground available at http://localhost:${port}/graphql`);
95});