moleculerjs / moleculer-apollo-server

:rocket: Apollo GraphQL server for Moleculer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Testing GraphQL Services?

robodude666 opened this issue · 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",
},
});
});