[Solved] Need Help testing Graph QL

Help!!

I’m new to Adonis Testing – got things going ok with one sample test to prove vow & a second to prove database access via a normal http/controller

The third test is for my GraphQL

I’m following a sample contributed by @vinicius-batista from a previous thread: Strategy for creating reusable test helpers

I am using a query tested with the ‘Insomnia’ client

With my code like this:

const { test, trait } = use('Test/Suite')('Graph-QL Contacts & Groups')

trait('Test/ApiClient')

test('create & delete contact', async ({ client }) => {

  const query = `  fetchEventType(id: 1){
    id
    name
  }
`
  const variables = {}

  const response = await client
    .post('/graphql')
    .send({ query, variables })
    .end()

  response.assertStatus(200)
  response.assertJSONSubset({
    name: 'test',
  })
})

I’m getting the Error: Test timeout, ensure “done()” is called

$ adonis test
info: serving app on http://127.0.0.1:4000

  Example
    ✓ make sure 2 + 2 is 4 (1ms)

  Graph-QL - run TestController
    ✓ get event id 4 (142ms)

  Graph-QL Contacts & Groups
superagent: Enable experimental feature http2
    ✖ create & delete contact (2s)

   ERRORS

  1. create & delete contact
  Error: Test timeout, ensure "done()" is called; if returning a Promise, ensure it resolves.
    at Test._parseError ([...]/nov-backend/node_modules/japa/src/Test.js:109:16)
    at [...]/nov-backend/node_modules/japa/src/Test.js:195:21

   FAILED

  total       : 3
  passed      : 2
  failed      : 1
  time        : 2s

I am afraid I don’t understand…

My routing is like this:

const GraphqlAdonis = use('ApolloServer')
const schema = require('../app/data/schema')
Route.get('/graphiql', ({ request, response }) => { return GraphqlAdonis.graphiql({ endpointURL: '/graphql' }, request, response) })
Route.route('/graphql', ({ request, auth, response }) => { return GraphqlAdonis.graphql({ schema, context: { auth } }, request, response) }, ['GET', 'POST'])
1 Like

Hi, Can you try this :

Hello and thanks for replying !!

i’ve tried adding the timeout and there is no difference except that it takes longer to timeout

I think that the problem is not in the response time, but that I am somehow not sending a perfect request

I know when I make a request from Insomnia that is incorrect then it just hangs and doesn’t come back

I’ve copied this request verbatim so that I know I’m working with a good query

So it’s something else… It’s something about how I’m asking…

I wish there were some more examples!!

1 Like

getting closer – now I can get a response when I format my graphql correctly (perhaps that old example was from a previous version of graphql)

so this:

const query = ‘fetchEventType(id: 1){ id name }’
const variables = {}

is now this:

const query = ‘query {fetchEventType(id: 1){ id name }}’
const variables = {}

and I get a response of type text

I need to figure out how to get back JSON (or parse it myself)

{“data”:{“fetchEventType”:{“id”:1,“name”:“open house”}}}

so my JSON assertion is now failing

response.assertJSONSubset({ “name”: “open house” })

and a text one succeeds

response.assertText(’{“data”:{“fetchEventType”:{“id”:1,“name”:“open house”}}}’)

1 Like

In your tests you can use JSON.parse(). Like :

...
const jsonResponse = JSON.parse(response)
// Your tests

or parse directly on your route :

Route.route('/graphql', ({ request, auth, response }) => { 
   return JSON.parse(GraphqlAdonis.graphql({ schema, context: { auth } }, request, response))
}, ['GET', 'POST'])

Be careful if in a specific case the result is not in JSON (I don’t know GraphQL well enough)

Thanks for your ideas, they helped me to find my solutions !!

I wrote a middleware to add a ‘ContentType: application/json’ header so that the browser will parse the json for me

'use strict'
class ContentTypeJSON {
  async handle(ctx, next) {
    ctx.response.response.setHeader('Content-Type', 'application/json')
    await next()
  }
}
module.exports = ContentTypeJSON

const namedMiddleware = {
json: ‘App/Middleware/ContentTypeJSON’,

Route.route(’/graphql’, ({ request, auth, response }) => { return GraphqlAdonis.graphql({ schema, context: { auth } }, request, response) }, [‘GET’, ‘POST’]).middleware([‘auth’, ‘json’])

1 Like

As per: adonis-apollo-server

There are a few problems with the npm hosted version, whose git hub repo is missing (and may be here: ammezie/ adonis-apollo-server

  1. it doesn’t return header - application/json
  2. it doesn’t return the statusCode on error

I combined those fixes and am making it avail for now at: ajoslin103/ adonis-apollo-server

2 Likes