use crate::ast;
use crate::tests::utils::parse_executable;
use crate::tests::utils::parse_schema;
pub(super) fn extract_query(source: &str) -> ast::OperationDefinition<'_> {
let (doc, _) = parse_executable(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::OperationDefinition(op))
if op.operation_kind == ast::OperationKind::Query
&& !op.shorthand =>
{
op
},
other => panic!("Expected non-shorthand Query operation, got: {other:?}"),
}
}
pub(super) fn extract_mutation(source: &str) -> ast::OperationDefinition<'_> {
let (doc, _) = parse_executable(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::OperationDefinition(op))
if op.operation_kind == ast::OperationKind::Mutation =>
{
op
},
other => panic!("Expected Mutation operation, got: {other:?}"),
}
}
pub(super) fn extract_subscription(source: &str) -> ast::OperationDefinition<'_> {
let (doc, _) = parse_executable(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::OperationDefinition(op))
if op.operation_kind == ast::OperationKind::Subscription =>
{
op
},
other => panic!("Expected Subscription operation, got: {other:?}"),
}
}
pub(super) fn extract_fragment(source: &str) -> ast::FragmentDefinition<'_> {
let (doc, _) = parse_executable(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::FragmentDefinition(f)) => f,
other => panic!("Expected FragmentDefinition, got: {other:?}"),
}
}
pub(super) fn extract_shorthand_query(source: &str) -> ast::OperationDefinition<'_> {
let (doc, _) = parse_executable(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::OperationDefinition(op)) if op.shorthand => op,
other => {
panic!("Expected shorthand query (OperationDefinition with shorthand=true), got: {other:?}")
},
}
}
pub(super) fn extract_first_type_def(source: &str) -> ast::TypeDefinition<'_> {
let (doc, _) = parse_schema(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::TypeDefinition(td)) => td,
other => panic!("Expected TypeDefinition, got: {other:?}"),
}
}
pub(super) fn extract_first_object_type(source: &str) -> ast::ObjectTypeDefinition<'_> {
match extract_first_type_def(source) {
ast::TypeDefinition::Object(obj) => obj,
other => panic!("Expected ObjectType, got: {other:?}"),
}
}
pub(super) fn extract_first_interface_type(
source: &str,
) -> ast::InterfaceTypeDefinition<'_> {
match extract_first_type_def(source) {
ast::TypeDefinition::Interface(iface) => iface,
other => panic!("Expected InterfaceType, got: {other:?}"),
}
}
pub(super) fn extract_first_enum_type(source: &str) -> ast::EnumTypeDefinition<'_> {
match extract_first_type_def(source) {
ast::TypeDefinition::Enum(e) => e,
other => panic!("Expected EnumType, got: {other:?}"),
}
}
pub(super) fn extract_first_union_type(source: &str) -> ast::UnionTypeDefinition<'_> {
match extract_first_type_def(source) {
ast::TypeDefinition::Union(u) => u,
other => panic!("Expected UnionType, got: {other:?}"),
}
}
pub(super) fn extract_first_input_object_type(
source: &str,
) -> ast::InputObjectTypeDefinition<'_> {
match extract_first_type_def(source) {
ast::TypeDefinition::InputObject(io) => io,
other => panic!("Expected InputObjectType, got: {other:?}"),
}
}
pub(super) fn extract_first_scalar_type(source: &str) -> ast::ScalarTypeDefinition<'_> {
match extract_first_type_def(source) {
ast::TypeDefinition::Scalar(s) => s,
other => panic!("Expected ScalarType, got: {other:?}"),
}
}
pub(super) fn extract_first_directive_def(
source: &str,
) -> ast::DirectiveDefinition<'_> {
let (doc, _) = parse_schema(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::DirectiveDefinition(dd)) => dd,
other => panic!("Expected DirectiveDefinition, got: {other:?}"),
}
}
pub(super) fn extract_schema_def(source: &str) -> ast::SchemaDefinition<'_> {
let (doc, _) = parse_schema(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::SchemaDefinition(sd)) => sd,
other => panic!("Expected SchemaDefinition, got: {other:?}"),
}
}
pub(super) fn extract_first_type_extension(source: &str) -> ast::TypeExtension<'_> {
let (doc, _) = parse_schema(source).into_valid().unwrap();
match doc.definitions.into_iter().next() {
Some(ast::Definition::TypeExtension(te)) => te,
other => panic!("Expected TypeExtension, got: {other:?}"),
}
}
pub(super) fn first_field<'a, 'src>(ss: &'a ast::SelectionSet<'src>) -> &'a ast::FieldSelection<'src> {
match ss.selections.first() {
Some(ast::Selection::Field(f)) => f,
other => panic!("Expected first selection to be Field, got: {other:?}"),
}
}
pub(super) fn field_at<'a, 'src>(
ss: &'a ast::SelectionSet<'src>,
idx: usize,
) -> &'a ast::FieldSelection<'src> {
match ss.selections.get(idx) {
Some(ast::Selection::Field(f)) => f,
other => panic!(
"Expected selection at index {idx} to be Field, got: {other:?}"
),
}
}
pub(super) fn first_fragment_spread<'a, 'src>(
ss: &'a ast::SelectionSet<'src>,
) -> &'a ast::FragmentSpread<'src> {
for item in &ss.selections {
if let ast::Selection::FragmentSpread(fs) = item {
return fs;
}
}
panic!("No FragmentSpread found in selection set");
}
pub(super) fn first_inline_fragment<'a, 'src>(
ss: &'a ast::SelectionSet<'src>,
) -> &'a ast::InlineFragment<'src> {
for item in &ss.selections {
if let ast::Selection::InlineFragment(inf) = item {
return inf;
}
}
panic!("No InlineFragment found in selection set");
}
pub(super) fn first_arg_value<'a, 'src>(field: &'a ast::FieldSelection<'src>) -> &'a ast::Value<'src> {
match field.arguments.first() {
Some(arg) => &arg.value,
None => panic!("Field has no arguments"),
}
}
pub(super) fn inner_type_name<'a, 'src>(ty: &'a ast::TypeAnnotation<'src>) -> &'a str {
match ty {
ast::TypeAnnotation::Named(n) => &n.name.value,
ast::TypeAnnotation::List(l) => inner_type_name(&l.element_type),
}
}
pub(super) fn find_root_op<'a, 'src>(
sd: &'a ast::SchemaDefinition<'src>,
kind: ast::OperationKind,
) -> Option<&'a str> {
sd.root_operations
.iter()
.find(|r| r.operation_kind == kind)
.map(|r| &*r.named_type.value)
}
pub(super) fn object_field_value<'a, 'src>(
obj: &'a ast::ObjectValue<'src>,
name: &str,
) -> Option<&'a ast::Value<'src>> {
obj.fields
.iter()
.find(|f| f.name.value == name)
.map(|f| &f.value)
}