Cliff
Cliff

Reputation: 1568

Graphene - GraphQL : 'Request' object has no attribute 'context'

I'm working on a simple GraphQL Flask API and I'm implementing a simple find user resolver. This is my schema.py

import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from models import db_session, Users as UsersModel, Channel as ChannelModel, Video as VideoModel

class Users(SQLAlchemyObjectType):
    class Meta:
        model = UsersModel
        interfaces = (relay.Node, )

class Channel(SQLAlchemyObjectType):
    class Meta:
        model = ChannelModel
        interfaces = (relay.Node, )

class Query(graphene.ObjectType):
    node = relay.Node.Field()
    users = SQLAlchemyConnectionField(Users)
    find_user = graphene.Field(Users, name = graphene.String())
    all_user = SQLAlchemyConnectionField(Users)

    def resolve_find_user(self, args, context, info):
        query = Users.get_query(context)
        name = args.get('name')
        return query.get(name)

I'm using graphene 2.0 and the code returns an error of "message": "resolve_find_user() got an unexpected keyword argument 'name'", After some Googling, it looks like that is a major change in graphene that change the way resolver works.

I went through the UPGRADE-v2.0.md and change things accordingly. The resolver looks like this now.

# def resolve_find_user(self, info, name):
#     query = Users.get_query(info.context)
#     return query.get(name)

Now, the keyword argument disappears but I'm having a new issue. The error is "message": "'Request' object has no attribute 'context'",. Now I'm confused because I thought info.context should have worked but apparently it is not. Any idea about how to fix this context issue? This is my first time working on GraphQL and any help is really appreciated!

Edit: It's looks like type(info) is a <class 'graphql.execution.base.ResolveInfo'> and type(info.context) is <class 'werkzeug.local.LocalProxy'>. Should the context be the SQLAlchemy context?

Upvotes: 3

Views: 3709

Answers (1)

Alex
Alex

Reputation: 625

In version 2.0 of graphene your solution would be like this

def resolve_find_user(self, info, name):
  query = Users.get_query(info)
  return query.filter(UsersModel.name == name).first()

For more information here

Upvotes: 1

Related Questions