Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couldn't find type Upload in any of the schemas #286

Closed
prajjwaldimri opened this issue Feb 13, 2019 · 13 comments
Closed

Couldn't find type Upload in any of the schemas #286

prajjwaldimri opened this issue Feb 13, 2019 · 13 comments

Comments

@prajjwaldimri
Copy link

I was trying to get the file upload working in my graphql server by following
https://blog.apollographql.com/file-uploads-with-apollo-server-2-0-5db2f3f60675

In the article the author mentions that Apollo automatically adds the Upload type to the schema. However, when importing my typeDefs using graphql-import I am getting the error:

Error: Field file: Couldn't find type Upload in any of the schemas. at collectNode (/home/--/--/node_modules/graphql-import/src/definition.ts:154:15)

When adding scalar Upload to the schema.graphql file I get the error:

Error: There can be only one type named "Upload". at assertValidSDL (/home/--/--/node_modules/graphql/validation/validate.js:89:11)

Thanks

@daologger
Copy link

daologger commented Feb 28, 2019

same here. Currently, I have to mix importSchema with gql like this to make it work:

const basicDefs = importSchema('./schema.graphql')

const mutationDefs = gql`
  # I have other mutation fields defined in basicDefs
  extend type Mutation {
    imageUpload(file: Upload!): File
  }
`

new ApolloServer({
  typeDefs: [basicDefs, mutationDefs], resolvers,
})

@mxstbr
Copy link

mxstbr commented Apr 25, 2019

I am also running into this. Any ideas how to solve it?

@mxstbr
Copy link

mxstbr commented Apr 25, 2019

As said in the OP:

When adding scalar Upload to the schema.graphql file I get the error:
Error: There can be only one type named "Upload". at assertValidSDL (/home/--/--/node_modules/graphql/validation/validate.js:89:11)

@maticzav
Copy link
Collaborator

Just noticed. (I deleted the comment). To my understanding, this happens because of the way graphql-import is implemented. While generating the imported schema, definition pool searches for all definitions mentioned in the file (this happens because graphql-import includes dependencies of the imported types as well as the imported types themselves.)

Because of that, it throws an exception that Upload is missing.

Did anyone perhaps test the behaviour with GraphQL Yoga?

I hope that helps with understanding, sorry for the previous answer.

@diegofly91
Copy link

diegofly91 commented Jun 19, 2019

Fix the error by putting the scheme const typeDefs = gql......and not importing the .graphql file

@felipeborba-dev
Copy link

felipeborba-dev commented Nov 11, 2019

I have the same problem, look at my server implementation

const { ApolloServer, gql } = require('apollo-server-express')
const { importSchema } = require('graphql-import')
const express = require('express')
const app = express()
const routes = require('./routes')


initServer();

function initServer() {
    try {
        
        const mutationDefs = gql`
            scalar Upload
            type Mutation {
            uploadFotoColaborador(file: Upload!): File!
            }
        `
        const resolvers = require('./resolvers')
        const context = require('./config/context')
        const schemaPath = './src/schema/index.graphql';
        const basicDefs = importSchema(schemaPath)
        const serverGraphQL = new ApolloServer({
            typeDefs:[basicDefs, mutationDefs],
            resolvers,
            context
        });

        serverGraphQL.applyMiddleware({ app, path: '/api' })
        app.use(express.json())
        app.use(routes)
        app.listen({ port: 4000 }, () => {
            console.log('Executando em localhost:4000')
        })
    }
    catch (error) {
        console.log(`Servidor não pode ser inicializado: ${error}`)
    }
}

@amiiit
Copy link

amiiit commented Nov 23, 2019

I'm still trying to get the whole setup working, but perhaps the following workaround might be of use to someone:

  const typeDefs = importSchema(__dirname + "/schema.graphql").replace(
    "scalar Upload",
    ""
  )

Makes the toolset happy, because scalar Upload is part of the schema file, and so apollo server isn't complaining anymore about double definition of the scalar Upload

EDIT: I can confirm, this workaround is working fine.

@felipeborba-dev
Copy link

felipeborba-dev commented Nov 27, 2019

@amiiit will perform the tests, thank you very much!

@hanselabreu
Copy link

@amiiit that doesn't work for me, I still get the error. I'm on version 0.7.1

@byteab
Copy link

byteab commented Jan 20, 2020

the error is because importSchema throw an error if it find type Upload used but not defined in Schema.
and if you add the type "scalar Upload" the importSchema will pass but apollo server will complain because apollo server will also add a "scalar Upload" by default. and now your schema have two "scalar Upload".
I think apollo server must check if type Upload not defined in schema then add it.

the temporary solution is to remove "scalar Upload" after importSchema read it.
as @amiiit said

@hyochan
Copy link

hyochan commented Feb 10, 2020

This issue makes apollo-server upload useless. I hope, I can see a full workaround. Tried all the above solution and still having the problem.

@ardatan
Copy link
Owner

ardatan commented Mar 17, 2020

Available in 1.0.0!

@ardatan ardatan closed this as completed Mar 17, 2020
@Dev-Addict
Copy link

I was having the same problem and by reading @hyochan comment i found out it's because of the name of scalar we put.
you can easily fix the problem by renaming the scalar. what i did was i renamed it to UploadFile and this fixed my problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests