Skip to content

Instantly share code, notes, and snippets.

@ssomnoremac
Last active May 1, 2018 14:43
Show Gist options
  • Save ssomnoremac/ccd5ccc84307a8dc06784c50eb1158c8 to your computer and use it in GitHub Desktop.
Save ssomnoremac/ccd5ccc84307a8dc06784c50eb1158c8 to your computer and use it in GitHub Desktop.
schema.py for graphene and sqlalchemy
import graphene
from graphene_sqlalchemy import SQLAlchemyConnectionField, SQLAlchemyObjectType
from models import *
class Person(SQLAlchemyObjectType):
class Meta:
model = PersonModel
interfaces = (graphene.relay.Node, )
class Article(SQLAlchemyObjectType):
class Meta:
model = ArticleModel
interfaces = (graphene.relay.Node, )
class Query(graphene.ObjectType):
node = graphene.relay.Node.Field()
person = graphene.Field(Person, uuid = graphene.Int())
def resolve_person(self, args, context, info):
query = Person.get_query(context)
uuid = args.get('uuid')
return query.get(uuid)
schema = graphene.Schema(query=Query, types=[Person])
@Yeboster
Copy link

Hi,
Thank you for sharing your examples.
I've tryed to follow you guidelines but when I'm trying to query it give me error = "resolve_quote() got an unexpected keyword argument 'id'"
Here is schema:

`class Quote(SQLAlchemyObjectType):
	class Meta:
		model = QuoteModel
		interfaces = (relay.Node, )

class Query(ObjectType):
	node = relay.Node.Field()
	#for one quote
	quote = graphene.Field(Quote,ids = graphene.Int())
	#for all quotes
	all_quotes = SQLAlchemyConnectionField(Quote)

	'''
	This return a quote selected by the id
	'''
	def resolve_quote(self, args, context, info):
		query = Quote.get_query(context)
		id = args.get('id')
		return query.get(id)

schema = Schema(query=Query, types=[Quote])`

Here is the query:

query{
quote(id:2){
  date
}
}

Here is the how a quote object should result:

{
"data": {
"allQuotes": {
"edges": [
        {
          "node": {
            "id": "UXVvdGU6MQ==",
            "date": "2018-04-03",
            "quote": "Obey your soul, always.",
            "author": "Yeboster"
          }
        },
OMITTED LINES
]
}
}

Also, I'm using postgre with Serial type as Primary key; Why is id looking like a string instead of a int ?
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment