Skip to content

passing context to GraphQLView no more supported? #52

Open
@Tryph

Description

@Tryph

Hi and thanks for the geat job.

With 1.4.1 version I used to pass my DB session via the context option of the GraphQLView as still mentioned in the README (which should be updated).

Below is an exemple of what I was doing:

# [...]

db_session = scoped_session(
    sessionmaker(autocommit=False, autoflush=False, bind=engine))

# [...]

app.add_url_rule(
    '/graphql', view_func=GraphQLView.as_view(
        'graphql', schema=SCHEMA, graphiql=True,
        context={'session': db_session}))

# [...]

It seems this is no more possible and I can't figure out how to pass the DB session to the GraphQLView. Any information and/or updated doc would be nice.

Any solution to continue passing DB session via the GraphQLView (or by any other way allowing to pass it at execution time) would be perfect.

EDIT: for an undocumented but working solution see @cockscomb 's comment

Activity

vlad-bezden

vlad-bezden commented on Jul 28, 2018

@vlad-bezden

I'm having the same problem. I tried to use context and context_value and neither of them works.

jjwtay

jjwtay commented on Jul 31, 2018

@jjwtay

same

rscarrera27

rscarrera27 commented on Jul 31, 2018

@rscarrera27

same

dbasden

dbasden commented on Aug 2, 2018

@dbasden

There seem to be changes in graphql-server-core and flask-graphql to change unit tests :
"Improved query execution using pluggable backend" in graphql-server-core e.g.:
graphql-python/graphql-server@d60b180

There is an underlying change in graphql-python/graphql-core@f6d79ab
in the commit "Modernize execute function" around graphql-core v2.1.0

This is where "context_value" is deprecated for "context"

graphql-python/graphql-core@f6d79ab

It looks like "context" is now not what is being passed by the user of flask-graphql, but a Request object instead. Maybe there is a name collision in flask-graphql?

dbasden

dbasden commented on Aug 2, 2018

@dbasden

It looks like the breaking change was actually a long time ago, but was only put out in a full release recently:

d728f80

I don't know if the behaviour of "context_value" was meant to go away and the documentation was left unchanged?

Edit: This also seems to be referenced in the last comment (made after merge) of #19

jw3126

jw3126 commented on Aug 3, 2018

@jw3126

Is there a recommended way to do the things that were done by using context in older versions? I have seen the suggestion to restore the old context behaviour by inheritence. But I guess context was removed for a reason (btw what reason?), so there may be a better way?

jjwtay

jjwtay commented on Aug 3, 2018

@jjwtay

Cannot speak to why they would have possibly removed it but I can say for now I just am using this to work around.

class MyGraphQLView(GraphQLView):
   def get_context(self):
       context = super().get_context()
       context.session = db.session

       return context
jw3126

jw3126 commented on Aug 3, 2018

@jw3126

Ah thanks! This was what I meant by "restore the old context behavior by inheritance".

cockscomb

cockscomb commented on Aug 14, 2018

@cockscomb

I found a test case of passing context.

@pytest.mark.parametrize('app', [create_app(get_context=lambda:"CUSTOM CONTEXT")])

I succeeded passing custom context in the following way.

app.add_url_rule(
    '/graphql', view_func=GraphQLView.as_view(
        'graphql', schema=SCHEMA, graphiql=True,
        get_context=lambda: {'session': db_session}))
Tryph

Tryph commented on Aug 15, 2018

@Tryph
Author

@cockscomb It works perfectly thanks for sharing this

linked a pull request that will close this issue on Sep 30, 2018
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

      passing context to GraphQLView no more supported? · Issue #52 · graphql-python/flask-graphql