Skip to content

Example Page #84

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

Merged
merged 2 commits into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
Schema Examples
===========================


Search all Models with Union
-----------------

.. code:: python

class Book(SQLAlchemyObjectType):
class Meta:
model = BookModel
interfaces = (relay.Node,)


class Author(SQLAlchemyObjectType):
class Meta:
model = AuthorModel
interfaces = (relay.Node,)


class SearchResult(graphene.Union):
class Meta:
types = (Book, Author)


class Query(graphene.ObjectType):
node = relay.Node.Field()
search = graphene.List(SearchResult, q=graphene.String()) # List field for search results

# Normal Fields
all_books = SQLAlchemyConnectionField(Book)
all_authors = SQLAlchemyConnectionField(Author)

def resolve_search(self, info, **args):
q = args.get("q") # Search query

# Get queries
bookdata_query = BookData.get_query(info)
author_query = Author.get_query(info)

# Query Books
books = bookdata_query.filter((BookModel.title.contains(q)) |
(BookModel.isbn.contains(q)) |
(BookModel.authors.any(AuthorModel.name.contains(q)))).all()

# Query Authors
authors = author_query.filter(AuthorModel.name.contains(q)).all()

return authors + books # Combine lists

schema = graphene.Schema(query=Query, types=[Book, Author, SearchResult])

Example GraphQL query

.. code:: GraphQL

book(id: "Qm9vazow") {
id
title
}
search(q: "Making Games") {
__typename
... on Author {
fname
lname
}
... on Book {
title
isbn
}
}
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Contents:

tutorial
tips
examples