moleculerjs/moleculer-apollo-server

Testing GraphQL Services?

robodude666 opened this issue · 2 comments

I'm curious how you'd test a GraphQL service. For example, take the simple greeter service.

Sure, you could certainly call the actions and pass in params, but that wouldn't test the graphql definitions. To test that, would you need to add the ApolloService mixin to your test broker and then use a graphql client? Or is there a simpler way?

I think you can test it with a simple request lib which sends the GraphQL query as a REST request.

I've just added some integration test:

it("should call the greeter.welcome action with parameter", async () => {
const res = await fetch(`http://localhost:${port}/graphql`, {
method: "post",
body: JSON.stringify({
operationName: null,
variables: {},
query: 'query { welcome(name: "GraphQL") }',
}),
headers: { "Content-Type": "application/json" },
});
expect(res.status).toBe(200);
expect(await res.json()).toStrictEqual({
data: {
welcome: "Hello GraphQL",
},
});
});