Reputation: 5654
I have a tag model and I would like to search for a specific tag with a specific name. Here is what I have so far.
from datetime import datetime
from graphene_sqlalchemy import SQLAlchemyObjectType,SQLAlchemyConnectionField
from database.base import db_session
from database.models.model_post import ModelPost
from database.models.model_image import ModelImage
from database.models.model_posttag import PostsTags
from database.models.model_tag import ModelTag
from database.models.model_user import ModelUser
from database.models.model_locale import ModelLocale
import graphene
from helpers import utils
# Create a generic class to mutualize description of user attributes for both queries and mutations
class TagAttribute:
name = graphene.String(description="Tag Name")
isactive = graphene.Boolean(description="is active")
class Tag(SQLAlchemyObjectType):
"""Tag node."""
class Meta:
model = ModelTag
interfaces = (graphene.relay.Node,)
class Query(graphene.ObjectType):
tagToFind = graphene.Field(lambda: Tag, name=graphene.String())
def resolve_find_tag(self, info, **kwargs):
query = Tag.get_query(info)
name = kwargs.get("name")
return query.filter(TagModel.name == name).first()
class CreateTagInput(graphene.InputObjectType, TagAttribute):
"""Arguments to create a tag."""
pass
class CreateTag(graphene.Mutation):
"""Mutation to create a tag."""
tag = graphene.Field(lambda: Tag, description="tag created by this mutation.")
class Arguments:
input = CreateTagInput(required=True)
def mutate(self, info, input):
data = utils.input_to_dictionary(input)
data['created_at'] = datetime.utcnow()
data['updated_at'] = datetime.utcnow()
tag = ModelTag(**data)
db_session.add(tag)
db_session.commit()
return CreateTag(tag=tag)
class UpdateTagInput(graphene.InputObjectType, TagAttribute):
"""Arguments to update a tag."""
id = graphene.ID(required=True, description="Global Id of the tag.")
class UpdateTag(graphene.Mutation):
"""Update a tag."""
tag = graphene.Field(lambda: Tag, description="tag updated by this mutation.")
class Arguments:
input = UpdateTagInput(required=True)
def mutate(self, info, input):
data = utils.input_to_dictionary(input)
data['updated_at'] = datetime.utcnow()
tag = db_session.query(ModelTag).filter_by(id=data['id'])
tag.update(data)
db_session.commit()
tag = db_session.query(ModelTag).filter_by(id=data['id']).first()
return UpdateTag(tag=tag)
Here is a query that I have
query FindTagByName($name: String ="searchstring"){
findTag(name: $name) {
id
name
}
}
but I am getting this error
Can you please tell me what I am doing wrong here ? Thanks
Upvotes: 3
Views: 474
Reputation: 452
Your Query
has tagToFind
fileld (that should be in snake case) but you resolve a find_tag
so graphene couldn't find it.
Change tagToFind
to find_tag
to fix it.
Upvotes: 1