Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo server 2.0. Type "Upload" not found in document

How to replicate:

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}`);
});

package.json

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

On node server.js we get the following error:

Type "Upload" not found in document.

Given the latest version of apollo server, am I supposed to add anything else to the query? According to this tutorial and few other sources that I currently cannot recall, one does not need to do anything more than just write Upload and it should work fine. Am I missing anything?

like image 801
ZenVentzi Avatar asked Mar 06 '23 20:03

ZenVentzi


1 Answers

There are a couple of ways I've fixed this, in the example on the apollo docs:

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

you can see he doesn't use makeExecutableSchema but passed the resolvers and schema to the apollo server this stopped the error:

Type "Upload" not found in document.

If you want to use makeExecutableSchema then import the scalar

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

if you look at some of the example source code for the at blog post you can see he uses a scalar

The reason it wasn't being added automatically is

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

  • stream
  • filename
  • mimetype
  • encoding

UPDATE: Apollo have made it clearer that when you use makeExecutableSchema you need to define the scalar for it to work

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

like image 157
Joe Warner Avatar answered Apr 30 '23 06:04

Joe Warner