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.
73 lines (72 loc) • 2.33 kB
TypeScript
import { Upload } from './Upload';
import { GraphQLScalarType } from '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;
* },
* },
* },
* }),
* });
* ```
*/
export declare const GraphQLUpload: GraphQLScalarType<Promise<Upload> & Promise<import("./Upload").FileUpload>, never>;