Skip to content

file upload testcase with graphql issue. #848

@nishangTagline

Description

@nishangTagline

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions