From 248136d0b644dbe5f73cd0cc903a6a1c594e87a9 Mon Sep 17 00:00:00 2001 From: Oliver Cooper Date: Thu, 15 Jul 2021 15:51:44 +1200 Subject: [PATCH 1/3] Added From<&[SelectionField<'a>]> for Lookahead<'a> --- src/look_ahead.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/look_ahead.rs b/src/look_ahead.rs index 38f43db6..2c78eda6 100644 --- a/src/look_ahead.rs +++ b/src/look_ahead.rs @@ -53,6 +53,24 @@ impl<'a> From> for Lookahead<'a> { } } +/// Convert a slice of `SelectionField`s to a `Lookahead`. +/// +/// This assumes that the `SelectionField`s are all from the same query. +/// +/// # Panics +/// Panics if 0 `SelectionField`s are provided. +impl<'a> From<&[SelectionField<'a>]> for Lookahead<'a> { + fn from(selection_fields: &[SelectionField<'a>]) -> Self { + Lookahead { + fragments: selection_fields[0].fragments, + fields: selection_fields + .iter() + .map(|selection_field| selection_field.field) + .collect(), + } + } +} + fn filter<'a>( fields: &mut Vec<&'a Field>, fragments: &'a HashMap>, From a1c9c6c8bafcde2008e155650b40872b49217c47 Mon Sep 17 00:00:00 2001 From: Oliver Cooper Date: Thu, 15 Jul 2021 16:03:33 +1200 Subject: [PATCH 2/3] Replaced panicking From with TryFrom --- src/look_ahead.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/look_ahead.rs b/src/look_ahead.rs index 2c78eda6..1dab6bf1 100644 --- a/src/look_ahead.rs +++ b/src/look_ahead.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::convert::TryFrom; use crate::parser::types::{Field, FragmentDefinition, Selection, SelectionSet}; use crate::{Name, Positioned, SelectionField}; @@ -54,19 +55,23 @@ impl<'a> From> for Lookahead<'a> { } /// Convert a slice of `SelectionField`s to a `Lookahead`. +/// Assumes all `SelectionField`s are from the same query and thus have the same fragments. /// -/// This assumes that the `SelectionField`s are all from the same query. -/// -/// # Panics -/// Panics if 0 `SelectionField`s are provided. -impl<'a> From<&[SelectionField<'a>]> for Lookahead<'a> { - fn from(selection_fields: &[SelectionField<'a>]) -> Self { - Lookahead { - fragments: selection_fields[0].fragments, - fields: selection_fields - .iter() - .map(|selection_field| selection_field.field) - .collect(), +/// Fails if either no `SelectionField`s were provided. +impl<'a> TryFrom<&[SelectionField<'a>]> for Lookahead<'a> { + type Error = (); + + fn try_from(selection_fields: &[SelectionField<'a>]) -> Result { + if selection_fields.is_empty() { + Err(()) + } else { + Ok(Lookahead { + fragments: selection_fields[0].fragments, + fields: selection_fields + .iter() + .map(|selection_field| selection_field.field) + .collect(), + }) } } } From cc34213317bf065252fe21d94af4d078efb26153 Mon Sep 17 00:00:00 2001 From: Oliver Cooper Date: Thu, 15 Jul 2021 16:05:31 +1200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aea3fc5e..7c7c7efe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Add binary types to `ConstValue` and `Value`. [#569](https://github.com/async-graphql/async-graphql/issues/569) -- Changed Lookahead to support multiple fields. [#574](https://github.com/async-graphql/async-graphql/issues/547) +- Changed Lookahead to support multiple fields. [#574](https://github.com/async-graphql/async-graphql/issues/574) +- Implement `TryFrom<&[SelectionField<'a>]>` for `Lookahead<'a>`. [#575](https://github.com/async-graphql/async-graphql/issues/575) ## [2.9.8] 2021-07-12