lmcgartland / graphene-file-upload

Enhances Graphene Django GraphQL Server for intuitive file uploads via GraphQL mutations.
MIT License
285 stars 49 forks source link

Upgrade to graphql-core v3 #42

Open fhennig opened 3 years ago

fhennig commented 3 years ago

Graphene's update to v3 is stabilizing. For flask, it is already possible to operate with pre-release packages (graphene v.3.0b5, graphql-server[flask] v3.0.0b1), for graphql-django the upgrade to v3 is in progress: https://github.com/graphql-python/graphene-django/issues/705.

leahein commented 3 years ago

Is there an update on this? I've submitted PR #61 that should resolve this.

Kulv3r commented 3 weeks ago

Hacky workaround for anyone searching for it:

make an extra file called graphene_file_upload_fix.py, paste this into it:

import sys
from importlib.abc import MetaPathFinder, Loader
from importlib.machinery import ModuleSpec

import graphql_server.flask

class GrapheneFileUploadImportHook(MetaPathFinder, Loader):
    """
    Since we can't modify `graphene_file_upload` lib directly and
    it's still trying to import from `flask_graphql` (since not maintained),
    we can achieve this by manipulating Python's module import system
    to intercept and redirect imports from `flask_graphql` to `graphql_server.flask`.
    This method is a bit hacky but works well for this issue.

    Import this module before importing `graphene_file_upload`.
    """
    def find_spec(self, fullname, path, target=None):
        if fullname == 'flask_graphql':
            return ModuleSpec(fullname, self, is_package=False)

    def create_module(self, spec):
        # Since flask_graphql doesn't exist, we're going to redirect it to graphql_server.flask
        return sys.modules.setdefault(spec.name, sys.modules['graphql_server.flask'])

    def exec_module(self, module):
        # No module execution needed as we're aliasing to an existing module
        pass

# Add the custom import hook to the beginning of the meta path finder list
sys.meta_path.insert(0, GrapheneFileUploadImportHook())

it's for Flask, but you can easily adjust it for Django or whatnot

then import it BEFORE you 1st time import graphene-file-upload in my case i import it in app.py, as a 1st line.

that's it.