jaydenseric / graphql-upload

Middleware and a scalar Upload to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 132 forks source link

Setup for Meteor and Apollo Server v3 #264

Closed freeseamew closed 3 years ago

freeseamew commented 3 years ago

As far as I know, file upload has also changed in apollo server 3. May I know how to set the next code in meter?

async function startServer() {
  const server = new ApolloServer({
    typeDefs,
    resolvers,
  });
  await server.start();

  const app = express();

  // This middleware should be added before calling `applyMiddleware`.
  app.use(graphqlUploadExpress());

  server.applyMiddleware({ app });

  await new Promise(r => app.listen({ port: 4000 }, r));

  console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
}
jaydenseric commented 3 years ago

There is an Apollo Server v3 example, using Koa:

https://github.com/jaydenseric/apollo-upload-examples/blob/e1131d21b54d66391da342fb81981e805b857c8a/api/server.mjs#L16-L38

It's linked in the readme “Usage” section.

Your question is really a usage question about Apollo Server; particularly what their v3 API for Express is. Please raise usage questions about Apollo with the Apollo community.

freeseamew commented 3 years ago

I'm using meter and apolo.

I uploaded apollo-server-3 and looked at the document. (https://www.apollographql.com/docs/apollo-server/data/file-uploads/)

I saw that they told me to upload the file using graphql-upload.

Currently, I have set up and used meteorJs and apollo as follows.

I'm asking how to add a graphql-upload setting to this part.


  const server = new ApolloServer({
    playground: true,
    schema,
    plugins: [
      ...
    ],
    context: async({req}) => ({
      user: await getUser(req.headers.authorization) 
    })  
  });  

  await server.start();

  server.applyMiddleware({
    app: WebApp.connectHandlers,
    cors: true,
    path: '/graphql',
  });
jaydenseric commented 3 years ago

I don't use Meteor, so can't give you any advice about it. Once you figure it out, feel free to leave a follow-up comment here with the answer in case it helps other Meteor users.

freeseamew commented 3 years ago

The conclusion was solved by loading express as follows.

I hope it will help someone.

...

(async function(){

...

  const server = new ApolloServer({
    playground: true,
    schema,
    plugins: [
      ...
    ],
    context: async({req}) => ({
      user: await getUser(req.headers.authorization) ;
    }),
  });  

  await server.start();

  const app = express(); // This is important!
  app.use(graphqlUploadExpress()); // This is important!
  WebApp.connectHandlers.use('/graphql', app); // This is important!

  WebApp.connectHandlers.use("/graphql", (req, res, next) => {

    if (req.method === "GET") {
      console.log('Test!!!!!!');
      res.end()
    }    
    if(req.method ==="POST") {
      console.log('@@@@')
      console.log(`req: ${JSON.stringify(req.body) }`);
    }
    next();

  });
  server.applyMiddleware({
    app,
    cors: true,
    path: '/graphql',
  });

})();