Getting body from request is not working when using fastify HTTP platform with graphql and nestjs
See original GitHub issuedependencies:
- apollo-server-fastify: 2.9.16
- @nestjs/platform-fastify: 6.10.14
Steps to reproduce:
- setup basic project with Nest CLI
- use NestFastifyApplication to create fastify HTTP platform in NestFactory
- create GraphqlModule and import basic schema
- when reading the request object, I can’t retrieve the body and some other parameters which are documented in fastify documentation.
I have tried to get this information when creating app context or with middleware but with any success. Body parameter is available when I use express HTTP platform!
See example below:
// main.ts
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({
logger: false
}),
);
await app.listen(9040, 127.0.0.1);
}
bootstrap();
// graphql.module.ts
@Module({
providers: [...],
imports: [
GraphQLModule.forRoot({
introspection: true,
typePaths: [path.join(__dirname, 'schema.graphql')],
context: ({ req, params }): Context => {
console.log('////////////////////////////////');
console.log(req.body);
console.log(req.query);
console.log(req.params);
console.log(req.headers);
console.log(req.raw);
console.log(req.id);
console.log(req.ip);
console.log(req.ips);
console.log(req.hostname);
console.log('////////////////////////////////');
const userId = parseInt(params.userId, 10);
const regionId = parseInt(params.regionId, 10);
return {
userId,
regionId,
};
},
path: '/vod/:regionId/:userId/graphql',
}),
.....
],
})
export class GraphqlModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LogBodyMiddleware).forRoutes('/vod/:regionId/:userId/graphql');
}
}
// log-body.middleware.ts
@Injectable()
export class LogBodyMiddleware implements NestMiddleware {
use(req: any, res: any, next: () => void) {
console.log('========================================');
console.log(req.body);
console.log(req.query);
console.log(req.params);
console.log(req.headers);
console.log(req.raw);
console.log(req.id);
console.log(req.ip);
console.log(req.ips);
console.log(req.hostname);
console.log('========================================');
next();
}
}
I only get values for the following request parameters headers
, id
, ip
and hostname
.
How can I get body
value and other values?
Fastify documentation for request object and its fields: https://www.fastify.io/docs/latest/Request/
What am I doing wrong here?
Response:
////////////////////////////////
undefined
undefined
undefined
{ 'content-type': 'application/json',
'user-agent': 'PostmanRuntime/7.20.1',
accept: '*/*',
'cache-control': 'no-cache',
'postman-token': '7ad4b6fa-bde1-4c5d-8fe8-29b10bddac35',
host: 'localhost:9040',
'accept-encoding': 'gzip, deflate',
'content-length': '363',
connection: 'keep-alive' }
undefined
1
127.0.0.1
undefined
localhost:9040
////////////////////////////////
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Performance (Fastify) - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >Serverless - Fastify
Run serverless applications and REST APIs using your existing Fastify application. By default, Fastify will not work on your serverless platform of choice, ......
Read more >Newest 'fastify' Questions - Page 2 - Stack Overflow
I want to use Fastify to create a REST API, but I want to use it with a plain Node.js http.Server instance created...
Read more >Build Fullstack Apps with NestJS, Hasura, and GraphQL APIs
Under the hood, Nest makes use of robust HTTP Server frameworks like Express (the ... NestJS is not the easiest platform to onboard...
Read more >Nestjs | The framework of Nodejs (Part-1) | Controllers ...
Nestjs actually uses express & fastify as its platform for http server ... You can also perform validation on request-body using class- ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
i am experiencing the same issue. would love to see a solution for it.
the versions i am using.
i am trying to set up a middleware aswell.
Since this issue was opened we’ve done major version upgrades of Fastify and Apollo Server itself. I do hear that people are still seeing this, but it’s hard to dig in without a reproduction that we can test out. I’m closing this, but if somebody posts a git repo or codesandbox showing the issue still existing with AS3 and the latest Fastify and Nest, we’ll be happy to reopen. That said, we’re not really Nest experts here and so you might get more help at https://github.com/nestjs/graphql