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

feat(smith): add link directive to schema definition generation #800

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 45 additions & 0 deletions crates/apollo-smith/src/directive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
argument::{Argument, ArgumentsDef},
description::Description,
input_value::InputValueDef,
name::Name,
DocumentBuilder,
};
Expand Down Expand Up @@ -321,3 +322,47 @@ impl From<String> for DirectiveLocation {
}
}
}

pub(crate) fn at_link() -> DirectiveDef {
let mut directive_locations = IndexSet::new();
directive_locations.insert(DirectiveLocation::Schema);

DirectiveDef {
description: None,
name: Name::new("link".to_string()),
arguments_definition: Some(ArgumentsDef {
input_value_definitions: vec![
InputValueDef {
description: None,
name: Name::new("url".to_string()),
ty: crate::ty::Ty::Named(Name::new("String".to_string())),
default_value: None,
directives: Default::default(),
},
InputValueDef {
description: None,
name: Name::new("as".to_string()),
ty: crate::ty::Ty::Named(Name::new("String".to_string())),
default_value: None,
directives: Default::default(),
},
InputValueDef {
description: None,
name: Name::new("for".to_string()),
ty: crate::ty::Ty::Named(Name::new("link__Purpose".to_string())),
default_value: None,
directives: Default::default(),
},
InputValueDef {
description: None,
name: Name::new("import".to_string()),
ty: crate::ty::Ty::Named(Name::new("link__Import".to_string())),
default_value: None,
directives: Default::default(),
},
],
}),
repeatable: true,
directive_locations,
}
}
26 changes: 26 additions & 0 deletions crates/apollo-smith/src/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,29 @@ impl<'a> DocumentBuilder<'a> {
Ok(enum_values_def)
}
}

pub(crate) fn link_purpose() -> EnumTypeDef {
let mut enum_values_def = IndexSet::new();
enum_values_def.insert(EnumValueDefinition {
description: Some(Description::from(String::from(
"`SECURITY` features provide metadata necessary to securely resolve fields.",
))),
value: Name::new(String::from("SECURITY")),
directives: Default::default(),
});
enum_values_def.insert(EnumValueDefinition {
description: Some(Description::from(String::from(
"`EXECUTION` features provide metadata necessary for operation execution.",
))),
value: Name::new(String::from("EXECUTION")),
directives: Default::default(),
});

EnumTypeDef {
description: None,
name: Name::new(String::from("link__Purpose")),
directives: Default::default(),
enum_values_def,
extend: false,
}
}
1 change: 1 addition & 0 deletions crates/apollo-smith/src/input_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ mod tests {
stack: Vec::new(),
chosen_arguments: IndexMap::new(),
chosen_aliases: IndexMap::new(),
is_supergraph: false,
};
let my_nested_type = ObjectTypeDef {
description: None,
Expand Down
52 changes: 43 additions & 9 deletions crates/apollo-smith/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub struct DocumentBuilder<'a> {
pub(crate) chosen_arguments: IndexMap<Name, Vec<Argument>>,
// Useful to keep the same aliases for a specific field name
pub(crate) chosen_aliases: IndexMap<Name, Name>,
// Is this builder creating a supergraph
pub(crate) is_supergraph: bool,
}

impl<'a> Debug for DocumentBuilder<'a> {
Expand All @@ -114,7 +116,7 @@ impl<'a> Debug for DocumentBuilder<'a> {

impl<'a> DocumentBuilder<'a> {
/// Create an instance of `DocumentBuilder`
pub fn new(u: &'a mut Unstructured<'a>) -> Result<Self> {
pub fn new(u: &'a mut Unstructured<'a>, is_supergraph: bool) -> Result<Self> {
let mut builder = Self {
u,
object_type_defs: Vec::new(),
Expand All @@ -130,6 +132,7 @@ impl<'a> DocumentBuilder<'a> {
stack: Vec::new(),
chosen_arguments: IndexMap::new(),
chosen_aliases: IndexMap::new(),
is_supergraph,
};

for _ in 0..builder.u.int_in_range(1..=50)? {
Expand Down Expand Up @@ -162,24 +165,39 @@ impl<'a> DocumentBuilder<'a> {
builder.input_object_type_defs.push(input_object_type_def);
}

for _ in 0..builder.u.int_in_range(1..=50)? {
let fragment_def = builder.fragment_definition()?;
builder.fragment_defs.push(fragment_def);
if !builder.is_supergraph {
for _ in 0..builder.u.int_in_range(1..=50)? {
let fragment_def = builder.fragment_definition()?;
builder.fragment_defs.push(fragment_def);
}
}

for _ in 0..builder.u.int_in_range(1..=50)? {
let directive_def = builder.directive_def()?;
builder.directive_defs.push(directive_def);
}

if builder.is_supergraph {
let at_link = directive::at_link();
builder.directive_defs.push(at_link);

let link_import = scalar::link_import();
builder.scalar_type_defs.push(link_import);

let link_purpose = enum_::link_purpose();
builder.enum_type_defs.push(link_purpose);
}

let schema_def = builder.schema_definition()?;
builder.schema_def = Some(schema_def);

for _ in 0..builder.u.int_in_range(1..=50)? {
let operation_def = builder.operation_definition()?;
// Could be None if there is no schema definition (in this case it never happens)
if let Some(operation_def) = operation_def {
builder.operation_defs.push(operation_def);
if !builder.is_supergraph {
for _ in 0..builder.u.int_in_range(1..=50)? {
let operation_def = builder.operation_definition()?;
// Could be None if there is no schema definition (in this case it never happens)
if let Some(operation_def) = operation_def {
builder.operation_defs.push(operation_def);
}
}
}

Expand All @@ -204,6 +222,7 @@ impl<'a> DocumentBuilder<'a> {
stack: Vec::new(),
chosen_arguments: IndexMap::new(),
chosen_aliases: IndexMap::new(),
is_supergraph: false,
};

Ok(builder)
Expand Down Expand Up @@ -269,3 +288,18 @@ pub(crate) trait StackedEntity {
fn name(&self) -> &Name;
fn fields_def(&self) -> &[FieldDef];
}

#[cfg(test)]
mod tests {
use crate::DocumentBuilder;
use arbitrary::Unstructured;

#[test]
fn link_supergraph() {
let mut u = Unstructured::new(&[1, 2, 3]);
let gql_doc = DocumentBuilder::new(&mut u, true).unwrap();
let document = gql_doc.finish();
let document_str = String::from(document);
dbg!(document_str);
}
}
9 changes: 9 additions & 0 deletions crates/apollo-smith/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,12 @@ impl<'a> DocumentBuilder<'a> {
})
}
}

pub(crate) fn link_import() -> ScalarTypeDef {
ScalarTypeDef {
name: Name::new(String::from("link__Import")),
description: None,
directives: Default::default(),
extend: false,
}
}
15 changes: 14 additions & 1 deletion crates/apollo-smith/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{
argument::Argument,
description::Description,
directive::{Directive, DirectiveLocation},
input_value::InputValue,
name::Name,
ty::Ty,
DocumentBuilder,
Expand Down Expand Up @@ -132,7 +134,18 @@ impl<'a> DocumentBuilder<'a> {
.unwrap_or(false)
.then(|| self.description())
.transpose()?;
let directives = self.directives(DirectiveLocation::Schema)?;
let mut directives = self.directives(DirectiveLocation::Schema)?;
if self.is_supergraph {
let link_arg = Argument {
name: Name::new(String::from("url")),
value: InputValue::String("https://specs.apollo.dev/link/v1.0".into()),
};
let directive = Directive {
name: Name::new(String::from("link")),
arguments: vec![link_arg],
};
directives.insert(Name::new("link".to_string()), directive.into());
}
let named_types: Vec<Ty> = self
.list_existing_object_types()
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/apollo-smith/src/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use expect_test::expect;

fn gen(len: usize) -> String {
let entropy: Vec<u8> = (0..len).map(|i| i as u8).collect();
DocumentBuilder::new(&mut Unstructured::new(&entropy))
DocumentBuilder::new(&mut Unstructured::new(&entropy), false)
.unwrap()
.finish()
.into()
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn generate_valid_document(input: &[u8]) -> Result<String> {
drop(env_logger::try_init());

let mut u = Unstructured::new(input);
let gql_doc = DocumentBuilder::new(&mut u)?;
let gql_doc = DocumentBuilder::new(&mut u, false)?;
let document = gql_doc.finish();

Ok(document.into())
Expand Down