要解决“Apollo Server从Apollo Upload Client接收到空对象”的问题,您可以尝试以下解决方法:
ApolloLink
。下面是一个示例:import { createUploadLink } from 'apollo-upload-client';
import { ApolloClient, InMemoryCache, ApolloLink } from '@apollo/client';
const uploadLink = createUploadLink({
uri: '/graphql',
});
const client = new ApolloClient({
link: ApolloLink.from([uploadLink]),
cache: new InMemoryCache(),
});
ApolloServer
的uploads
选项设置为false
,以启用文件上传。下面是一个示例:const { ApolloServer } = require('apollo-server');
const { ApolloServerPluginDrainHttpServer } = require('apollo-server-core');
const http = require('http');
const server = new ApolloServer({
// ...其他配置项
uploads: false, // 设置为true以启用文件上传
});
const httpServer = http.createServer();
await server.start();
server.applyMiddleware({ app: httpServer });
httpServer.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
const { GraphQLUpload } = require('graphql-upload');
const resolvers = {
// ...其他解析器
Upload: GraphQLUpload, // 定义上传文件的类型
};
createUploadLink
提供的createUploadMiddleware
中间件来处理文件上传。下面是一个示例:import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
import { createUploadLink, createUploadMiddleware } from 'apollo-upload-client';
const uploadMiddleware = createUploadMiddleware();
const httpLink = createHttpLink({ uri: '/graphql' });
const client = new ApolloClient({
link: uploadMiddleware.concat(httpLink),
cache: new InMemoryCache(),
});
// 使用client发送GraphQL请求
通过检查这些方面,您应该能够解决“Apollo Server从Apollo Upload Client接收到空对象”的问题。