Skip to content

Commit 59d0831

Browse files
committed
subscription
1 parent a21f3cb commit 59d0831

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/ast/operation_visitor.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,26 @@ fn visit_definitions<'a, Visitor, UserContext>(
184184
})
185185
}
186186
OperationDefinition::Subscription(_) => {
187-
context.schema.subscription_type().map(|t| &t.name)
187+
context
188+
.schema
189+
.subscription_type()
190+
.map(|t| &t.name)
191+
.or_else(|| {
192+
// Awkward hack but enables me to move forward
193+
// Somehow the `subscription_type()` gives None, even though `Subscription` type is defined in the schema.
194+
if let Some(type_definition) =
195+
context.schema.type_by_name("Subscription")
196+
{
197+
return match type_definition {
198+
graphql_parser::schema::TypeDefinition::Object(object_type) => {
199+
Some(&object_type.name)
200+
}
201+
_ => None,
202+
};
203+
}
204+
205+
None
206+
})
188207
}
189208
},
190209
};

src/validation/rules/fields_on_correct_type.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ pub static FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA: &str = "
115115
type Mutation {
116116
deletePetByName(name: String): Pet
117117
}
118+
type Subscription {
119+
onNewPet: Pet
120+
}
118121
";
119122

120123
#[test]
@@ -258,6 +261,27 @@ fn unknown_mutation_field() {
258261
);
259262
}
260263

264+
#[test]
265+
fn unknown_subscription_field() {
266+
use crate::validation::test_utils::*;
267+
268+
let mut plan = create_plan_from_rule(Box::new(FieldsOnCorrectType {}));
269+
let errors = test_operation_with_schema(
270+
"subscription test {
271+
unknownField
272+
}",
273+
&FIELDS_ON_CORRECT_TYPE_TEST_SCHEMA,
274+
&mut plan,
275+
);
276+
277+
let messages = get_messages(&errors);
278+
assert_eq!(messages.len(), 1);
279+
assert_eq!(
280+
messages,
281+
vec!["Cannot query field \"unknownField\" on type \"Subscription\"."]
282+
);
283+
}
284+
261285
#[test]
262286
fn reports_errors_when_type_is_known_again() {
263287
use crate::validation::test_utils::*;

0 commit comments

Comments
 (0)