“”POST body missing, invalid Content-Type, or JSON object has no keys.” Error on Apollo v4

“”POST body missing, invalid Content-Type, or JSON object has no keys.” Error on Apollo v4


0

//Sever.js
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import {makeExecutableSchema} from "@graphql-tools/schema";
import {typeDefs, resolvers} from "./schema.js";
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import express from 'express';
import http from 'http';
import cors from 'cors';
import bodyParser from 'body-parser';


async function startServer(port, callback){
    const schema = makeExecutableSchema({resolvers,typeDefs})
    const app = express();
    const httpServer = http.createServer(app);
    const server = new ApolloServer({
        typeDefs,
        resolvers,
        plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
    });
    await server.start();
    app.use(
        '/',
        cors(),
        bodyParser.json(),
        expressMiddleware(server, {
            context: async ({ req }) => ({ token: req.headers.token }),
        }),
    );
    
    await new Promise((resolve) => httpServer.listen({ port: 4000 }, resolve));
    console.log(`🚀 Server ready at https://localhost:4000/`);
}
startServer();

//typeDefs.js
export default gql`
    scalar Upload
    type Mutation{
        createPost(
            title: String!
            content: String!
            userId: Int! 
            categoryId: Int!
            file: Upload
        ):MutationResponse!
    }
    
`
//mutation.js
export default {
    Mutation: {
        createPost: async (_,{title, content, userId, categoryId, file}) => {
                /*if(!loggedInUser.id){
                    return{
                        ok:false
                    }
                }*/
            return{
                ok:true
            }

        }
    }
}

I use Apollo v4. for file Upload and do not use ‘graphql-upload’.

This is my server code and I tried file upload on Altair. Then below error occurs.

""POST body missing, invalid Content-Type, or JSON object has no keys." Error on Apollo v4

How can I fix this error?

I tried set ‘content-type’:’application/json’ at headers, but another error " Unexpected token ‘-‘, "——WebK"… is not valid JSON" occurs.

Please

Share


Load 5 more related questions


Show fewer related questions

0

Reset to default



Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *