|
| 1 | +const { ServiceBroker } = require("moleculer"); |
| 2 | +const { MoleculerClientError } = require("moleculer").Errors; |
| 3 | + |
| 4 | +const ApiGateway = require("moleculer-web"); |
| 5 | +const { ApolloService } = require("../../index"); |
| 6 | + |
| 7 | +const fetch = require("node-fetch"); |
| 8 | + |
| 9 | +describe("Integration test for greeter service", () => { |
| 10 | + const broker = new ServiceBroker({ logger: false }); |
| 11 | + |
| 12 | + let port; |
| 13 | + const apiSvc = broker.createService({ |
| 14 | + name: "api", |
| 15 | + |
| 16 | + mixins: [ |
| 17 | + // Gateway |
| 18 | + ApiGateway, |
| 19 | + |
| 20 | + // GraphQL Apollo Server |
| 21 | + ApolloService({ |
| 22 | + // API Gateway route options |
| 23 | + routeOptions: { |
| 24 | + path: "/graphql", |
| 25 | + cors: true, |
| 26 | + mappingPolicy: "restrict", |
| 27 | + }, |
| 28 | + |
| 29 | + // https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html |
| 30 | + serverOptions: {}, |
| 31 | + }), |
| 32 | + ], |
| 33 | + |
| 34 | + settings: { |
| 35 | + port: 0, // Random |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + broker.createService({ |
| 40 | + name: "greeter", |
| 41 | + |
| 42 | + actions: { |
| 43 | + hello: { |
| 44 | + graphql: { |
| 45 | + query: "hello: String!", |
| 46 | + }, |
| 47 | + handler() { |
| 48 | + return "Hello Moleculer!"; |
| 49 | + }, |
| 50 | + }, |
| 51 | + welcome: { |
| 52 | + graphql: { |
| 53 | + query: ` |
| 54 | + welcome(name: String!): String! |
| 55 | + `, |
| 56 | + }, |
| 57 | + handler(ctx) { |
| 58 | + return `Hello ${ctx.params.name}`; |
| 59 | + }, |
| 60 | + }, |
| 61 | + /*update: { |
| 62 | + graphql: { |
| 63 | + subscription: "update: String!", |
| 64 | + tags: ["TEST"], |
| 65 | + }, |
| 66 | + handler(ctx) { |
| 67 | + return ctx.params.payload; |
| 68 | + }, |
| 69 | + },*/ |
| 70 | + |
| 71 | + danger: { |
| 72 | + graphql: { |
| 73 | + query: "danger: String!", |
| 74 | + }, |
| 75 | + async handler() { |
| 76 | + throw new MoleculerClientError( |
| 77 | + "I've said it's a danger action!", |
| 78 | + 422, |
| 79 | + "DANGER" |
| 80 | + ); |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + beforeAll(async () => { |
| 87 | + await broker.start(); |
| 88 | + port = apiSvc.server.address().port; |
| 89 | + }); |
| 90 | + afterAll(() => broker.stop()); |
| 91 | + |
| 92 | + it("should call the greeter.hello action", async () => { |
| 93 | + const res = await fetch(`http://localhost:${port}/graphql`, { |
| 94 | + method: "post", |
| 95 | + body: JSON.stringify({ |
| 96 | + operationName: null, |
| 97 | + variables: {}, |
| 98 | + query: "{ hello }", |
| 99 | + }), |
| 100 | + headers: { "Content-Type": "application/json" }, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(res.status).toBe(200); |
| 104 | + expect(await res.json()).toStrictEqual({ |
| 105 | + data: { |
| 106 | + hello: "Hello Moleculer!", |
| 107 | + }, |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should call the greeter.welcome action with parameter", async () => { |
| 112 | + const res = await fetch(`http://localhost:${port}/graphql`, { |
| 113 | + method: "post", |
| 114 | + body: JSON.stringify({ |
| 115 | + operationName: null, |
| 116 | + variables: {}, |
| 117 | + query: 'query { welcome(name: "GraphQL") }', |
| 118 | + }), |
| 119 | + headers: { "Content-Type": "application/json" }, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(res.status).toBe(200); |
| 123 | + expect(await res.json()).toStrictEqual({ |
| 124 | + data: { |
| 125 | + welcome: "Hello GraphQL", |
| 126 | + }, |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it("should call the greeter.welcome action with query variable", async () => { |
| 131 | + const res = await fetch(`http://localhost:${port}/graphql`, { |
| 132 | + method: "post", |
| 133 | + body: JSON.stringify({ |
| 134 | + operationName: null, |
| 135 | + variables: { name: "Moleculer GraphQL" }, |
| 136 | + query: "query ($name: String!) { welcome(name: $name) }", |
| 137 | + }), |
| 138 | + headers: { "Content-Type": "application/json" }, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(res.status).toBe(200); |
| 142 | + expect(await res.json()).toStrictEqual({ |
| 143 | + data: { |
| 144 | + welcome: "Hello Moleculer GraphQL", |
| 145 | + }, |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should call the greeter.danger and receives an error", async () => { |
| 150 | + const res = await fetch(`http://localhost:${port}/graphql`, { |
| 151 | + method: "post", |
| 152 | + body: JSON.stringify({ |
| 153 | + operationName: null, |
| 154 | + variables: {}, |
| 155 | + query: "query { danger }", |
| 156 | + }), |
| 157 | + headers: { "Content-Type": "application/json" }, |
| 158 | + }); |
| 159 | + |
| 160 | + expect(res.status).toBe(200); |
| 161 | + expect(await res.json()).toStrictEqual({ |
| 162 | + data: null, |
| 163 | + errors: [ |
| 164 | + { |
| 165 | + extensions: { |
| 166 | + code: "INTERNAL_SERVER_ERROR", |
| 167 | + exception: { |
| 168 | + code: 422, |
| 169 | + retryable: false, |
| 170 | + type: "DANGER", |
| 171 | + }, |
| 172 | + }, |
| 173 | + locations: [ |
| 174 | + { |
| 175 | + column: 9, |
| 176 | + line: 1, |
| 177 | + }, |
| 178 | + ], |
| 179 | + message: "I've said it's a danger action!", |
| 180 | + path: ["danger"], |
| 181 | + }, |
| 182 | + ], |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments