Apollo服务器2.0。在文档中找不到“上传”类型

Zen*_*tzi 3 apollo graphql apollo-server

如何复制:

server.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}`);
});
Run Code Online (Sandbox Code Playgroud)

package.json

"dependencies": {
    "apollo-server": "^2.0.0-rc.6",
    "graphql": "^0.13.2"
  }
Run Code Online (Sandbox Code Playgroud)

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

键入在文档中找不到的“上传”。

给定最新版本的apollo服务器,我是否应该在查询中添加其他内容?根据教程和我目前不记得的其他一些资料,除了编写Upload之外,不需要做任何其他事情,它应该可以正常工作。我有什么想念的吗?

Joe*_*ner 5

在apollo文档上的示例中,有几种方法可以解决此问题:

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

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

键入在文档中找不到的“上传”。

如果要使用,请makeExecutableSchema导入标量

const typeDefs = gql`
  scalar Upload

  type Mutation {
    uploadAvatar(upload: Upload!): String!
  }
  type Query {
    ping: String
  }
`;
Run Code Online (Sandbox Code Playgroud)

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

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

未自动添加的原因是

标量上传由Apollo Server自动添加到架构的Upload类型解析包含以下内容的对象:

  • 文档名称
  • 模仿型
  • 编码方式

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

在使用makeExecutableSchema手动设置架构并使用架构参数将其传递给ApolloServer构造函数的情况下,将Upload标量添加到类型定义中,并将Upload添加到解析器中

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