question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

undefined properties when uploading using apollo-server-express

See original GitHub issue

I’ve been trying to add the upload feature to our graphql endpoint. We manage to hit the graphql mutation but the file stream is undefined. I can see on the developer tools that nothing is being sent in the request body which is odd. A console.log on the mutation server side renders { preview: ‘blob:http://localhost:3000/ba021b5c-57e6-4fba-b574-a4933250b0c2’ } and nothing else.

stream, filename, mimetype, encoding are all undefined.

Mutation Field

uploadFile: {
    type: UploadType,
    args: {
      file: {
        type: GraphQLUpload
      }
    },
    async resolve(_, { file }) {
      const { stream, filename, mimetype, encoding } = await file;
      console.log(file);
      return {
        stream,
        filename,
        mimetype,
        encoding
      };
    }
  }

Object Type

const {
  GraphQLObjectType,
  GraphQLString
} = require('graphql');

const UploadType = new GraphQLObjectType({
  name: 'UploadType',
  fields: () => ({
    filename: {
      type: GraphQLString
    },
    mimetype: {
      type: GraphQLString
    },
    encoding: {
      type: GraphQLString
    }
  })
});

module.exports = UploadType;

Mutation

const UPLOAD_FILE = gql`
	mutation uploadFile($file: Upload!) {
		uploadFile(file: $file) {
			filename
		}
	}
`;

Mutation Component

<Mutation mutation={UPLOAD_FILE}>
  {uploadFile => ( ... )}
</Mutation>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mike-marcaccicommented, Aug 30, 2018

Aah, that’s interesting. This is probably something we should clarify in our docs – that if your query accepts an Upload rather than a (non-null) Upload! the argument can be null instead of a Promise.

Glad you figured out the issue with apollo-boost!

1reaction
indieoceancommented, Aug 30, 2018

Update: I managed to remove apollo-boost that was the issue they don’t provide the upload feature on it or at least I cant find how to enable it anywhere. I managed to get it to work with the following config.

import React, { Component } from 'react';
import { ApolloProvider } from 'react-apollo';
import routes from './routes';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { onError } from 'apollo-link-error';
import { ApolloLink } from 'apollo-link';
import { createUploadLink } from 'apollo-upload-client'

const client = new ApolloClient({
  link: ApolloLink.from([
    onError(({ graphQLErrors, networkError }) => {
      if (graphQLErrors)
        graphQLErrors.map(({ message, locations, path }) =>
          console.log(
            `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
          ),
        );
      if (networkError) console.log(`[Network error]: ${networkError}`);
    }),
    new createUploadLink({
      uri: process.env.REACT_APP_API_BASE_PATH
    })
  ]),
  cache: new InMemoryCache()
});

class App extends Component {
	render() {
		return (
			<ApolloProvider client={client}>
				{ routes }
			</ApolloProvider>
		)
	}
}

export default App;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating to Apollo Server 4 - Apollo GraphQL Docs
Migrate from apollo-server-express. If you used the apollo-server-express package in Apollo Server 3, use the expressMiddleware function in Apollo Server 4 ...
Read more >
Undefined args on a mutation, using apollo-server
Im working with apollo-server, everything works as expetected but the mutation arguments are undefined when the mutation is called from the ...
Read more >
Apollo Server Express | Part 7 | Authentication (Login and ...
apollo # apollo-server-express #nodejs #curdDo Consider Joining our patron page for special and on-demand lectures and solutions, ...
Read more >
Apollo-server-express and express-session: session undefined
I am using the boilerplate from apollo-server-express. ... Main error: Error: Cannot set properties of undefined (setting 'userId').
Read more >
apollo-server-express | Yarn - Package Manager
This is the Express integration of Apollo Server. Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found