|
1 |
| -import path from 'node:path'; |
| 1 | +import { resolve } from 'node:path'; |
| 2 | +import { sql } from 'drizzle-orm'; |
| 3 | +import { drizzle, NodePgDatabase } from 'drizzle-orm/node-postgres'; |
| 4 | +import { migrate } from 'drizzle-orm/postgres-js/migrator'; |
2 | 5 | import { createYoga, YogaServerInstance } from 'graphql-yoga';
|
3 |
| -import { PrismaClient } from '@prisma/client'; |
4 |
| -import { DbDrop, MigrateDev } from '@prisma/migrate'; |
5 |
| -import type { GraphQLContext } from '../src/context'; |
6 |
| -import { schema } from '../src/schema'; |
| 6 | +import pg from 'pg'; |
| 7 | +import type { GraphQLContext } from '../src/context.js'; |
| 8 | +import { schema } from '../src/schema.js'; |
| 9 | + |
| 10 | +const connectionString = |
| 11 | + process.env['PG_CONNECTION_STRING'] ?? |
| 12 | + 'postgres://postgres:postgres@localhost:5432/postgres?currentSchema=integration-tests'; |
| 13 | + |
| 14 | +export async function resetDatabase(db: NodePgDatabase, schema: string) { |
| 15 | + // sql query for resetting the database |
| 16 | + const query = sql` |
| 17 | + DROP SCHEMA IF EXISTS ${sql.raw(schema)} CASCADE; |
| 18 | + CREATE SCHEMA ${sql.raw(schema)}; |
| 19 | + `; |
| 20 | + await db.execute(query); |
| 21 | +} |
7 | 22 |
|
8 | 23 | describe('hackernews example integration', () => {
|
9 | 24 | let yoga: YogaServerInstance<GraphQLContext, GraphQLContext>;
|
| 25 | + let db: NodePgDatabase; |
| 26 | + let client: pg.Client; |
| 27 | + let currentSchema: string; |
10 | 28 | beforeAll(async () => {
|
11 |
| - const { createContext } = await import('../src/context'); |
12 |
| - yoga = createYoga({ schema, context: createContext }); |
13 |
| - |
14 |
| - // migrate |
15 |
| - await MigrateDev.new().parse([ |
16 |
| - `--schema=${path.resolve(__dirname, '..', 'prisma', 'schema.prisma')}`, |
17 |
| - ]); |
18 |
| - |
19 |
| - // seed |
20 |
| - const client = new PrismaClient(); |
21 |
| - await client.link.create({ |
22 |
| - data: { |
23 |
| - url: 'https://www.prisma.io', |
24 |
| - description: 'Prisma replaces traditional ORMs', |
25 |
| - }, |
| 29 | + const url = new URL(connectionString); |
| 30 | + const cs = url.searchParams.get('currentSchema'); |
| 31 | + if (!cs) { |
| 32 | + throw new Error("Must provide 'currentSchema' in the connection string"); |
| 33 | + } |
| 34 | + currentSchema = cs; |
| 35 | + client = new pg.Client(connectionString); |
| 36 | + await client.connect(); |
| 37 | + db = drizzle(client); |
| 38 | + await resetDatabase(db, currentSchema); |
| 39 | + await migrate(drizzle(client), { |
| 40 | + migrationsSchema: currentSchema, |
| 41 | + migrationsFolder: resolve(__dirname, '../src/drizzle'), |
26 | 42 | });
|
27 |
| - await client.$disconnect(); |
| 43 | + yoga = createYoga({ schema, context: { db } }); |
28 | 44 | });
|
29 | 45 |
|
30 | 46 | afterAll(async () => {
|
31 |
| - // drop |
32 |
| - await DbDrop.new().parse([ |
33 |
| - `--schema=${path.resolve(__dirname, '..', 'prisma', 'schema.prisma')}`, |
34 |
| - '--preview-feature', // DbDrop is an experimental feature |
35 |
| - '--force', |
36 |
| - ]); |
37 |
| - }); |
38 |
| - |
39 |
| - it('should get posts from feed', async () => { |
40 |
| - const response = await yoga.fetch('http://yoga/graphql?query={feed{url,description}}'); |
41 |
| - |
42 |
| - const body = await response.json(); |
43 |
| - expect(body).toMatchInlineSnapshot(` |
44 |
| - { |
45 |
| - "data": { |
46 |
| - "feed": [ |
47 |
| - { |
48 |
| - "description": "Prisma replaces traditional ORMs", |
49 |
| - "url": "https://www.prisma.io", |
50 |
| - }, |
51 |
| - ], |
52 |
| - }, |
53 |
| - } |
54 |
| - `); |
| 47 | + await resetDatabase(db, currentSchema); |
| 48 | + await client.end(); |
55 | 49 | });
|
56 | 50 |
|
57 | 51 | it('should create a new post', async () => {
|
|
0 commit comments