import supertest from "supertest"; import { Module } from "@nimbus/core/dist"; import { gql } from 'apollo-server-express'; import { GraphQLBaseModule } from '../src/graphql.module'; import { GraphQLServer } from "../src/graphql.server"; describe("GraphQL module", () => { it ("Will bind custom graphql scalars", async () => { const {schemaAsync} = new Module({ imports: [GraphQLBaseModule], typeDefs: gql` type Query { scalars: Scalars } type Scalars { date: Date dateTime: DateTime time: Time uuid: UUID json: JSON } `, resolvers: { Query: { scalars() { const date = new Date(2019, 6, 5, 10, 50, 20, 20); return { date: date, dateTime: date, time: date, uuid: "a62c1c3c-b6de-4cba-9706-402a82c135e8", json: JSON.stringify({json: "json"}), } } } } }); const schema = await schemaAsync; const graphqlServer = new GraphQLServer({ server: { path: "/graphql" }, schema, }).prepare(); const response = await supertest(graphqlServer.express) .post("/graphql") .send({ 'query': ` query { scalars { date dateTime time uuid json } } ` }) .set('Content-Type', 'application/json') .set('X-Correlation-ID', 'test-id') .set('Accept', 'application/json') .expect(200); expect(response.body.data).toEqual({ "scalars": { "date": "2019-07-05", "dateTime": "2019-07-05T09:50:20.020Z", "json": "{\"json\":\"json\"}", "time": "09:50:20.020Z", "uuid": "a62c1c3c-b6de-4cba-9706-402a82c135e8" } }); }); it ("Will allow to use mock resolvers", async () => { const {schemaAsync} = new Module({ useMocks: true, typeDefs: gql` type Query { test: Test } type Test { id: String name: String subField: SubType } type SubType { type: String } `, mocks: { Query: { test() { return { id: "mock-id", name: "mock-name", }; }, }, Test: { subField() { return { type: "mock-subfield", } } } }, resolvers: { Query: { test() { return { id: "12", name: "test", }; }, }, Test: { subField() { return { type: "sub", } } } } }); const schema = await schemaAsync; const graphqlServer = new GraphQLServer({ server: { path: "/graphql" }, schema, }).prepare(); const response = await supertest(graphqlServer.express) .post("/graphql") .send({ 'query': ` query { test { id name subField { type } } } ` }) .set('Content-Type', 'application/json') .set('Accept', 'application/json') .expect(200); expect(response.body.data).toEqual({ "test": { "id": "mock-id", "name": "mock-name", "subField": { "type": "mock-subfield", }, } }); }); });