Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload single file does not fall on resolver #114

Closed
lucasdeassis opened this issue Sep 12, 2018 · 4 comments
Closed

Upload single file does not fall on resolver #114

lucasdeassis opened this issue Sep 12, 2018 · 4 comments
Labels

Comments

@lucasdeassis
Copy link

lucasdeassis commented Sep 12, 2018

Hello, I'm trying to make a file upload from a mutation using apollo-upload-client and apollo-upload-server, but as I send the mutation the server does not respond on any available resolver and it seems that no errors are produced. Can someone help me? I tried following the example approach fro jaydenseric but my server is using apollo-server-express. Here are some depicts:

creating server:

const { ApolloServer } = require('apollo-server-express');
const { apolloUploadExpress } = require('apollo-upload-server');
const jwt = require('express-jwt');
const createGraphQLSchema = require('./graphql');

app.use(
    '/graphql',
    cors(),
    jwt({ secret, credentialsRequired: false }),
    apolloUploadExpress(),
  );

  const server = new ApolloServer({
    schema,
    context: ({ req }) => ({ user: req.user, db }),
  });

  server.applyMiddleware({ app });

executable schema

const glue = require('schemaglue');
const { makeExecutableSchema } = require('apollo-server-express');
const schemaDirectives = require('./directives');

const createSchema = async () => {
  const { schema, resolver } = glue('graphql/types');

  return makeExecutableSchema({
    typeDefs: schema,
    resolvers: resolver,
    schemaDirectives,
    formatError: () => new Error('Internal server error'),
  });
};

schema

scalar Upload

type Photo {
    id: ID!
}

type Mutation {
    uploadPhoto(file: Upload!, caption: String, isPrivate: Boolean): Photo
}

my resolver is more or less like:

(root,  { file, caption, private: isPrivate }, { user, db }) => { 
     const {
        createReadStream, filename, mimetype, encoding,
      } = await file;

      const imageStream = createReadStream();

      console.log('stream', imageStream);
      console.log('caption', caption);
      console.log('private', isPrivate);
} 

that is, configuring the apollo client like:

import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createUploadLink } from 'apollo-upload-client';

const apolloClient = new ApolloClient({
    cache: new InMemoryCache().restore({ }),
    createUploadLink({ uri: 'http://localhost:3001/graphql' }),
    ]),
});

Whenever I send the mutation, I see the request being sent and parsed, but in no way the request falls under the uploadPhoto resolver. Here is a console log of my resolver from schemaglue;

{ Mutation:
   { register: [AsyncFunction: register],
     login: [AsyncFunction: login],
     uploadPhoto: [Function: uploadPhoto],
     editPhoto: [AsyncFunction: editPhoto],
     deletePhoto: [AsyncFunction: deletePhoto] },
  Upload:
   { name: 'Upload',
     description: 'The `Upload` scalar type represents a file upload.',
     serialize: [Function: serialize],
     parseValue: [Function: parseValue],
     parseLiteral: [Function: parseLiteral],
     astNode: undefined,
     extensionASTNodes: undefined },
  Photo: { id: [Function: id], owner: [Function: owner] },
@jaydenseric
Copy link
Owner

jaydenseric commented Sep 14, 2018

Keep in mind that the current version of apollo-server-express comes with a fork of this package at v5 (current is v7.1.0), so there might be 2 things going wrong:

  1. The apolloUploadExpress middleware setup might be conflicting with the built-in setup.
  2. createReadStream() is not available in the forked v5 version of apollo-upload-server, as it was introduced in v7.

@lucasdeassis
Copy link
Author

@jaydenseric Thanks. Actually, it was the apolloUploadExpress conflicting with the built-in setup. I didn't know about that and couldn't find in the docs.

@semy
Copy link

semy commented Sep 17, 2018

Hello @jaydenseric ,

current version is 7.1 and apolloUploadExpress uses v5. But how can I use 7.1 without removing apolloUploadExpress? Is there an example somewhere?

Many thanks for your help :)

@jaydenseric
Copy link
Owner

The original question has been answered. @semy there is a lot of existing issues and discussions about that, really it is a question for the Apollo team... it is up to them to update their own dependencies and support users. You might like to follow #109.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants