gpt4 book ai didi

graphql - Apollo 服务器 2.0。在文档中找不到类型 "Upload"

转载 作者:行者123 更新时间:2023-12-03 20:29:59 24 4
gpt4 key购买 nike

如何复制:

服务器.js

const { ApolloServer, makeExecutableSchema, gql } = require('apollo-server');

const typeDefs = gql`
type Mutation {
uploadAvatar(upload: Upload!): String!
}
`;
const resolvers = {
Mutation: {
uploadAvatar(root, args, context, info) {
return 'test';
}
}
};

const schema = makeExecutableSchema({ typeDefs, resolvers });

const server = new ApolloServer({
schema,
});

server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});

包.json
"dependencies": {
"apollo-server": "^2.0.0-rc.6",
"graphql": "^0.13.2"
}

在节点 server.js 上,我们收到以下错误:

Type "Upload" not found in document.



鉴于最新版本的 Apollo 服务器,我是否应该在查询中添加任何其他内容?根据 this教程和我目前不记得的其他一些资源,除了写上传之外不需要做任何其他事情,它应该可以正常工作。我错过了什么吗?

最佳答案

在 Apollo 文档的示例中,我有几种方法可以解决这个问题:

https://www.apollographql.com/docs/guides/file-uploads.html



你可以看到他不使用 makeExecutableSchema但是将解析器和模式传递给 Apollo 服务器,这停止了错误:

Type "Upload" not found in document.



如果你想使用 makeExecutableSchema然后导入标量
const typeDefs = gql`
scalar Upload

type Mutation {
uploadAvatar(upload: Upload!): String!
}
type Query {
ping: String
}
`;

https://github.com/jaydenseric/apollo-upload-examples/blob/master/api/schema.mjs



如果您查看 at 博客文章的一些示例源代码,您会发现他使用了一个标量

它没有被自动添加的原因是

Scalar Upload The Upload type automatically added to the schema by Apollo Server resolves an object containing the following:


  • 文件名
  • 模仿类型
  • 编码

  • 更新: Apollo 更清楚地表明,当您使用 makeExecutableSchema 时,您需要定义标量才能使其工作

    In a situation where a schema is set manually using makeExecutableSchema and passed to the ApolloServer constructor using the schema params, add the Upload scalar to the type definitions and Upload to the resolver



    https://www.apollographql.com/docs/guides/file-uploads.html#File-upload-with-schema-param

    关于graphql - Apollo 服务器 2.0。在文档中找不到类型 "Upload",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51246024/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com