Graphql-code-generator: Support Upload Scalar (Apollo Server 2)

Created on 19 Dec 2018  Â·  3Comments  Â·  Source: dotansimha/graphql-code-generator

Is your feature request related to a problem? Please describe.
We want to upload images through graphql. Apollo Server supports this out of the box, by automatically inserting an Upload scalar. However, when trying to use Upload with this generator, I get the error Error: Type "Upload" not found in document..
Describe the solution you'd like
Some way for the code generator to know about the Upload scalar defined by Apollo.

Describe alternatives you've considered
A workaround is to add it, but remove it before loading the schema for Apollo. My current workaround is untyped.

Additional context
See https://blog.apollographql.com/file-uploads-with-apollo-server-2-0-5db2f3f60675

The first question you’re most likely to ask from the schema observation above is “Where does Upload scalar type come from?” Don’t fret. It is added automatically by Apollo Server.

core enhancement

Most helpful comment

Hi :)

Apollo Server adds Upload Scalar when typeDefs are used, they don't extend the schema.

https://github.com/apollographql/apollo-server/blob/ad4240295469821c24aaf2f1032b12882dd3b6f9/packages/apollo-server-core/src/ApolloServer.ts#L271-L275

Exactly as you described, the solution would be to add that scalar just for GraphQL Code Generator. The current API allows to achieve that but I found out it won't work because of a bug... I'm trying to fix it in https://github.com/dotansimha/graphql-code-generator/pull/1074

About the solution, you can define a schema as a string in the config file:

schema: 
 - ./schema.js
 - |
   scalar Upload
overwrite: true
generates:
  types.ts:
    plugins:
      - typescript-common
      - typescript-server

In an example above I used | to tell a yaml interpreter that it's a block which means you use multiline string there :) but - scalar Upload should work too.

All 3 comments

Hi :)

Apollo Server adds Upload Scalar when typeDefs are used, they don't extend the schema.

https://github.com/apollographql/apollo-server/blob/ad4240295469821c24aaf2f1032b12882dd3b6f9/packages/apollo-server-core/src/ApolloServer.ts#L271-L275

Exactly as you described, the solution would be to add that scalar just for GraphQL Code Generator. The current API allows to achieve that but I found out it won't work because of a bug... I'm trying to fix it in https://github.com/dotansimha/graphql-code-generator/pull/1074

About the solution, you can define a schema as a string in the config file:

schema: 
 - ./schema.js
 - |
   scalar Upload
overwrite: true
generates:
  types.ts:
    plugins:
      - typescript-common
      - typescript-server

In an example above I used | to tell a yaml interpreter that it's a block which means you use multiline string there :) but - scalar Upload should work too.

I get

âś– src/gql/generated/graphql.tsx
    Failed to load schema from http://localhost:4000/graphql,scalar Upload:

        type.getFields is not a function
        TypeError: type.getFields is not a function
    at applyExtensions (/Users/joshbedo/Desktop/projects/influencial-1.0/client/node_modules/@graphql-tools/merge/index.cjs.js:879:40)
    at makeSchema (/Users/joshbedo/Desktop/projects/influencial-1.0/client/node_modules/@graphql-tools/merge/index.cjs.js:998:5)
    at Object.mergeSchemasAsync (/Users/joshbedo/Desktop/projects/influencial-1.0/client/node_modules/@graphql-tools/merge/index.cjs.js:968:12)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Object.loadSchema (/Users/joshbedo/Desktop/projects/influencial-1.0/client/node_modules/@graphql-tools/load/index.cjs.js:650:20)
    at async loadSchema (/Users/joshbedo/Desktop/projects/influencial-1.0/client/node_modules/@graphql-codegen/cli/bin.js:397:24)
    at async /Users/joshbedo/Desktop/projects/influencial-1.0/client/node_modules/@graphql-codegen/cli/bin.js:779:55
    at async Task.task (/Users/joshbedo/Desktop/projects/influencial-1.0/client/node_modules/@graphql-codegen/cli/bin.js:634:17)

When i enter the Scalar above.

@joshbedo You configuration/schema url seems invalid, please double check it :) it might be indented incorrectly, take a look at the url...

Was this page helpful?
0 / 5 - 0 ratings