|
| 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 | +from graphene import ( |
| 15 | + ObjectType, |
| 16 | + Field, |
| 17 | + String, |
| 18 | + Schema, |
| 19 | + Mutation as GrapheneMutation, |
| 20 | + Int, |
| 21 | + List, |
| 22 | + NonNull, |
| 23 | + Union, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +class Author(ObjectType): |
| 28 | + first_name = String() |
| 29 | + last_name = String() |
| 30 | + |
| 31 | + |
| 32 | +class Book(ObjectType): |
| 33 | + id = Int() |
| 34 | + name = String() |
| 35 | + isbn = String() |
| 36 | + author = Field(Author) |
| 37 | + branch = String() |
| 38 | + |
| 39 | + |
| 40 | +class Magazine(ObjectType): |
| 41 | + id = Int() |
| 42 | + name = String() |
| 43 | + issue = Int() |
| 44 | + branch = String() |
| 45 | + |
| 46 | + |
| 47 | +class Item(Union): |
| 48 | + class Meta: |
| 49 | + types = (Book, Magazine) |
| 50 | + |
| 51 | + |
| 52 | +class Library(ObjectType): |
| 53 | + id = Int() |
| 54 | + branch = String() |
| 55 | + magazine = Field(List(Magazine)) |
| 56 | + book = Field(List(Book)) |
| 57 | + |
| 58 | + |
| 59 | +Storage = List(String) |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +authors = [ |
| 64 | + Author( |
| 65 | + first_name="New", |
| 66 | + last_name="Relic", |
| 67 | + ), |
| 68 | + Author( |
| 69 | + first_name="Bob", |
| 70 | + last_name="Smith", |
| 71 | + ), |
| 72 | + Author( |
| 73 | + first_name="Leslie", |
| 74 | + last_name="Jones", |
| 75 | + ), |
| 76 | +] |
| 77 | + |
| 78 | +books = [ |
| 79 | + Book( |
| 80 | + id=1, |
| 81 | + name="Python Agent: The Book", |
| 82 | + isbn="a-fake-isbn", |
| 83 | + author=authors[0], |
| 84 | + branch="riverside", |
| 85 | + ), |
| 86 | + Book( |
| 87 | + id=2, |
| 88 | + name="Ollies for O11y: A Sk8er's Guide to Observability", |
| 89 | + isbn="a-second-fake-isbn", |
| 90 | + author=authors[1], |
| 91 | + branch="downtown", |
| 92 | + ), |
| 93 | + Book( |
| 94 | + id=3, |
| 95 | + name="[Redacted]", |
| 96 | + isbn="a-third-fake-isbn", |
| 97 | + author=authors[2], |
| 98 | + branch="riverside", |
| 99 | + ), |
| 100 | +] |
| 101 | + |
| 102 | +magazines = [ |
| 103 | + Magazine(id=1, name="Reli Updates Weekly", issue=1, branch="riverside"), |
| 104 | + Magazine(id=2, name="Reli Updates Weekly", issue=2, branch="downtown"), |
| 105 | + Magazine(id=3, name="Node Weekly", issue=1, branch="riverside"), |
| 106 | +] |
| 107 | + |
| 108 | + |
| 109 | +libraries = ["riverside", "downtown"] |
| 110 | +libraries = [ |
| 111 | + Library( |
| 112 | + id=i + 1, |
| 113 | + branch=branch, |
| 114 | + magazine=[m for m in magazines if m.branch == branch], |
| 115 | + book=[b for b in books if b.branch == branch], |
| 116 | + ) |
| 117 | + for i, branch in enumerate(libraries) |
| 118 | +] |
| 119 | + |
| 120 | +storage = [] |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +class StorageAdd(GrapheneMutation): |
| 125 | + class Arguments: |
| 126 | + string = String(required=True) |
| 127 | + |
| 128 | + string = String() |
| 129 | + |
| 130 | + def mutate(parent, info, string): |
| 131 | + storage.append(string) |
| 132 | + return String(string=string) |
| 133 | + |
| 134 | + |
| 135 | +class Query(ObjectType): |
| 136 | + library = Field(Library, index=Int(required=True)) |
| 137 | + hello = String() |
| 138 | + search = Field(List(Item), contains=String(required=True)) |
| 139 | + echo = Field(String, echo=String(required=True)) |
| 140 | + storage = Storage |
| 141 | + error = String() |
| 142 | + |
| 143 | + def resolve_library(parent, info, index): |
| 144 | + # returns an object that represents a Person |
| 145 | + return libraries[index] |
| 146 | + |
| 147 | + def resolve_storage(parent, info): |
| 148 | + return storage |
| 149 | + |
| 150 | + def resolve_search(parent, info, contains): |
| 151 | + search_books = [b for b in books if contains in b.name] |
| 152 | + search_magazines = [m for m in magazines if contains in m.name] |
| 153 | + return search_books + search_magazines |
| 154 | + |
| 155 | + def resolve_hello(root, info): |
| 156 | + return "Hello!" |
| 157 | + |
| 158 | + def resolve_echo(root, info, echo): |
| 159 | + return echo |
| 160 | + |
| 161 | + def resolve_error(root, info): |
| 162 | + raise RuntimeError("Runtime Error!") |
| 163 | + |
| 164 | + error_non_null = Field(NonNull(String), resolver=resolve_error) |
| 165 | + |
| 166 | + |
| 167 | +class Mutation(ObjectType): |
| 168 | + storage_add = StorageAdd.Field() |
| 169 | + |
| 170 | +_target_application = Schema(query=Query, mutation=Mutation, auto_camelcase=False) |
0 commit comments