Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: query interface with fragments #3238

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: query interface with fragments
  • Loading branch information
MedHeikelBouzayene committed Jan 4, 2025
commit d23720edc17b336e51a64371692c420b74ea2791
14 changes: 14 additions & 0 deletions src/core/blueprint/index.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use indexmap::IndexMap;

use super::InputObjectTypeDefinition;
Expand Down Expand Up @@ -38,6 +40,18 @@ impl Index {
matches!(def, Some(Definition::Scalar(_))) || scalar::Scalar::is_predefined(type_name)
}

pub fn get_interfaces(&self) -> Box<HashSet<String>> {
Box::new(
self.map
.iter()
.filter_map(|(_, (def, _))| match def {
Definition::Interface(i) => Some(i.name.to_owned()),
_ => None,
})
.collect(),
)
}

pub fn type_is_enum(&self, type_name: &str) -> bool {
let def = self.map.get(type_name).map(|(def, _)| def);

Expand Down
24 changes: 19 additions & 5 deletions src/core/jit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl<'a> Builder<'a> {
#[inline(always)]
fn iter(
&self,
parent_fragment: Option<&str>,
selection: &SelectionSet,
type_condition: &str,
fragments: &HashMap<&str, &FragmentDefinition>,
Expand Down Expand Up @@ -168,6 +169,7 @@ impl<'a> Builder<'a> {
.map(|(k, v)| (k.node.as_str().to_string(), v.node.to_owned()))
.collect::<HashMap<_, _>>();

let parent_fragment = parent_fragment.map(|s| s.to_owned());
// Check if the field is present in the schema index
if let Some(field_def) = self.index.get_field(type_condition, field_name) {
let mut args = Vec::with_capacity(request_args.len());
Expand Down Expand Up @@ -198,8 +200,12 @@ impl<'a> Builder<'a> {
let id = FieldId::new(self.field_id.next());

// Recursively gather child fields for the selection set
let child_fields =
self.iter(&gql_field.selection_set.node, type_of.name(), fragments);
let child_fields = self.iter(
None,
&gql_field.selection_set.node,
type_of.name(),
fragments,
);

let ir = match field_def {
QueryField::Field((field_def, _)) => field_def.resolver.clone(),
Expand All @@ -220,6 +226,7 @@ impl<'a> Builder<'a> {
let field = Field {
id,
selection: child_fields,
parent_fragment,
name: field_name.to_string(),
output_name: gql_field
.alias
Expand Down Expand Up @@ -252,6 +259,7 @@ impl<'a> Builder<'a> {
args: Vec::new(),
pos: selection.pos.into(),
selection: vec![], // __typename has no child selection
parent_fragment,
directives,
is_enum: false,
scalar: Some(scalar::Scalar::Empty),
Expand All @@ -265,6 +273,7 @@ impl<'a> Builder<'a> {
fragments.get(fragment_spread.fragment_name.node.as_str())
{
fields.extend(self.iter(
Some(fragment.type_condition.node.on.node.as_str()),
&fragment.selection_set.node,
fragment.type_condition.node.on.node.as_str(),
fragments,
Expand All @@ -277,8 +286,12 @@ impl<'a> Builder<'a> {
.as_ref()
.map(|cond| cond.node.on.node.as_str())
.unwrap_or(type_condition);

fields.extend(self.iter(&fragment.selection_set.node, type_of, fragments));
fields.extend(self.iter(
Some(type_of),
&fragment.selection_set.node,
type_of,
fragments,
));
}
}
}
Expand Down Expand Up @@ -334,7 +347,7 @@ impl<'a> Builder<'a> {
let name = self
.get_type(operation.ty)
.ok_or(BuildError::RootOperationTypeNotDefined { operation: operation.ty })?;
let fields = self.iter(&operation.selection_set.node, name, &fragments);
let fields = self.iter(None, &operation.selection_set.node, name, &fragments);

let is_introspection_query = operation.selection_set.node.items.iter().any(|f| {
if let Selection::Field(Positioned { node: gql_field, .. }) = &f.node {
Expand All @@ -351,6 +364,7 @@ impl<'a> Builder<'a> {
operation.ty,
self.index.clone(),
is_introspection_query,
Some(*self.index.get_interfaces()),
);
Ok(plan)
}
Expand Down
8 changes: 7 additions & 1 deletion src/core/jit/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::fmt::{Debug, Display, Formatter};
use std::num::NonZeroU64;
use std::sync::Arc;
Expand Down Expand Up @@ -187,6 +187,7 @@ pub struct Field<Input> {
pub include: Option<Variable>,
pub args: Vec<Arg<Input>>,
pub selection: Vec<Field<Input>>,
pub parent_fragment: Option<String>,
pub pos: Pos,
pub directives: Vec<Directive<Input>>,
pub is_enum: bool,
Expand Down Expand Up @@ -245,6 +246,7 @@ impl<Input> Field<Input> {
.into_iter()
.map(|f| f.try_map(map))
.collect::<Result<Vec<Field<Output>>, Error>>()?,
parent_fragment: None,
skip: self.skip,
include: self.include,
pos: self.pos,
Expand Down Expand Up @@ -315,6 +317,7 @@ pub struct OperationPlan<Input> {
pub min_cache_ttl: Option<NonZeroU64>,
pub selection: Vec<Field<Input>>,
pub before: Option<IR>,
pub interfaces: Option<HashSet<String>>,
}

impl<Input> OperationPlan<Input> {
Expand All @@ -339,6 +342,7 @@ impl<Input> OperationPlan<Input> {
is_protected: self.is_protected,
min_cache_ttl: self.min_cache_ttl,
before: self.before,
interfaces: None,
})
}
}
Expand All @@ -351,6 +355,7 @@ impl<Input> OperationPlan<Input> {
operation_type: OperationType,
index: Arc<Index>,
is_introspection_query: bool,
interfaces: Option<HashSet<String>>,
) -> Self
where
Input: Clone,
Expand All @@ -366,6 +371,7 @@ impl<Input> OperationPlan<Input> {
is_protected: false,
min_cache_ttl: None,
before: Default::default(),
interfaces,
}
}

Expand Down
63 changes: 55 additions & 8 deletions src/core/jit/transform/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::convert::Infallible;
use std::fmt::{Debug, Display};
use std::marker::PhantomData;
Expand All @@ -20,18 +21,25 @@ impl<A> GraphQL<A> {
}
}

fn compute_selection_set<A: Display + Debug + JsonLikeOwned>(base_field: &mut [Field<A>]) {
fn compute_selection_set<A: Display + Debug + JsonLikeOwned>(
base_field: &mut [Field<A>],
interfaces: &HashSet<String>,
) {
for field in base_field.iter_mut() {
if let Some(ir) = field.ir.as_mut() {
ir.modify_io(&mut |io| {
if let IO::GraphQL { req_template, .. } = io {
if let Some(v) = format_selection_set(field.selection.iter()) {
if let Some(v) = format_selection_set(
field.selection.iter(),
interfaces,
interfaces.contains(field.type_of.name()),
) {
req_template.selection = Some(Mustache::parse(&v).into());
}
}
});
}
compute_selection_set(field.selection.as_mut());
compute_selection_set(field.selection.as_mut(), interfaces);
}
}

Expand All @@ -40,15 +48,23 @@ impl<A: Display + Debug + JsonLikeOwned + Clone> Transform for GraphQL<A> {
type Error = Infallible;

fn transform(&self, mut plan: Self::Value) -> Valid<Self::Value, Self::Error> {
compute_selection_set(&mut plan.selection);
let interfaces = match plan.interfaces {
Some(ref interfaces) => interfaces,
None => &HashSet::new(),
};
compute_selection_set(&mut plan.selection, interfaces);

Valid::succeed(plan)
}
}

fn format_selection_set<'a, A: 'a + Display + JsonLikeOwned>(
selection_set: impl Iterator<Item = &'a Field<A>>,
interfaces: &HashSet<String>,
is_parent_interface: bool,
) -> Option<String> {
let mut fragments_fields = HashMap::new();
let mut normal_fields = vec![];
let set = selection_set
.filter(|field| !matches!(&field.ir, Some(IR::IO(_)) | Some(IR::Dynamic(_))))
.map(|field| {
Expand All @@ -58,20 +74,51 @@ fn format_selection_set<'a, A: 'a + Display + JsonLikeOwned>(
} else {
field.name.to_string()
};
format_selection_field(field, &field_name)
let is_this_field_interface = interfaces.contains(field.type_of.name());
let formatted_selection_fields =
format_selection_field(field, &field_name, interfaces, is_this_field_interface);
match &field.parent_fragment {
Some(fragment) if is_parent_interface => {
fragments_fields
.entry(fragment.to_owned())
.or_insert_with(Vec::new)
.push(formatted_selection_fields);
}
_ => {
normal_fields.push(formatted_selection_fields);
}
}
})
.collect::<Vec<_>>();

if set.is_empty() {
return None;
}

Some(format!("{{ {} }}", set.join(" ")))
let string_set: Vec<String> = fragments_fields
.into_iter()
.map(|(fragment_name, fields)| {
format!("... on {} {{ {} }}", fragment_name, fields.join(" "))
})
.collect();

//Don't force user to query the type and get it automatically
if is_parent_interface {
normal_fields.push("__typename".to_owned());
}
normal_fields.push(string_set.join(" "));
Some(format!("{{ {} }}", normal_fields.join(" ")))
}

fn format_selection_field<A: Display + JsonLikeOwned>(field: &Field<A>, name: &str) -> String {
fn format_selection_field<A: Display + JsonLikeOwned>(
field: &Field<A>,
name: &str,
interfaces: &HashSet<String>,
is_parent_interface: bool,
) -> String {
let arguments = format_selection_field_arguments(field);
let selection_set = format_selection_set(field.selection.iter());
let selection_set =
format_selection_set(field.selection.iter(), interfaces, is_parent_interface);

let mut output = format!("{}{}", name, arguments);

Expand Down
1 change: 1 addition & 0 deletions src/core/jit/transform/input_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ where
is_const: self.plan.is_const,
is_protected: self.plan.is_protected,
min_cache_ttl: self.plan.min_cache_ttl,
interfaces: None,
selection,
before: self.plan.before,
})
Expand Down
27 changes: 27 additions & 0 deletions tests/core/snapshots/graphql-with-fragments.md_0.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/core/spec.rs
expression: response
snapshot_kind: text
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": {
"queryNodeC": [
{
"name": "nodeA",
"__typename": "NodeA",
"nodeA_id": "nodeA_id"
},
{
"name": "nodeB",
"__typename": "NodeB",
"nodeB_id": "nodeB_id"
}
]
}
}
}
28 changes: 28 additions & 0 deletions tests/core/snapshots/graphql-with-fragments.md_client.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: tests/core/spec.rs
expression: formatted
snapshot_kind: text
---
type NodeA implements NodeC {
name: String
nodeA_id: String
}

type NodeB implements NodeC {
name: String
nodeB_id: String
}

interface NodeC {
name: String
}

type Query {
queryNodeA: [NodeA!]
queryNodeB: [NodeB!]
queryNodeC: [NodeC!]
}

schema {
query: Query
}
28 changes: 28 additions & 0 deletions tests/core/snapshots/graphql-with-fragments.md_merged.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: tests/core/spec.rs
expression: formatter
snapshot_kind: text
---
schema @server @upstream @link(src: "schema_0.graphql", type: Config) {
query: Query
}

interface NodeC {
name: String
}

type NodeA implements NodeC {
name: String
nodeA_id: String
}

type NodeB implements NodeC {
name: String
nodeB_id: String
}

type Query {
queryNodeA: [NodeA!] @graphQL(url: "http://upstream/graphql", name: "nodeA")
queryNodeB: [NodeB!] @graphQL(url: "http://upstream/graphql", name: "nodeB")
queryNodeC: [NodeC!] @graphQL(url: "http://upstream/graphql", name: "nodeC")
}
Loading