1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- // @ts-check
- "use strict";
- const { GraphQLScalarType, GraphQLError } = require("graphql");
- const Upload = require("./Upload.js");
- /** @typedef {import("./processRequest").FileUpload} FileUpload */
- /**
- * 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
- * {@link FileUpload file upload details} for processing and storage.
- * @example
- * A schema built using
- * [`makeExecutableSchema`](https://www.graphql-tools.com/docs/api/modules/schema_src#makeexecutableschema)
- * from [`@graphql-tools/schema`](https://npm.im/@graphql-tools/schema):
- *
- * ```js
- * const { makeExecutableSchema } = require("@graphql-tools/schema");
- * const GraphQLUpload = require("graphql-upload/GraphQLUpload.js");
- *
- * const schema = makeExecutableSchema({
- * typeDefs: `
- * scalar Upload
- * `,
- * resolvers: {
- * Upload: GraphQLUpload,
- * },
- * });
- * ```
- * @example
- * A manually constructed schema with an image upload mutation:
- *
- * ```js
- * const { GraphQLSchema, GraphQLObjectType, GraphQLBoolean } = require("graphql");
- * const GraphQLUpload = require("graphql-upload/GraphQLUpload.js");
- *
- * 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, mimetype, createReadStream } = await image;
- * const stream = createReadStream();
- * // Promisify the stream and store the file, then…
- * return true;
- * },
- * },
- * },
- * }),
- * });
- * ```
- */
- const GraphQLUpload = new GraphQLScalarType({
- name: "Upload",
- description: "The `Upload` scalar type represents a file upload.",
- parseValue(value) {
- if (value instanceof Upload) return value.promise;
- throw new GraphQLError("Upload value invalid.");
- },
- parseLiteral(node) {
- throw new GraphQLError("Upload literal unsupported.", { nodes: node });
- },
- serialize() {
- throw new GraphQLError("Upload serialization unsupported.");
- },
- });
- module.exports = GraphQLUpload;
|