| 1 |
from functools import partial |
| 2 |
|
| 3 |
from graphql.utilities import build_schema |
| 4 |
from graphql.validation import SingleFieldSubscriptionsRule |
| 5 |
|
| 6 |
from .harness import assert_validation_errors |
| 7 |
|
| 8 |
schema = build_schema(""" |
| 9 |
type Message { |
| 10 |
body: String |
| 11 |
sender: String |
| 12 |
} |
| 13 |
|
| 14 |
type SubscriptionRoot { |
| 15 |
importantEmails: [String] |
| 16 |
notImportantEmails: [String] |
| 17 |
moreImportantEmails: [String] |
| 18 |
spamEmails: [String] |
| 19 |
deletedEmails: [String] |
| 20 |
newMessage: Message |
| 21 |
} |
| 22 |
|
| 23 |
type QueryRoot { |
| 24 |
dummy: String |
| 25 |
} |
| 26 |
|
| 27 |
schema { |
| 28 |
query: QueryRoot |
| 29 |
subscription: SubscriptionRoot |
| 30 |
} |
| 31 |
""") |
| 32 |
|
| 33 |
assert_errors = partial( |
| 34 |
assert_validation_errors, SingleFieldSubscriptionsRule, schema=schema |
| 35 |
) |
| 36 |
|
| 37 |
assert_valid = partial(assert_errors, errors=[]) |
| 38 |
|
| 39 |
|
| 40 |
def describe_validate_subscriptions_with_single_field(): |
| 41 |
def valid_subscription(): |
| 42 |
assert_valid(""" |
| 43 |
subscription ImportantEmails { |
| 44 |
importantEmails |
| 45 |
} |
| 46 |
""") |
| 47 |
|
| 48 |
def valid_subscription_with_fragment(): |
| 49 |
assert_valid(""" |
| 50 |
subscription sub { |
| 51 |
...newMessageFields |
| 52 |
} |
| 53 |
|
| 54 |
fragment newMessageFields on SubscriptionRoot { |
| 55 |
newMessage { |
| 56 |
body |
| 57 |
sender |
| 58 |
} |
| 59 |
} |
| 60 |
""") |
| 61 |
|
| 62 |
def valid_subscription_with_fragment_and_field(): |
| 63 |
assert_valid(""" |
| 64 |
subscription sub { |
| 65 |
newMessage { |
| 66 |
body |
| 67 |
} |
| 68 |
...newMessageFields |
| 69 |
} |
| 70 |
|
| 71 |
fragment newMessageFields on SubscriptionRoot { |
| 72 |
newMessage { |
| 73 |
body |
| 74 |
sender |
| 75 |
} |
| 76 |
} |
| 77 |
""") |
| 78 |
|
| 79 |
def fails_with_more_than_one_root_field(): |
| 80 |
assert_errors( |
| 81 |
""" |
| 82 |
subscription ImportantEmails { |
| 83 |
importantEmails |
| 84 |
notImportantEmails |
| 85 |
} |
| 86 |
""", |
| 87 |
[ |
| 88 |
{ |
| 89 |
"message": "Subscription 'ImportantEmails'" |
| 90 |
" must select only one top level field.", |
| 91 |
"locations": [(4, 15)], |
| 92 |
} |
| 93 |
], |
| 94 |
) |
| 95 |
|
| 96 |
def fails_with_more_than_one_root_field_including_introspection(): |
| 97 |
assert_errors( |
| 98 |
""" |
| 99 |
subscription ImportantEmails { |
| 100 |
importantEmails |
| 101 |
__typename |
| 102 |
} |
| 103 |
""", |
| 104 |
[ |
| 105 |
{ |
| 106 |
"message": "Subscription 'ImportantEmails'" |
| 107 |
" must select only one top level field.", |
| 108 |
"locations": [(4, 15)], |
| 109 |
}, |
| 110 |
{ |
| 111 |
"message": "Subscription 'ImportantEmails'" |
| 112 |
" must not select an introspection top level field.", |
| 113 |
"locations": [(4, 15)], |
| 114 |
}, |
| 115 |
], |
| 116 |
) |
| 117 |
|
| 118 |
def fails_with_more_than_one_root_field_including_aliased_introspection(): |
| 119 |
assert_errors( |
| 120 |
""" |
| 121 |
subscription ImportantEmails { |
| 122 |
importantEmails |
| 123 |
...Introspection |
| 124 |
} |
| 125 |
fragment Introspection on SubscriptionRoot { |
| 126 |
typename: __typename |
| 127 |
} |
| 128 |
""", |
| 129 |
[ |
| 130 |
{ |
| 131 |
"message": "Subscription 'ImportantEmails'" |
| 132 |
" must select only one top level field.", |
| 133 |
"locations": [(7, 15)], |
| 134 |
}, |
| 135 |
{ |
| 136 |
"message": "Subscription 'ImportantEmails'" |
| 137 |
" must not select an introspection top level field.", |
| 138 |
"locations": [(7, 15)], |
| 139 |
}, |
| 140 |
], |
| 141 |
) |
| 142 |
|
| 143 |
def fails_with_many_more_than_one_root_field(): |
| 144 |
assert_errors( |
| 145 |
""" |
| 146 |
subscription ImportantEmails { |
| 147 |
importantEmails |
| 148 |
notImportantEmails |
| 149 |
spamEmails |
| 150 |
} |
| 151 |
""", |
| 152 |
[ |
| 153 |
{ |
| 154 |
"message": "Subscription 'ImportantEmails'" |
| 155 |
" must select only one top level field.", |
| 156 |
"locations": [(4, 15), (5, 15)], |
| 157 |
} |
| 158 |
], |
| 159 |
) |
| 160 |
|
| 161 |
def fails_with_more_than_one_root_field_via_fragments(): |
| 162 |
assert_errors( |
| 163 |
""" |
| 164 |
subscription ImportantEmails { |
| 165 |
importantEmails |
| 166 |
... { |
| 167 |
more: moreImportantEmails |
| 168 |
} |
| 169 |
...NotImportantEmails |
| 170 |
} |
| 171 |
fragment NotImportantEmails on SubscriptionRoot { |
| 172 |
notImportantEmails |
| 173 |
deleted: deletedEmails |
| 174 |
...SpamEmails |
| 175 |
} |
| 176 |
fragment SpamEmails on SubscriptionRoot { |
| 177 |
spamEmails |
| 178 |
} |
| 179 |
""", |
| 180 |
[ |
| 181 |
{ |
| 182 |
"message": "Subscription 'ImportantEmails'" |
| 183 |
" must select only one top level field.", |
| 184 |
"locations": [(5, 17), (10, 15), (11, 15), (15, 15)], |
| 185 |
}, |
| 186 |
], |
| 187 |
) |
| 188 |
|
| 189 |
def does_not_infinite_loop_on_recursive_fragments(): |
| 190 |
assert_errors( |
| 191 |
""" |
| 192 |
subscription NoInfiniteLoop { |
| 193 |
...A |
| 194 |
} |
| 195 |
fragment A on SubscriptionRoot { |
| 196 |
...A |
| 197 |
} |
| 198 |
""", |
| 199 |
[], |
| 200 |
) |
| 201 |
|
| 202 |
def fails_with_more_than_one_root_field_via_fragments_anonymous(): |
| 203 |
assert_errors( |
| 204 |
""" |
| 205 |
subscription { |
| 206 |
importantEmails |
| 207 |
... { |
| 208 |
more: moreImportantEmails |
| 209 |
...NotImportantEmails |
| 210 |
} |
| 211 |
...NotImportantEmails |
| 212 |
} |
| 213 |
fragment NotImportantEmails on SubscriptionRoot { |
| 214 |
notImportantEmails |
| 215 |
deleted: deletedEmails |
| 216 |
... { |
| 217 |
... { |
| 218 |
archivedEmails |
| 219 |
} |
| 220 |
} |
| 221 |
...SpamEmails |
| 222 |
} |
| 223 |
fragment SpamEmails on SubscriptionRoot { |
| 224 |
spamEmails |
| 225 |
...NonExistentFragment |
| 226 |
} |
| 227 |
""", |
| 228 |
[ |
| 229 |
{ |
| 230 |
"message": "Anonymous Subscription" |
| 231 |
" must select only one top level field.", |
| 232 |
"locations": [(5, 17), (11, 15), (12, 15), (15, 19), (21, 15)], |
| 233 |
}, |
| 234 |
], |
| 235 |
) |
| 236 |
|
| 237 |
def fails_with_more_than_one_root_field_in_anonymous_subscriptions(): |
| 238 |
assert_errors( |
| 239 |
""" |
| 240 |
subscription { |
| 241 |
importantEmails |
| 242 |
notImportantEmails |
| 243 |
} |
| 244 |
""", |
| 245 |
[ |
| 246 |
{ |
| 247 |
"message": "Anonymous Subscription" |
| 248 |
" must select only one top level field.", |
| 249 |
"locations": [(4, 15)], |
| 250 |
} |
| 251 |
], |
| 252 |
) |
| 253 |
|
| 254 |
def fails_with_introspection_field(): |
| 255 |
assert_errors( |
| 256 |
""" |
| 257 |
subscription ImportantEmails { |
| 258 |
__typename |
| 259 |
} |
| 260 |
""", |
| 261 |
[ |
| 262 |
{ |
| 263 |
"message": "Subscription 'ImportantEmails' must not" |
| 264 |
" select an introspection top level field.", |
| 265 |
"locations": [(3, 15)], |
| 266 |
} |
| 267 |
], |
| 268 |
) |
| 269 |
|
| 270 |
def fails_with_introspection_field_in_anonymous_subscription(): |
| 271 |
assert_errors( |
| 272 |
""" |
| 273 |
subscription { |
| 274 |
__typename |
| 275 |
} |
| 276 |
""", |
| 277 |
[ |
| 278 |
{ |
| 279 |
"message": "Anonymous Subscription must not" |
| 280 |
" select an introspection top level field.", |
| 281 |
"locations": [(3, 15)], |
| 282 |
} |
| 283 |
], |
| 284 |
) |
| 285 |
|
| 286 |
def skips_if_not_subscription_type(): |
| 287 |
empty_schema = build_schema(""" |
| 288 |
type Query { |
| 289 |
dummy: String |
| 290 |
} |
| 291 |
""") |
| 292 |
assert_errors( |
| 293 |
""" |
| 294 |
subscription { |
| 295 |
__typename |
| 296 |
} |
| 297 |
""", |
| 298 |
[], |
| 299 |
schema=empty_schema, |
| 300 |
) |