UNPKG

graphql-upload-ts

Version:

Typescript minimalistic and developer friendly middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.

90 lines • 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphQLUpload = void 0; const Upload_1 = require("./Upload"); const graphql_1 = require("graphql"); /** * A GraphQL `Upload` scalar that can be used in a * [`GraphQLSchema`](https://graphql.org/graphql-js/type/#graphqlschema). * It's value in resolvers is a promise that resolves * [file upload details]{@link FileUpload} for processing and storage. * @example <caption>Ways to `import`.</caption> * ```js * import { GraphQLUpload } from 'graphql-upload-ts'; * ``` * * ```js * import GraphQLUpload from 'graphql-upload-ts/dist/GraphQLUpload.js'; * ``` * @example <caption>Ways to `require`.</caption> * ```js * const { GraphQLUpload } = require('graphql-upload-ts'); * ``` * * ```js * const GraphQLUpload = require('graphql-upload-ts/dist/GraphQLUpload'); * ``` * @example <caption>Setup for a schema built with [`makeExecutableSchema`](https://apollographql.com/docs/graphql-tools/generate-schema#makeExecutableSchema).</caption> * ```js * const { makeExecutableSchema } = require('graphql-tools'); * const { GraphQLUpload } = require('graphql-upload-ts'); * * const schema = makeExecutableSchema({ * typeDefs: /* GraphQL *\/ ` * scalar Upload * `, * resolvers: { * Upload: GraphQLUpload, * }, * }); * ``` * @example <caption>A manually constructed schema with an image upload mutation.</caption> * ```js * const { * GraphQLSchema, * GraphQLObjectType, * GraphQLBoolean, * } = require('graphql'); * const { GraphQLUpload } = require('graphql-upload-ts'); * * const schema = new GraphQLSchema({ * mutation: new GraphQLObjectType({ * name: 'Mutation', * fields: { * uploadImage: { * description: 'Uploads an image.', * type: GraphQLBoolean, * args: { * image: { * description: 'Image file.', * type: GraphQLUpload, * }, * }, * async resolve(parent, { image }) { * const { filename, fieldName, mimetype, createReadStream } = await image; * const stream = createReadStream(); * // Promisify the stream and store the file, then… * return true; * }, * }, * }, * }), * }); * ``` */ exports.GraphQLUpload = new graphql_1.GraphQLScalarType({ name: 'Upload', description: 'The `Upload` scalar type represents a file upload.', parseValue(value) { if (value instanceof Upload_1.Upload) return value.promise; throw new graphql_1.GraphQLError('Upload value invalid.'); }, parseLiteral(node) { throw new graphql_1.GraphQLError('Upload literal unsupported.', { nodes: node }); }, serialize() { throw new graphql_1.GraphQLError('Upload serialization unsupported.'); }, }); //# sourceMappingURL=GraphQLUpload.js.map