question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add possibility to rename Upload scalar

See original GitHub issue

Hey,

It would be great to have a possibility to rename scalar. I tried this code, but it is still named Upload:

import { GraphQLUpload } from 'apollo-upload-server'

const imageTypeDefs = `
  scalar Image
`

const imageResolvers = {
  Image: GraphQLUpload
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
jaydensericcommented, Mar 22, 2018

The best way to validate uploads is to do it the same way you would for any mutation argument; in the mutation resolver.

For example:

scalar Upload

type Image {
  filename: String!
  filesize: Int!
  width: Int!
  height: Int!
  url: String!
}

type UploadImageErrors {
  image: [String!]
}

type UploadImagePayload {
  errors: UploadImageErrors
  image: Image
}

uploadImage (image: Upload!): UploadImagePayload!

In the uploadImage mutation resolver:

(obj, { image }) => {
  const { stream, filename, mimetype, encoding } = await image

  const payload = {
    errors: {
      image: []
    }
  }

  if (mimetype !== 'image/png') payload.errors.image.push('Must be a PNG file.')

  // Handle stream, either wasting or storing it based on validation so far…

  if (
    // Image stream was truncated due to maxFileSize
  ) payload.errors.image.push('Filesize too big.')

  if (
    // Image stored ok, and we have determined filesize, width, etc.
  ) payload.image = {
    filename,
    filesize
    width
    height
    url
  }

  if (!payload.errors.image.length) delete payload.errors.image
  if (!Object.keys(payload.errors).length) delete payload.errors

  return payload
}
1reaction
vladshcherbincommented, Feb 21, 2018

@jaydenseric thank you, this will definitely help 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

File Uploads with Apollo Server 2.0 - Apollo GraphQL Blog
Apollo Server 2.0 automatically adds the Upload scalar to the schema, when you are not setting the schema manually.
Read more >
Feature Request: FilePicker on Upload scalar #597 - GitHub
It works quite nice with the option to "add query" and "add fragments" directly from the docs. I'm only missing the Tracing view...
Read more >
File Upload - gqlgen
To use it you need to add the Upload scalar in your schema, and it will automatically add the marshalling behaviour to Go...
Read more >
GraphQL: File Upload & Troubleshooting - Szhshp
scalar Upload always causes error :( · If I add it -> Error: There can be only one type named "Upload" · If...
Read more >
Add Upload type to GraphQL - node.js - Stack Overflow
This was working until I changed the image argument type from String to Upload . Where, according to Apollo documentation is enabled by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found