【发布时间】:2018-07-26 04:17:09
【问题描述】:
我目前为我的项目定义了以下突变:
我的班级PlanetAttribute 用于定义石墨烯字段,用作我的突变的输入
class PlanetAttribute:
name = graphene.String(required=True, description="Name of the planet.")
rotation_period = graphene.String(default_value="unknown", description="Rotation period of the planet.")
orbital_period = graphene.String(default_value="unknown", description="Orbital period of the planet.")
diameter = graphene.String(default_value="unknown", description="Diameter of the planet.")
climate = graphene.String(default_value="unknown", description="Climate period of the planet.")
gravity = graphene.String(default_value="unknown", description="Gravity of the planet.")
terrain = graphene.String(default_value="unknown", description="Terrain of the planet.")
surface_water = graphene.String(default_value="unknown", description="Surface water of the planet.")
population = graphene.String(default_value="unknown", description="Population of the planet.")
url = graphene.String(default_value="unknown", description="URL of the planet in the Star Wars API.")
我的类CreatePlanetInput 用于定义石墨烯输入对象类型。请注意,它从上面定义的PlanetAttribute 类继承其属性。
class CreatePlanetInput(graphene.InputObjectType, PlanetAttribute):
"""Arguments to create a planet."""
pass
我的CreatePlanet 类是我的石墨烯突变类,它以CreatePlanetInput 类作为参数。
class CreatePlanet(graphene.Mutation):
"""Create a planet."""
planet = graphene.Field(lambda: Planet, description="Planet created by this mutation.")
class Arguments:
input = CreatePlanetInput(required=True)
def mutate(self, info, input):
data = utils.input_to_dictionary(input)
data['created'] = datetime.utcnow()
data['edited'] = datetime.utcnow()
planet = ModelPlanet(**data)
db_session.add(planet)
db_session.commit()
return CreatePlanet(planet=planet)
与其在PlanetAttribute 类中手动声明变异输入,我宁愿从我的SQLALchemy 类ModelPlanet 动态生成它们,定义如下:
class ModelPlanet(Base):
"""Planet model."""
__tablename__ = 'planet'
id = Column('id', Integer, primary_key=True, doc="Id of the person.")
name = Column('name', String, doc="Name of the planet.")
rotation_period = Column('rotation_period', String, doc="Rotation period of the planet.")
orbital_period = Column('orbital_period', String, doc="Orbital period of the planet.")
diameter = Column('diameter', String, doc="Diameter of the planet.")
climate = Column('climate', String, doc="Climate period of the planet.")
gravity = Column('gravity', String, doc="Gravity of the planet.")
terrain = Column('terrain', String, doc="Terrain of the planet.")
surface_water = Column('surface_water', String, doc="Surface water of the planet.")
population = Column('population', String, doc="Population of the planet.")
created = Column('created', String, doc="Record created date.")
edited = Column('edited', String, doc="Record last updated date.")
url = Column('url', String, doc="URL of the planet in the Star Wars API.")
peopleList = relationship(ModelPeople, backref='planet')
你会怎么做?
请注意,我也在此处发布了问题:https://github.com/graphql-python/graphene-sqlalchemy/issues/112
【问题讨论】:
标签: python sqlalchemy graphene-python