同じclass内のメソッドを呼び出すことはできないっぽい
class Query(graphene.ObjectType):
@classmethod
def q_all_employees(self):
records = SQLAlchemyConnectionField(Employee)
return records
node = relay.Node.Field()
all_employees = q_all_employees()
all_departments = SQLAlchemyConnectionField(Department)
[root@localhost flask_sqlalchemy]# python app.py
Traceback (most recent call last):
File "app.py", line 5, in <module>
from schema import schema, Department
File "/root/study2/flask_sqlalchemy/schema.py", line 24, in <module>
class Query(graphene.ObjectType):
File "/root/study2/flask_sqlalchemy/schema.py", line 33, in Query
all_employees = q_all_employees()
TypeError: 'classmethod' object is not callable
参考:https://stackoverflow.com/questions/11058686/classmethod-object-is-not-callable
仕方がないのでclass外に切り出し
def q_all_employees():
records = SQLAlchemyConnectionField(Employee)
return records
class Query(graphene.ObjectType):
node = relay.Node.Field()
all_employees = q_all_employees()
all_departments = SQLAlchemyConnectionField(Department)
値の返し方
returnを使う
def q_all_employees():
records = SQLAlchemyConnectionField(Employee)
return records
何も返したくない場合はreturnを記述しない。その場合、呼び出し元で戻り値を期待している場合はエラーになる。
- 本当に何も返す必要が無い(nullさえも返す必要が無い)場合はreturn記述無し
- 何かを返したい場合returnを使う
- 要検証:nullを返したい場合はreturn Noneを使う
デバッグ色々
デバッグしたい箇所にimport pdb; pdb.set_trace()
を記述する。
これによって記述した箇所が処理された段階でconsoleにpdbとかいうのが立ち上がって色々操作できるようになる。
def q_all_employees():
records = SQLAlchemyConnectionField(Employee)
import pdb; pdb.set_trace()
return records
以下pdbでの操作色々
変数の値を確認する
pもしくはppを使用する(ppはpretty printの略っぽい。整形されて見やすくなる)
-> import pdb; pdb.set_trace()
(Pdb) pp records
<graphene_sqlalchemy.fields.SQLAlchemyConnectionField object at 0x7f028a2f9ac8>
オブジェクトのメソッド(attributes)一覧を取得する
dir
メソッドを使用する
(Pdb) pp dir(records)
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_type',
'args',
'connection_resolver',
'creation_counter',
'default_value',
'deprecation_reason',
'description',
'gen_counter',
'get_query',
'get_resolver',
'model',
'mounted',
'name',
'reset_counter',
'resolve_connection',
'resolver',
'type']
オブジェクトのclass名を取得する
__class__
メソッドでclassを取得、classに対して__name__
メソッドでclass名を取得
(Pdb) pp records.__class__
<class 'graphene_sqlalchemy.fields.SQLAlchemyConnectionField'>
(Pdb) pp records.__class__.__name__
'SQLAlchemyConnectionField'