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.
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
|