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

POST requests #1

Closed
fwoelffel opened this issue Dec 31, 2017 · 4 comments
Closed

POST requests #1

fwoelffel opened this issue Dec 31, 2017 · 4 comments
Assignees

Comments

@fwoelffel
Copy link

Hi guys,

I started to use the GraphQL module and I must say that it is awesome. However, I had some difficulties to perform POST requests against my GraphQL endpoints (GET requests works out of the box).

I'm using the following packages:

"dependencies": {
    "@nestjs/common": "^4.5.4",
    "@nestjs/core": "^4.5.4",
    "@nestjs/graphql": "^2.0.0",
    "@nestjs/microservices": "^4.5.3",
    "@nestjs/testing": "^4.5.4",
    "@nestjs/websockets": "^4.5.3",
    ...
    "apollo-server-express": "^1.3.2",
    "graphql": "^0.11.7",
    "graphql-tools": "^2.11.0",
    ...
  }

Here is my GraphQL module implementation, according to the Nestjs documentation:

import {Module, NestModule, MiddlewaresConsumer, RequestMethod} from '@nestjs/common';
import {graphqlExpress, graphiqlExpress} from 'apollo-server-express';
import {GraphQLFactory, GraphQLModule} from '@nestjs/graphql';
import {SnippetResolvers} from "./snippet/snippet.resolvers";
import {SnippetModule} from "../snippet/snippet.module";

@Module({
  imports: [
    GraphQLModule,
    SnippetModule
  ],
  components: [
    SnippetResolvers
  ]
})
export class GQLModule implements NestModule {

  constructor(private readonly graphQLFactory: GraphQLFactory) {}

  configure(consumer: MiddlewaresConsumer) {
    const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
    const schema = this.graphQLFactory.createSchema({ typeDefs });
    consumer
      .apply(graphiqlExpress({ endpointURL: '/graphql' }))
      .forRoutes({ path: '/graphiql', method: RequestMethod.GET })
      .apply(graphqlExpress(req => ({ schema, rootValue: req })))
      .forRoutes({ path: '/graphql', method: RequestMethod.ALL });
  }

}

With the following request:

POST /graphql HTTP/1.1
Host: localhost:3000
Content-Type: application/graphql
Cache-Control: no-cache

{ snippets {name}}

I get the following response:

HTTP 500

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

I found the solution reading some Apollo documentation; all I had to do was to add the following middlewares to my endpoint:

import {Module, NestModule, MiddlewaresConsumer, RequestMethod} from '@nestjs/common';
import {graphqlExpress, graphiqlExpress} from 'apollo-server-express';
import {GraphQLFactory, GraphQLModule} from '@nestjs/graphql';
import {SnippetResolvers} from "./snippet/snippet.resolvers";
import {SnippetModule} from "../snippet/snippet.module";
import * as bodyParser from 'body-parser';

@Module({
  imports: [
    GraphQLModule,
    SnippetModule
  ],
  components: [
    SnippetResolvers
  ]
})
export class GQLModule implements NestModule {

  constructor(private readonly graphQLFactory: GraphQLFactory) {}

  configure(consumer: MiddlewaresConsumer) {
    const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
    const schema = this.graphQLFactory.createSchema({ typeDefs });
    consumer
      .apply(bodyParser.text({ type: 'application/graphql' }))
      .forRoutes({ path: '/graphql', method: RequestMethod.ALL })
      .apply((req, res, next) => {
        if (req.is('application/graphql')) {
          req.body = { query: req.body };
        }
        next();
      })
      .forRoutes({ path: '/graphql', method: RequestMethod.ALL })
      .apply(graphiqlExpress({ endpointURL: '/graphql' }))
      .forRoutes({ path: '/graphiql', method: RequestMethod.GET })
      .apply(graphqlExpress(req => ({ schema, rootValue: req })))
      .forRoutes({ path: '/graphql', method: RequestMethod.ALL });
  }

}

At this point, everything looks like working fine for me and I'm pretty happy with this solution. Could any of you give me some impression on this implementation? Is it the way to go? (if yes, then we should maybe add those details to the documentation)

Thanks for your time 😄

@kamilmysliwiec
Copy link
Member

Hi @fwoelffel,
yeah, your solution sounds good. I guess we can make this as a part of the module, available out-of-the-box. 😄

@fwoelffel
Copy link
Author

Thanks! I think I don't have enough experience with Nest to submit a PR for this. How could we setup this as part of the GraphQL module and still configure the routes to which it should be applied?

@kamilmysliwiec
Copy link
Member

In the next release, nest won't be wholly dependent of express library. That's why we cannot make this middleware a part of the graphql package since it won't be compatible with any other HTTP provider (like, for example, fastify). Luckily, @fwoelffel solution should work for any other person who would encounter a similar problem 🙂

@lock
Copy link

lock bot commented Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants