Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test graphql api in fastify #664

Closed
raviSussol opened this issue Apr 11, 2022 · 8 comments
Closed

How to test graphql api in fastify #664

raviSussol opened this issue Apr 11, 2022 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@raviSussol
Copy link

馃挰 Question here

How to test graphql api in fastify app? There are lots of example for the rest api to test in fastify app but for the graphql based app how can I test using testing method like fastify.inject? An online guide for testing in fastify here doesn't contain for graphql api test. Any examples if anybody can share would really be appreciated. 馃檹

// No idea. Looking for demo code to test graphql api

If possible add a link to sample codes for error reproduction (github or replit)

Your Environment

  • node version: 16
  • fastify version: >=3.23.1
  • os: Mac
@raviSussol raviSussol added the help wanted Extra attention is needed label Apr 11, 2022
@RafaelGSS
Copy link
Member

See: https://github.com/mercurius-js/mercurius/blob/master/test/routes.js#L12

@raviSussol
Copy link
Author

@RafaelGSS Thanks for the link. I'm able to test graphql api using fastify.inject now. But I have a bit different scenario to test graphql api. I have a login auth api which at first listens /api/v1/graphql and then it redirects to the /graphql route. And if I use below code to test this login auth then the test only happens for the first route i.e. /api/v1/graphql and then stops. Say the last line expect(login.statusCode).toBe(200); throws an unexpected error because of wrong http status code is returned.

const mut = `
      mutation {
        login(input: { username: "test", password: "test" }) {
          success
          message
        }
      }
    `;

const login = await app.inject({
      url: 'api/v1/graphql',
      method: 'POST',
      payload: { query: mut },
    });
expect(login.statusCode).toBe(200);

Do you have any idea what I'm missing here?

@mcollina
Copy link
Member

inject() does not follow redirects, so you'll have to follow that manually.

@raviSussol
Copy link
Author

@mcollina Can you give me a small use case of that?

@mcollina
Copy link
Member

Unfortunately I can't. Your use case is really specific to your application. Unfortunately I do not have time atm to reconstruct it.

@raviSussol
Copy link
Author

@mcollina OK does inject method has any redirect property (like supertest has one - a redirect chain method) which can trace request redirection if the calling route has to serve? I'm able to achieve this test using supertest but I'm wondering as if the fastify has a built in support for this in its testing framework.

@simoneb
Copy link

simoneb commented Jul 4, 2022

@raviSussol fastify.inject doesn't follow redirects. In order to do that manually you will need to detect that a response is a redirect (301/302 status code) and then manually request the url contained in the Location response header.

@simoneb
Copy link

simoneb commented Jul 4, 2022

@fastify/core unless you intend to implement support for redirects in .inject, this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants