|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from dummy_app.models import AuthorModel, BookModel, LibraryModel, MagazineModel |
| 16 | +from graphene import Field, Int, List, NonNull, ObjectType, Schema, String, Union |
| 17 | +from graphene import Mutation as GrapheneMutation |
| 18 | +from graphene_django import DjangoObjectType |
| 19 | + |
| 20 | + |
| 21 | +class Author(DjangoObjectType): |
| 22 | + class Meta: |
| 23 | + model = AuthorModel |
| 24 | + fields = "__all__" |
| 25 | + |
| 26 | + |
| 27 | +class Book(DjangoObjectType): |
| 28 | + class Meta: |
| 29 | + model = BookModel |
| 30 | + fields = "__all__" |
| 31 | + |
| 32 | + |
| 33 | +class Magazine(DjangoObjectType): |
| 34 | + class Meta: |
| 35 | + model = MagazineModel |
| 36 | + fields = "__all__" |
| 37 | + |
| 38 | + |
| 39 | +class Item(Union): |
| 40 | + class Meta: |
| 41 | + types = (Book, Magazine) |
| 42 | + |
| 43 | + |
| 44 | +class Library(DjangoObjectType): |
| 45 | + class Meta: |
| 46 | + model = LibraryModel |
| 47 | + fields = "__all__" |
| 48 | + |
| 49 | + |
| 50 | +Storage = List(String) |
| 51 | + |
| 52 | + |
| 53 | +authors = [ |
| 54 | + Author(first_name="New", last_name="Relic"), |
| 55 | + Author(first_name="Bob", last_name="Smith"), |
| 56 | + Author(first_name="Leslie", last_name="Jones"), |
| 57 | +] |
| 58 | + |
| 59 | +books = [ |
| 60 | + Book(id=1, name="Python Agent: The Book", isbn="a-fake-isbn", author=authors[0], branch="riverside"), |
| 61 | + Book( |
| 62 | + id=2, |
| 63 | + name="Ollies for O11y: A Sk8er's Guide to Observability", |
| 64 | + isbn="a-second-fake-isbn", |
| 65 | + author=authors[1], |
| 66 | + branch="downtown", |
| 67 | + ), |
| 68 | + Book(id=3, name="[Redacted]", isbn="a-third-fake-isbn", author=authors[2], branch="riverside"), |
| 69 | +] |
| 70 | + |
| 71 | +magazines = [ |
| 72 | + Magazine(id=1, name="Reli Updates Weekly", issue=1, branch="riverside"), |
| 73 | + Magazine(id=2, name="Reli Updates Weekly", issue=2, branch="downtown"), |
| 74 | + Magazine(id=3, name="Node Weekly", issue=1, branch="riverside"), |
| 75 | +] |
| 76 | + |
| 77 | + |
| 78 | +libraries = ["riverside", "downtown"] |
| 79 | +libraries = [ |
| 80 | + Library( |
| 81 | + id=i + 1, |
| 82 | + branch=branch, |
| 83 | + magazine=[m for m in magazines if m.branch == branch], |
| 84 | + book=[b for b in books if b.branch == branch], |
| 85 | + ) |
| 86 | + for i, branch in enumerate(libraries) |
| 87 | +] |
| 88 | + |
| 89 | +storage = [] |
| 90 | + |
| 91 | + |
| 92 | +def resolve_library(self, info, index): |
| 93 | + return libraries[index] |
| 94 | + |
| 95 | + |
| 96 | +def resolve_storage(self, info): |
| 97 | + return [storage.pop()] |
| 98 | + |
| 99 | + |
| 100 | +def resolve_search(self, info, contains): |
| 101 | + search_books = [b for b in books if contains in b.name] |
| 102 | + search_magazines = [m for m in magazines if contains in m.name] |
| 103 | + return search_books + search_magazines |
| 104 | + |
| 105 | + |
| 106 | +def resolve_hello(self, info): |
| 107 | + return "Hello!" |
| 108 | + |
| 109 | + |
| 110 | +def resolve_echo(self, info, echo): |
| 111 | + return echo |
| 112 | + |
| 113 | + |
| 114 | +def resolve_error(self, info): |
| 115 | + raise RuntimeError("Runtime Error!") |
| 116 | + |
| 117 | + |
| 118 | +def resolve_storage_add(self, info, string): |
| 119 | + storage.append(string) |
| 120 | + return StorageAdd(string=string) |
| 121 | + |
| 122 | + |
| 123 | +class StorageAdd(GrapheneMutation): |
| 124 | + class Arguments: |
| 125 | + string = String(required=True) |
| 126 | + |
| 127 | + string = String() |
| 128 | + mutate = resolve_storage_add |
| 129 | + |
| 130 | + |
| 131 | +class Query(ObjectType): |
| 132 | + library = Field(Library, index=Int(required=True), resolver=resolve_library) |
| 133 | + hello = String(resolver=resolve_hello) |
| 134 | + search = Field(List(Item), contains=String(required=True), resolver=resolve_search) |
| 135 | + echo = Field(String, echo=String(required=True), resolver=resolve_echo) |
| 136 | + storage = Field(Storage, resolver=resolve_storage) |
| 137 | + error = String(resolver=resolve_error) |
| 138 | + error_non_null = Field(NonNull(String), resolver=resolve_error) |
| 139 | + error_middleware = String(resolver=resolve_hello) |
| 140 | + |
| 141 | + |
| 142 | +class Mutation(ObjectType): |
| 143 | + storage_add = StorageAdd.Field() |
| 144 | + |
| 145 | + |
| 146 | +target_schema = Schema(query=Query, mutation=Mutation, auto_camelcase=False) |
0 commit comments