Skip to content

I keep getting a request error: 'Request' object has no attribute 'get' #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jhonatanTeixeira opened this issue May 1, 2018 · 14 comments

Comments

@jhonatanTeixeira
Copy link

python 3.6
pipenv
ubuntu 16.04
mysql 5.7

my packages:

[packages]
flask-graphql = "*"
flask-sqlalchemy = "*"
sqlalchemy-fulltext-search = "*"
graphene-sqlalchemy = ">=2.0"
flask-restful = "*"
flask-marshmallow = "*"
sqlalchemy-pagination = "*"

my code:

from graphene import ObjectType, Schema, relay, Field
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from app.domain.model import Material, Grupo, Classe, Pdm

class MaterialType(SQLAlchemyObjectType):
    class Meta:
        model = Material
        interfaces = (relay.Node, )


class PdmType(SQLAlchemyObjectType):
    class Meta:
        model = Pdm
        interfaces = (relay.Node,)


class ClasseType(SQLAlchemyObjectType):
    class Meta:
        model = Classe
        interfaces = (relay.Node, )


class GrupoType(SQLAlchemyObjectType):
    class Meta:
        model = Grupo
        interfaces = (relay.Node, )


class Query(ObjectType):
    node = relay.Node.Field()
    materiais = SQLAlchemyConnectionField(MaterialType)
    pdms = SQLAlchemyConnectionField(PdmType)
    classes = SQLAlchemyConnectionField(ClasseType)
    grupos = SQLAlchemyConnectionField(GrupoType)
    material = Field(GrupoType)
    pdm = Field(PdmType)
    classe = Field(ClasseType)
    grupo = Field(GrupoType)

schema = Schema(query=Query, types=[MaterialType, PdmType, ClasseType, GrupoType])
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from app.domain.model import Base

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = connection_string
db = SQLAlchemy(app, metadata=Base.metadata)

app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True))

my error:

An error occurred while resolving field Query.materiais
Traceback (most recent call last):
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/graphql/execution/executor.py", line 311, in resolve_or_error
    return executor.execute(resolve_fn, source, info, **args)
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/graphql/execution/executors/sync.py", line 7, in execute
    return fn(*args, **kwargs)
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/graphene_sqlalchemy/fields.py", line 36, in connection_resolver
    iterable = cls.get_query(model, info, **args)
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/graphene_sqlalchemy/fields.py", line 20, in get_query
    return get_query(model, info.context)
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/graphene_sqlalchemy/utils.py", line 13, in get_query
    session = get_session(context)
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/graphene_sqlalchemy/utils.py", line 7, in get_session
    return context.get('session')
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'get'
Traceback (most recent call last):
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/graphql/execution/executor.py", line 330, in complete_value_catching_error
    exe_context, return_type, field_asts, info, result)
  File "/home/jhon/.local/share/virtualenvs/crm_api-CGOvMSyN/lib/python3.5/site-packages/graphql/execution/executor.py", line 383, in complete_value
    raise GraphQLLocatedError(field_asts, original_error=result)
graphql.error.located_error.GraphQLLocatedError: 'Request' object has no attribute 'get'

Am i doing something wrong?

@marvinkome
Copy link

What did you try to do that produced this error?

@jhonatanTeixeira
Copy link
Author

jhonatanTeixeira commented May 6, 2018 via email

@kigen
Copy link
Contributor

kigen commented May 6, 2018

This is an issue with db session, I also had the same issue.

I wish you had posted your models here also so that see how you've set up your Base object.

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

Base = declarative_base()
Base.query = db_session.query_property()

The key to solving your issue is this.. Base.query = db_session.query_property()

@macic
Copy link

macic commented May 15, 2018

Interesting

@jhonatanTeixeira
Copy link
Author

@kigen that was indeed the problem. however i had to set that on a non ortodox way cause i was using flask-sqlalchemy connection

@Kacppian
Copy link

set context_value={'session': db.session} when you're trying to execute the query. If you're using flask-graphql, context={'session': db.session}

@yoursdearboy
Copy link

yoursdearboy commented Nov 12, 2018

Seems to be a duplicate of #30

@aliwo
Copy link

aliwo commented Jul 31, 2019

it seems get_context also works for flask-graphql

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

the Session() func retrives SqlalchemySession from g (my custom function)

@herve-brun
Copy link

@kigen that was indeed the problem. however i had to set that on a non ortodox way cause i was using flask-sqlalchemy connection

Hi @jhonatanTeixeira ,

I know this is an old thread, but would you mind sharing the solution you had to implement ?
I'm running into the same problem :)

By advance, thank you

@valhuber
Copy link

valhuber commented Dec 2, 2020

I am having this issue, not with classical mapping. I adapted this example, resulting in this repo.

  • Database.py contains Base.query = db_session.query_property()

Stacktrace:

Traceback (most recent call last): File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphql/execution/executor.py", line 452, in resolve_or_error return executor.execute(resolve_fn, source, info, **args) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphql/execution/executors/sync.py", line 16, in execute return fn(*args, **kwargs) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/fields.py", line 78, in connection_resolver return on_resolve(resolved) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/fields.py", line 51, in resolve_connection resolved = cls.get_query(model, info, **args) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/fields.py", line 110, in get_query query = get_query(model, info.context) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/utils.py", line 16, in get_query session = get_session(context) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/utils.py", line 10, in get_session return context.get("session") File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/werkzeug/local.py", line 347, in __getattr__ return getattr(self._get_current_object(), name) AttributeError: 'Request' object has no attribute 'get' Traceback (most recent call last): File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphql/execution/executor.py", line 452, in resolve_or_error return executor.execute(resolve_fn, source, info, **args) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphql/execution/executors/sync.py", line 16, in execute return fn(*args, **kwargs) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/fields.py", line 78, in connection_resolver return on_resolve(resolved) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/fields.py", line 51, in resolve_connection resolved = cls.get_query(model, info, **args) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/fields.py", line 110, in get_query query = get_query(model, info.context) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/utils.py", line 16, in get_query session = get_session(context) File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/graphene_sqlalchemy/utils.py", line 10, in get_session return context.get("session") File "/Users/val/dev/graph_ql/payment_allocation_graphene/venv/lib/python3.8/site-packages/werkzeug/local.py", line 347, in __getattr__ return getattr(self._get_current_object(), name) graphql.error.located_error.GraphQLLocatedError: 'Request' object has no attribute 'get'

@ShantanuJoshi
Copy link

I am having this issue, not with classical mapping. I adapted this example, resulting in this repo.

Is your Base.query = db_session.query_property() in the same module as your mapping?

@valhuber
Copy link

Thanks for getting back... I will check

@mr-narender
Copy link

I tried the usual way by setting it at the model like this, which by the way doesnot work for me.

Base.query = db_session.query_property()

However, I think, @aliwo your idea was way cleaner to set the context at the root, and worked for me. thanks man.!

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests