Skip to content

Commit d7ca3bc

Browse files
committed
fix: default argument
1 parent 4a9c688 commit d7ca3bc

File tree

6 files changed

+64
-17
lines changed

6 files changed

+64
-17
lines changed

graphqxl_parser/src/ast_arguments.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ use crate::utils::{unknown_rule_error, OwnedSpan};
66
use crate::{parse_directive, parse_value_type, Directive, ValueType};
77
use pest::iterators::Pair;
88

9+
#[derive(Debug, Clone, PartialEq)]
10+
pub enum ArgumentDefaultValue {
11+
None,
12+
ValueData(ValueData),
13+
Identifier(Identifier),
14+
}
15+
916
#[derive(Debug, Clone, PartialEq)]
1017
pub struct Argument {
1118
pub span: OwnedSpan,
1219
pub name: Identifier,
1320
pub description: String,
1421
pub value_type: ValueType,
15-
pub default: Option<ValueData>,
22+
pub default: ArgumentDefaultValue,
1623
pub directives: Vec<Directive>,
1724
}
1825

@@ -23,7 +30,7 @@ impl Argument {
2330
name: Identifier::from(name),
2431
description: "".to_string(),
2532
value_type: t,
26-
default: None,
33+
default: ArgumentDefaultValue::None,
2734
directives: Vec::new(),
2835
}
2936
}
@@ -53,8 +60,8 @@ impl Argument {
5360
self.clone()
5461
}
5562

56-
pub fn default(&mut self, value_data: ValueData) -> Self {
57-
self.default = Some(value_data);
63+
pub fn default(&mut self, default: ArgumentDefaultValue) -> Self {
64+
self.default = default;
5865
self.clone()
5966
}
6067

@@ -75,11 +82,13 @@ fn parse_argument(pair: Pair<Rule>, file: &str) -> Result<Argument, Box<RuleErro
7582
// at this moment we are on [identifier, value]
7683
let name = parse_identifier(next.unwrap(), file)?;
7784
let value = parse_value_type(childs.next().unwrap(), file)?;
78-
let mut default = None;
85+
let mut default = ArgumentDefaultValue::None;
7986
let mut directives = Vec::new();
8087
if let Some(pair) = childs.next() {
8188
if let Rule::value_data = pair.as_rule() {
82-
default = Some(parse_value_data(pair, file)?)
89+
default = ArgumentDefaultValue::ValueData(parse_value_data(pair, file)?)
90+
} else if let Rule::identifier = pair.as_rule() {
91+
default = ArgumentDefaultValue::Identifier(parse_identifier(pair, file)?)
8392
}
8493
for directive in childs {
8594
directives.push(parse_directive(directive, file)?);
@@ -154,9 +163,19 @@ mod tests {
154163
fn test_default_value_for_argument_works() {
155164
assert_eq!(
156165
parse_input("(arg: String = \"default\")"),
157-
Ok(vec![
158-
Argument::string("arg").default(ValueData::string("default"))
159-
])
166+
Ok(vec![Argument::string("arg").default(
167+
ArgumentDefaultValue::ValueData(ValueData::string("default"))
168+
)])
169+
);
170+
}
171+
172+
#[test]
173+
fn test_default_value_as_identifier_for_argument_works() {
174+
assert_eq!(
175+
parse_input("(arg: String = Foo)"),
176+
Ok(vec![Argument::string("arg").default(
177+
ArgumentDefaultValue::Identifier(Identifier::from("Foo"))
178+
)])
160179
);
161180
}
162181

@@ -165,7 +184,9 @@ mod tests {
165184
assert_eq!(
166185
parse_input("(arg: String = \"default\" @dir1 @dir2)"),
167186
Ok(vec![Argument::string("arg")
168-
.default(ValueData::string("default"))
187+
.default(ArgumentDefaultValue::ValueData(ValueData::string(
188+
"default"
189+
)))
169190
.directive(Directive::build("dir1"))
170191
.directive(Directive::build("dir2"))])
171192
);

graphqxl_parser/src/ast_block_field.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ mod tests {
120120
use super::*;
121121
use crate::ast_value_data::ValueData;
122122
use crate::utils::parse_full_input;
123+
use crate::ArgumentDefaultValue;
123124

124125
fn parse_with_args_input(input: &str) -> Result<BlockField, Box<RuleError>> {
125126
parse_full_input(input, Rule::field_with_args, parse_block_field)
@@ -198,8 +199,9 @@ mod tests {
198199
Ok(BlockField::build("field")
199200
.string()
200201
.arg(
201-
Argument::build("arg1", ValueType::string().array().non_nullable())
202-
.default(ValueData::string("default").list())
202+
Argument::build("arg1", ValueType::string().array().non_nullable()).default(
203+
ArgumentDefaultValue::ValueData(ValueData::string("default").list())
204+
)
203205
)
204206
.arg(Argument::build("arg2", ValueType::float().non_nullable())))
205207
);

graphqxl_parser/src/grammar.pest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ field_without_args = { description? ~ identifier ~ ":" ~ value_type ~ directive*
7777
field_without_args_without_value = { description? ~ identifier ~ directive* }
7878

7979
arguments = { "(" ~ argument* ~ ")" }
80-
argument = { description? ~ identifier ~ ":" ~ value_type ~ ("=" ~ value_data)? ~ directive* }
80+
argument = { description? ~ identifier ~ ":" ~ value_type ~ ("=" ~ (value_data | identifier))? ~ directive* }
8181

8282
function_call = { "(" ~ function_input+ ~ ")" }
8383
function_input = { identifier ~ ":" ~ value_data }

graphqxl_synthesizer/src/synth_arguments.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::synth_directive::DirectiveSynth;
2+
use crate::synth_identifier::IdentifierSynth;
23
use crate::synth_value_data::ValueDataSynth;
34
use crate::synth_value_type::ValueTypeSynth;
45
use crate::synths::{
56
ChainSynth, MultilineListSynth, OneLineListSynth, StringSynth, Synth, SynthContext,
67
};
7-
use graphqxl_parser::Argument;
8+
use graphqxl_parser::{Argument, ArgumentDefaultValue};
89

910
pub(crate) struct ArgumentsSynth(pub(crate) Vec<Argument>);
1011

@@ -18,10 +19,14 @@ impl Synth for ArgumentsSynth {
1819
Box::new(StringSynth(argument.name.id.clone() + ": ")),
1920
Box::new(ValueTypeSynth(argument.value_type.clone())),
2021
];
21-
if let Some(default) = &argument.default {
22+
if let ArgumentDefaultValue::ValueData(default) = &argument.default {
2223
v.push(Box::new(StringSynth::from(" = ")));
2324
v.push(Box::new(ValueDataSynth(default.clone())));
25+
} else if let ArgumentDefaultValue::Identifier(default) = &argument.default {
26+
v.push(Box::new(StringSynth::from(" = ")));
27+
v.push(Box::new(IdentifierSynth(default.clone())));
2428
}
29+
2530
for directive in argument.directives.iter() {
2631
v.push(Box::new(StringSynth::from(" ")));
2732
v.push(Box::new(DirectiveSynth(directive.clone())));
@@ -68,7 +73,9 @@ mod tests {
6873

6974
#[test]
7075
fn test_with_default_value() {
71-
let synth = ArgumentsSynth(vec![Argument::int("arg").default(ValueData::int(1).list())]);
76+
let synth =
77+
ArgumentsSynth(vec![Argument::int("arg")
78+
.default(ArgumentDefaultValue::ValueData(ValueData::int(1).list()))]);
7279
assert_eq!(synth.synth_zero(), "(arg: Int = [ 1 ])")
7380
}
7481

@@ -81,7 +88,7 @@ mod tests {
8188
#[test]
8289
fn test_with_default_value_with_directives() {
8390
let synth = ArgumentsSynth(vec![Argument::int("arg")
84-
.default(ValueData::int(1).list())
91+
.default(ArgumentDefaultValue::ValueData(ValueData::int(1).list()))
8592
.directive(Directive::build("dir"))]);
8693
assert_eq!(synth.synth_zero(), "(arg: Int = [ 1 ] @dir)")
8794
}

src/test/default-arg.graphqxl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
enum Enum {
2+
One
3+
Other
4+
}
5+
6+
type Query {
7+
foo(bar: Enum! = One): Boolean
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
enum Enum {
2+
One
3+
Other
4+
}
5+
6+
type Query {
7+
foo(bar: Enum! = One): Boolean
8+
}
9+

0 commit comments

Comments
 (0)