From f625506a4f12bb80d1ea8e6a9b70c988da80eea7 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco <8059120+alesso-x@users.noreply.github.com> Date: Tue, 1 Jun 2021 21:22:42 -0400 Subject: [PATCH] add mutation test --- graphql-upload/__tests__/mutation.test.js | 100 ++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 graphql-upload/__tests__/mutation.test.js diff --git a/graphql-upload/__tests__/mutation.test.js b/graphql-upload/__tests__/mutation.test.js new file mode 100644 index 0000000..b8fc766 --- /dev/null +++ b/graphql-upload/__tests__/mutation.test.js @@ -0,0 +1,100 @@ +const { ApolloServer, gql } = require("apollo-server-express"); +const { buildSchema } = require("graphql"); +const { Upload } = require("graphql-upload/public"); +const { createReadStream } = require("fs"); +const path = require("path"); +const { stitchSchemas } = require("@graphql-tools/stitch"); +const { GraphQLUpload: GatewayGraphQLUpload } = require("@graphql-tools/links"); + +const makeRemoteExecutor = require("../lib/make_remote_executor"); +const localSchema = require("../services/local/schema"); + +async function makeGatewaySchema() { + // Make remote executors: + // these are simple functions that query a remote GraphQL API for JSON. + const productsExec = makeRemoteExecutor("http://localhost:4001/graphql"); + + return stitchSchemas({ + subschemas: [ + { + // 1. Introspect a remote schema. Simple, but there are caveats: + // - Remote server must enable introspection. + // - Custom directives are not included in introspection. + schema: buildSchema(` + type Product { + name: String! + price: Float! + upc: ID! + } + + type Query { + product(upc: ID!): Product + } + `), + executor: productsExec, + }, + { + // 4. Incorporate a locally-executable subschema. + // No need for a remote executor! + // Note that that the gateway still proxies through + // to this same underlying executable schema instance. + schema: localSchema, + }, + ], + resolvers: { + Upload: GatewayGraphQLUpload, + }, + }); +} + +async function createApolloserver() { + const schema = await makeGatewaySchema(); + + const server = new ApolloServer({ + schema, + uploads: false, + }); + + return server; +} + +test("mutation", async () => { + const THE_MUTATION = gql` + mutation uploadFile($input: Upload!) { + uploadFile(input: $input) { + filename + mimetype + content + } + } + `; + + const upload = new Upload(); + const filename = "some_file.jpeg"; + + upload.promise = new Promise((resolve) => + resolve({ + createReadStream: () => + createReadStream(path.join(__dirname, "../file.txt")), + filename, + mimetype: "text/plain", + }) + ); + + const server = await createApolloserver(); + const result = await server.executeOperation({ + query: THE_MUTATION, + variables: { + input: upload, + }, + }); + + expect(result.errors).toBeUndefined(); + expect(result.data).toMatchObject({ + uploadFile: { + filename: "some_file.jpeg", + mimetype: "text/plain", + content: "hello upload\n", + }, + }); +});