|
| 1 | +import { DbDrop, MigrateDev } from '@prisma/migrate' |
| 2 | +import { PrismaClient } from '@prisma/client' |
| 3 | +import { createYoga } from 'graphql-yoga' |
| 4 | +import { schema } from '../src/schema' |
| 5 | +import { createContext } from '../src/context' |
| 6 | + |
| 7 | +describe('hackernews example integration', () => { |
| 8 | + beforeAll(async () => { |
| 9 | + // migrate |
| 10 | + await MigrateDev.new().parse([]) |
| 11 | + |
| 12 | + // seed |
| 13 | + const client = new PrismaClient() |
| 14 | + await client.link.create({ |
| 15 | + data: { |
| 16 | + url: 'https://www.prisma.io', |
| 17 | + description: 'Prisma replaces traditional ORMs', |
| 18 | + }, |
| 19 | + }) |
| 20 | + await client.$disconnect() |
| 21 | + }) |
| 22 | + |
| 23 | + afterAll(async () => { |
| 24 | + // drop |
| 25 | + await DbDrop.new().parse([ |
| 26 | + '--preview-feature', // DbDrop is an experimental feature |
| 27 | + '--force', |
| 28 | + ]) |
| 29 | + }) |
| 30 | + |
| 31 | + const yoga = createYoga({ schema, context: createContext }) |
| 32 | + |
| 33 | + it('should get posts from feed', async () => { |
| 34 | + const response = await yoga.fetch( |
| 35 | + 'http://yoga/graphql?query={feed{url,description}}', |
| 36 | + ) |
| 37 | + |
| 38 | + const body = await response.json() |
| 39 | + expect(body).toMatchInlineSnapshot(` |
| 40 | + { |
| 41 | + "data": { |
| 42 | + "feed": [ |
| 43 | + { |
| 44 | + "description": "Prisma replaces traditional ORMs", |
| 45 | + "url": "https://www.prisma.io", |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + } |
| 50 | + `) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should create a new post', async () => { |
| 54 | + const response = await yoga.fetch('http://yoga/graphql', { |
| 55 | + method: 'POST', |
| 56 | + headers: { 'content-type': 'application/json' }, |
| 57 | + body: JSON.stringify({ |
| 58 | + query: /* GraphQL */ ` |
| 59 | + mutation createPost { |
| 60 | + postLink( |
| 61 | + url: "https://www.the-guild.dev/graphql/yoga-server" |
| 62 | + description: "Time to Relax with GraphQL Yoga" |
| 63 | + ) { |
| 64 | + url |
| 65 | + description |
| 66 | + } |
| 67 | + } |
| 68 | + `, |
| 69 | + }), |
| 70 | + }) |
| 71 | + |
| 72 | + const body = await response.json() |
| 73 | + expect(body).toMatchInlineSnapshot(` |
| 74 | + { |
| 75 | + "data": { |
| 76 | + "postLink": { |
| 77 | + "description": "Time to Relax with GraphQL Yoga", |
| 78 | + "url": "https://www.the-guild.dev/graphql/yoga-server", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + `) |
| 83 | + }) |
| 84 | +}) |
0 commit comments