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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file upload testcase with graphql issue. #848

Open
nishangTagline opened this issue Dec 9, 2024 · 0 comments
Open

file upload testcase with graphql issue. #848

nishangTagline opened this issue Dec 9, 2024 · 0 comments
Labels

Comments

@nishangTagline
Copy link

Bug Report

Node.js version: v18.20.4
OS version: macOS Monterey 12.7.5

Description

The following error occurs when attempting to make a POST request to the /graphql endpoint using Supertest:

Error: cannot POST /graphql (500)
          at Response.toError (/src/node_modules/superagent/src/node/response.js:110:17)
          at Response._setStatusProperties (/src/node_modules/superagent/src/response-base.js:107:48)
          at new Response (/src/node_modules/superagent/src/node/response.js:41:8)
          at Test._emitResponse (/src/node_modules/superagent/src/node/index.js:953:20)
          at IncomingMessage.<anonymous> (/src/node_modules/superagent/src/node/index.js:1166:38)
          at IncomingMessage.emit (node:events:525:35)
          at IncomingMessage.emit (node:domain:489:12)
          at endReadableNT (node:internal/streams/readable:1358:12)
          at processTicksAndRejections (node:internal/process/task_queues:83:21) {
        status: 500,
        text: 'POST body missing. Did you forget use body-parser middleware?',
        method: 'POST',
        path: '/graphql'
      }

Actual Behavior

A 500 error is returned with the message:

POST body missing. Did you forget use body-parser middleware?

FYI: The API is working fine with appolo graphql client and postman.

Expected Behavior

The mutation should execute successfully with the attached file being processed by the /graphql endpoint.

Steps to Reproduce

The error can be reproduced using the following test code:

export class IntegrationTestManager {
  public httpServer: any;

  private app: INestApplication;

  async beforeAll(): Promise<void> {
    const moduleRef = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    this.app = moduleRef.createNestApplication();
    await this.app.init();
    this.httpServer = this.app.getHttpServer();

  }

  async afterAll() {
    await this.app.close();
  }

  executeRESTRequest({ path, requestType }: { path: string, requestType: string }): Promise<any> {

    const restRequest = request(this.httpServer)[requestType](path);
    return restRequest.send();
  }

  executeGraphQLRequest({ query, variables, authToken }: { query: string, variables?: Object, authToken?: string }): Promise<any> {
    const graphqlRequest = request(this.httpServer).post('/graphql');

    if (authToken) {
      graphqlRequest.set('Authorization', authToken);
    }

    return graphqlRequest.send({
      query,
      variables,
    });
  }

  async getServerInstance() {
    return this.httpServer
  }
}
const filePath = path.join(__dirname, '1.png');
    const serverInstance = await integrationTestManager.getServerInstance()

const graphqlRequest = request(serverInstance).post('/graphql')
  .field('operations', jsonRequestBodyString)
  .set('Content-Type', 'multipart/form-data')
  .field("map", JSON.stringify({
    "0": ["variables.picUpload"],
  }))
  .attach("0", filePath)
  .set('Authorization', testingContext.users[user].token);

const testResponse = await graphqlRequest;

Notes for Maintainers

  • The issue seems related to missing or misconfigured middleware for processing multipart/form-data requests, such as body-parser or an equivalent package.
  • Ensure that the GraphQL server is configured to support file uploads and the appropriate middleware is applied before handling requests.

Request for Prioritization

This issue impacts the ability to test and upload files via the GraphQL API. A prompt resolution would be appreciated.

@nishangTagline nishangTagline changed the title [fix] DESCRIPTIVE TITLE file upload testcase with graphql issue. Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant