Skip to content

Commit

Permalink
Use DefaultValue for default value of InputValueDefinition (#4636)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #4636

Reviewed By: ginfung

Differential Revision: D54436496

Pulled By: captbaritone

fbshipit-source-id: d4e27a8823e25b9f073d9bcac439c5b335836e82
  • Loading branch information
tobias-tengler authored and facebook-github-bot committed Mar 4, 2024
1 parent dfc6bfa commit 37c9604
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 138 deletions.
13 changes: 13 additions & 0 deletions compiler/crates/graphql-syntax/src/node/constant_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,16 @@ impl std::convert::From<i64> for FloatValue {
FloatValue::new(value as f64)
}
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct DefaultValue {
pub span: Span,
pub equals: Token,
pub value: ConstantValue,
}

impl fmt::Display for DefaultValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{}", self.value))
}
}
13 changes: 0 additions & 13 deletions compiler/crates/graphql-syntax/src/node/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,6 @@ impl fmt::Display for TypeCondition {
}
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct DefaultValue {
pub span: Span,
pub equals: Token,
pub value: ConstantValue,
}

impl fmt::Display for DefaultValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{}", self.value))
}
}

// Selections

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/crates/graphql-syntax/src/node/type_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use common::Span;
use intern::string_key::StringKey;

use super::constant_directive::ConstantDirective;
use super::constant_value::ConstantValue;
use super::constant_value::StringNode;
use super::executable::OperationKind;
use super::primitive::*;
use super::type_annotation::TypeAnnotation;
use crate::DefaultValue;
use crate::TokenKind;

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
Expand Down Expand Up @@ -515,7 +515,7 @@ impl fmt::Display for DirectiveLocation {
pub struct InputValueDefinition {
pub name: Identifier,
pub type_: TypeAnnotation,
pub default_value: Option<ConstantValue>,
pub default_value: Option<DefaultValue>,
pub directives: Vec<ConstantDirective>,
pub span: Span,
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/crates/graphql-syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,8 @@ impl<'a> Parser<'a> {
let name = self.parse_identifier()?;
self.parse_kind(TokenKind::Colon)?;
let type_ = self.parse_type_annotation()?;
let default_value = if self.parse_optional_kind(TokenKind::Equals).is_some() {
Some(self.parse_constant_value()?)
let default_value = if self.peek_token_kind() == TokenKind::Equals {
Some(self.parse_default_value()?)
} else {
None
};
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion compiler/crates/relay-docblock/src/docblock_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ fn validate_field_arguments(
if let Some(default_value) = &argument.default_value {
errors.push(Diagnostic::error(
IrParsingErrorMessages::ArgumentDefaultValuesNoSupported,
Location::new(source_location, default_value.span()),
Location::new(source_location, default_value.value.span()),
));
}
}
Expand Down
9 changes: 8 additions & 1 deletion compiler/crates/relay-docblock/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use graphql_syntax::BooleanNode;
use graphql_syntax::ConstantArgument;
use graphql_syntax::ConstantDirective;
use graphql_syntax::ConstantValue;
use graphql_syntax::DefaultValue;
use graphql_syntax::FieldDefinition;
use graphql_syntax::FieldDefinitionStub;
use graphql_syntax::Identifier;
Expand Down Expand Up @@ -733,7 +734,13 @@ trait ResolverTypeDefinitionIr: ResolverIr {
.map(|arg| InputValueDefinition {
name: arg.name.clone(),
type_: arg.type_.clone(),
default_value: arg.default_value.clone(),
default_value: arg.default_value.as_ref().map(|default_value| {
DefaultValue {
value: default_value.clone(),
equals: dummy_token(span),
span,
}
}),
directives: vec![],
span,
})
Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/resolution-path/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,8 @@ impl<'a> ResolvePosition<'a> for List<ConstantArgument> {
}
}

pub type ConstantArgPath<'a> = Path<&'a ConstantArgument, ConstantObjPath<'a>>;
pub type ConstantArgPath<'a> = Path<&'a ConstantArgument, ConstantArgParent<'a>>;
pub type ConstantArgParent<'a> = ConstantObjPath<'a>;

impl<'a> ResolvePosition<'a> for ConstantArgument {
type Parent = ConstantObjPath<'a>;
Expand Down
5 changes: 4 additions & 1 deletion compiler/crates/schema/src/in_memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,10 @@ impl InMemorySchema {
Ok(Argument {
name: ArgumentName(arg_def.name.value),
type_: self.build_input_object_reference(&arg_def.type_)?,
default_value: arg_def.default_value.clone(),
default_value: arg_def
.default_value
.as_ref()
.map(|default_value| default_value.value.clone()),
description: None,
directives: self.build_directive_values(&arg_def.directives),
})
Expand Down

0 comments on commit 37c9604

Please sign in to comment.