Skip to content

Commit 5b02ab7

Browse files
committed
WIP: implement input object type
1 parent 137d851 commit 5b02ab7

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/schema/format.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,29 @@ impl Displayable for EnumTypeExtension {
314314
}
315315
}
316316

317+
fn format_inputs(fields: &[InputValue], f: &mut Formatter) {
318+
if !fields.is_empty() {
319+
f.write(" ");
320+
f.start_block();
321+
for fld in fields {
322+
f.indent();
323+
fld.display(f);
324+
f.endline();
325+
}
326+
f.end_block();
327+
} else {
328+
f.endline();
329+
}
330+
}
331+
317332
impl Displayable for InputObjectType {
318333
fn display(&self, f: &mut Formatter) {
319-
unimplemented!();
334+
description(&self.description, f);
335+
f.indent();
336+
f.write("input ");
337+
f.write(&self.name);
338+
format_directives(&self.directives, f);
339+
format_inputs(&self.fields, f);
320340
}
321341
}
322342

src/schema/grammar.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,33 @@ pub fn enum_type_extension<'a>(input: &mut TokenStream<'a>)
381381
.parse_stream(input)
382382
}
383383

384+
pub fn input_fields<'a>(input: &mut TokenStream<'a>)
385+
-> ParseResult<Vec<InputValue>, TokenStream<'a>>
386+
{
387+
optional(punct("{").with(many1(parser(input_value))).skip(punct("}")))
388+
.map(|v| v.unwrap_or_else(Vec::new))
389+
.parse_stream(input)
390+
}
391+
392+
pub fn input_object_type<'a>(input: &mut TokenStream<'a>)
393+
-> ParseResult<InputObjectType, TokenStream<'a>>
394+
{
395+
(
396+
position(),
397+
ident("input").with(name()),
398+
parser(implements_interfaces),
399+
parser(directives),
400+
parser(input_fields),
401+
)
402+
.map(|(position, name, interfaces, directives, fields)| {
403+
InputObjectType {
404+
position, name, directives, fields,
405+
description: None, // is filled in type_definition
406+
}
407+
})
408+
.parse_stream(input)
409+
}
410+
384411
pub fn type_definition<'a>(input: &mut TokenStream<'a>)
385412
-> ParseResult<TypeDefinition, TokenStream<'a>>
386413
{
@@ -392,6 +419,7 @@ pub fn type_definition<'a>(input: &mut TokenStream<'a>)
392419
parser(interface_type).map(TypeDefinition::Interface),
393420
parser(union_type).map(TypeDefinition::Union),
394421
parser(enum_type).map(TypeDefinition::Enum),
422+
parser(input_object_type).map(TypeDefinition::InputObject),
395423
)),
396424
)
397425
// We can't set description inside type definition parser, because

tests/schema_roundtrips.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ fn roundtrip2(filename: &str) {
4444
#[test] fn union_extension() { roundtrip("union_extension"); }
4545
#[test] fn enum_type() { roundtrip("enum"); }
4646
#[test] fn extend_enum() { roundtrip("extend_enum"); }
47+
#[test] fn input_type() { roundtrip("input_type"); }
4748
// Not yet fully supported
4849
//#[test] fn kitchen_sink() { roundtrip2("kitchen-sink"); }

tests/schemas/input_type.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
input InputType {
2+
key: String!
3+
answer: Int = 42
4+
}

0 commit comments

Comments
 (0)