diff --git a/docs/examples.rst b/docs/examples.rst new file mode 100644 index 00000000..50b48fab --- /dev/null +++ b/docs/examples.rst @@ -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 + } + } diff --git a/docs/index.rst b/docs/index.rst index 1bc22349..81b2f316 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -8,3 +8,4 @@ Contents: tutorial tips + examples