Skip to content

Commit

Permalink
build in apollo-server file upload uses an outdated apollo-upload ver…
Browse files Browse the repository at this point in the history
…sion: so an Maximum call stack size exceeded error occurs. -> solved as suggested: https://github.com/apollographql/apollo-server/issues/3508\#issuecomment-662371289
  • Loading branch information
DanBilic committed Jul 5, 2021
1 parent 3f3bc30 commit 894f808
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
config/config.env
config/config.env
public/uploads/*
62 changes: 62 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dotenv": "^10.0.0",
"express": "^4.17.1",
"graphql": "^15.5.0",
"graphql-upload": "^12.0.0",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"mongoose": "^5.12.13",
Expand Down
14 changes: 11 additions & 3 deletions src/modules/user/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import path from "path";
import { createWriteStream } from "fs";
import { gql } from "apollo-server-express";
import User from "../../models/User";
import { createWriteStream } from "fs";
import path from "path";
import {
GraphQLUpload, // The GraphQL "Upload" Scalar
} from "graphql-upload";

const typeDefs = gql`
scalar Upload
type User {
_id: ID! @log
email: String!
Expand Down Expand Up @@ -63,6 +68,9 @@ const typeDefs = gql`
`;

const resolvers = {
// Add this line to use the `GraphQLUpload` from `graphql-upload`.
Upload: GraphQLUpload,

Query: {
async users(_, __, { user }) {
return await User.find({});
Expand Down Expand Up @@ -130,7 +138,7 @@ const resolvers = {
},
async uploadUserImage(_, { input }, { user }, __) {
const { file } = input;
const { createReadStream, filename } = file;
const { createReadStream, filename, mimetype } = await file;

console.log("uploaded file", file);

Expand Down
9 changes: 9 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { ApolloServer } from "apollo-server-express";
import { typeDefs, resolvers } from "./modules";
// import { customLogPlugin } from "./services/gqlPlugin";
import express from "express";
import {
graphqlUploadExpress, // The Express middleware.
} from "graphql-upload";

import { getUserFromToken } from "./services/auth";

Expand All @@ -26,6 +29,10 @@ function startServer({ port = process.env.PORT } = {}) {
},
typeDefs,
resolvers,
// Disable the built in file upload implementation that uses an outdated
// `graphql-upload` version, see:
// https://github.com/apollographql/apollo-server/issues/3508#issuecomment-662371289
uploads: false,
async context({ connection, req }) {
if (connection) {
return { ...connection.context };
Expand Down Expand Up @@ -58,6 +65,8 @@ function startServer({ port = process.env.PORT } = {}) {
// });

const app = express();
app.use(graphqlUploadExpress());

app.use(
"/uploads",
express.static(path.join(__dirname, "../public/uploads"))
Expand Down

0 comments on commit 894f808

Please sign in to comment.