Skip to content

File Upload #33

Open
Open

Description

@vladinator1000

I'm working on an example repo and was wondering, what would be the simplest way to implement that? This is what I tried without GraphQL:

@app.route('/upload', methods=['POST'])
def uploadFile():
	for key in request.files:
		file = request.files[key]
		file.save(os.path.join(UPLOAD_FOLDER, file.filename))
	
	return 'uploaded'

This is what I used in Node.js, would love to do something like that for Flask in Python.

If it's not supported by flask-graphql I'd be happy to work on a pull request. Also, how would you test this without a browser, using Graphiql, Postman or something like that?

Activity

  1. michaelkuty commented on Apr 21, 2018

    @michaelkuty
        def mutate(cls, info, **kwargs):
    
            file = info.context.files.get('file', None)
    
  2. hxuanhung commented on May 4, 2018

    @hxuanhung

    @michaelkuty can you make it work with Apollo? I got "Must provide query string." error when trying to send multipart/form-data; to the server.

  3. michaelkuty commented on May 4, 2018

    @michaelkuty

    In apollo you must use something like middleware or something else..

  4. hxuanhung commented on May 4, 2018

    @hxuanhung

    hm, have you actually worked on it? I'm using https://github.com/jaydenseric/apollo-upload-client for a client that sends multipart/form-data to the server but it seems not working.

  5. rshk commented on May 24, 2018

    @rshk

    Ok, just managed to get it to work with Apollo. The problem lies with incorrect handling of the multipart-encoded request, here: https://github.com/graphql-python/flask-graphql/blob/master/flask_graphql/graphqlview.py#L126-L127

    I sub-classed the GraphQLView to properly handle multipart/form-data, as per the spec. I also created a custom scalar field that can be used for file uploads, so no need to get files out of info.context or flask.request.

    https://gist.github.com/rshk/97fd7b444e48c60218addb2b64897572

    This will work with the version of flask-graphql currently in pypi; I haven't tested whith the master version yet (but should still work).

    I can open a PR with the changes (I have unit tests covering that place_files_in_operations too).

  6. hxuanhung commented on May 24, 2018

    @hxuanhung
  7. rshk commented on Jul 24, 2018

    @rshk

    See #51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions