import { describe, beforeEach, afterEach, it, expect } from 'vitest'; import { Express } from 'express'; import path from 'path'; import request from 'supertest'; import { checkUpdates } from '../check-updates'; import { Config } from '../app'; import { init } from '../app'; import { remoteApplication1 } from '../test/remote-graphql-1'; import { remoteApplication2 } from '../test/remote-graphql-2'; describe('errors', () => { let remote1: Awaited>; let remote2: Awaited>; let app: Awaited>; beforeEach(async () => { const config: Config = { reloadInterval: 5000, list: [ { name: "Movies!", url: "http://localhost:8008/graphql", prefix: "movies_" }, { name: "Books!", url: "http://localhost:8009/graphql", prefix: "books_" } ], allowedQueriesAndMutations: ['getMovies', 'getBooks', 'getMoviesWithErrors'] }; remote1 = await remoteApplication1(); remote2 = await remoteApplication2(); app = await init({ config }); }); afterEach(async () => { app.stop(); remote1.server.close(); remote2.server.close(); }); it('shoudl be able to fetch data even after a normal graphql error', async () => { const errorResult = await request(app.app) .post('/graphql') .set('x-test', 'test') .send({ query: ` query { getMoviesWithErrors { movies { title } } } `, }); expect(errorResult.status).toEqual(200); // Directly after the error { const result = await request(app.app) .post('/graphql') .set('x-test', 'test') .send({ query: ` query { getMovies { movies { title } } getBooks { books { title } } } `, }); const { body, status } = result; expect(body.data.getMovies.movies).toMatchObject([ {title: 'The Shawshank Redemption'}, {title: 'The Godfather'}, {title: 'The Godfather: Part II'}, {title: 'The Dark Knight'}, ]); expect(body.data.getBooks.books).toMatchObject([ {title: 'The Awakening'}, {title: 'City of Glass'}, {title: 'The Great Gatsby'}, ]); } // Trigger an update await app.refresh(); // Get the data again { const result = await request(app.app) .post('/graphql') .set('x-test', 'test') .send({ query: ` query { getMovies { movies { title } } getBooks { books { title } } } `, }); const { body } = result; expect(body.data.getMovies.movies).toMatchObject([ {title: 'The Shawshank Redemption'}, {title: 'The Godfather'}, {title: 'The Godfather: Part II'}, {title: 'The Dark Knight'}, ]); expect(body.data.getBooks.books).toMatchObject([ {title: 'The Awakening'}, {title: 'City of Glass'}, {title: 'The Great Gatsby'}, ]); } }); it('should be able to work even if on eunderlying service goes down', async () => { remote1.server.close(); const result = await request(app.app) .post('/graphql') .set('x-test', 'test') .send({ query: ` query { getMovies { movies { title } } } `, }); //expect(result.status).toEqual(500); expect(result.body.errors[0].message).toContain('ECONN'); // Trigger an update await app.refresh(); // Fix underlying service remote1 = await remoteApplication1(); // Refresh the schema await app.refresh(); // Get the data again { const result = await request(app.app) .post('/graphql') .set('x-test', 'test') .send({ query: ` query { getMovies { movies { title } } getBooks { books { title } } } `, }); const { body } = result; expect(body.data.getMovies.movies).toMatchObject([ {title: 'The Shawshank Redemption'}, {title: 'The Godfather'}, {title: 'The Godfather: Part II'}, {title: 'The Dark Knight'}, ]); expect(body.data.getBooks.books).toMatchObject([ {title: 'The Awakening'}, {title: 'City of Glass'}, {title: 'The Great Gatsby'}, ]); } }); it('should be able to work even if on eunderlying service has a server error', async () => { remote1.setErrors(true); const result = await request(app.app) .post('/graphql') .set('x-test', 'test') .send({ query: ` query { getMovies { movies { title } } } `, }); expect(result.status).toEqual(200); expect(result.body.errors[0].message).toContain('ERROR'); // Trigger an update await app.refresh(); remote1.setErrors(false); // Refresh the schema await app.refresh(); // Get the data again { const result = await request(app.app) .post('/graphql') .set('x-test', 'test') .send({ query: ` query { getMovies { movies { title } } getBooks { books { title } } } `, }); const { body } = result; expect(body.data.getMovies.movies).toMatchObject([ {title: 'The Shawshank Redemption'}, {title: 'The Godfather'}, {title: 'The Godfather: Part II'}, {title: 'The Dark Knight'}, ]); expect(body.data.getBooks.books).toMatchObject([ {title: 'The Awakening'}, {title: 'City of Glass'}, {title: 'The Great Gatsby'}, ]); } }); });