Skip to content

Commit ef45fd0

Browse files
authored
Add serialization support for everything (#728)
`Schema`, `ExecutableDocument`, and all AST types already supported serialization to GraphQL syntax through the `Display` trait and the `.serialize()` method. This is now also the case of all other Rust types representing some element of a GraphQL document: * `schema::Directives` * `schema::ExtendedType` * `schema::ScalarType` * `schema::ObjectType` * `schema::InterfaceType` * `schema::EnumType` * `schema::UnionType` * `schema::InputObjectType` * `executable::Operation` * `executable::Fragment` * `executable::SelectionSet` * `executable::Selection` * `executable::Field` * `executable::InlineFragment` * `executable::FragmentSpread` * `executable::FieldSet`
1 parent 7efc372 commit ef45fd0

File tree

5 files changed

+359
-168
lines changed

5 files changed

+359
-168
lines changed

crates/apollo-compiler/CHANGELOG.md

+48-18
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1616
1717
## Maintenance
1818
## Documentation-->
19-
# [1.0.0-beta.5](https://crates.io/crates/apollo-compiler/1.0.0-beta.5) - 2023-11-08
20-
21-
## Features
22-
- Diangostic struct is now public by [SimonSapin] in [11fe454]
23-
- Improve lowercase enum value diagnostic by [goto-bus-stop] in [pull/725]
24-
25-
## Maintenance
26-
- Simplify `SchemaBuilder` internals by [SimonSapin] in [pull/722]
27-
- Remove validation dead code by [SimonSapin] in [bd5d107]
28-
- Skip schema AST conversion in ExecutableDocument::validate by [SimonSapin] in [pull/726]
29-
30-
[SimonSapin]: https://github.com/SimonSapin
31-
[goto-bus-stop]: https://github.com/goto-bus-stop
32-
[11fe454]: https://github.com/apollographql/apollo-rs/commit/11fe454f81b4cfbada4884a22575fa5c812a6ed4
33-
[bd5d107]: https://github.com/apollographql/apollo-rs/commit/bd5d107eca14a7fc06dd885b2952346326e648cb
34-
[pull/722]: https://github.com/apollographql/apollo-rs/pull/722
35-
[pull/725]: https://github.com/apollographql/apollo-rs/pull/725
36-
[pull/726]: https://github.com/apollographql/apollo-rs/pull/726
3719

3820
# [x.x.x] (unreleased) - 2023-mm-dd
3921

@@ -56,9 +38,57 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
5638
(which was equivalent to `(Option<&Name>, &Node<Operation>)`),
5739
replacing its uses with `&Node<Operation>`
5840

41+
## Features
42+
43+
- **Add serialization support for everything - [SimonSapin], [pull/728].**
44+
45+
`Schema`, `ExecutableDocument`, and all AST types
46+
already supported serialization to GraphQL syntax
47+
through the `Display` trait and the `.serialize()` method.
48+
This is now also the case of all other Rust types
49+
representing some element of a GraphQL document:
50+
* `schema::Directives`
51+
* `schema::ExtendedType`
52+
* `schema::ScalarType`
53+
* `schema::ObjectType`
54+
* `schema::InterfaceType`
55+
* `schema::EnumType`
56+
* `schema::UnionType`
57+
* `schema::InputObjectType`
58+
* `executable::Operation`
59+
* `executable::Fragment`
60+
* `executable::SelectionSet`
61+
* `executable::Selection`
62+
* `executable::Field`
63+
* `executable::InlineFragment`
64+
* `executable::FragmentSpread`
65+
* `executable::FieldSet`
66+
5967
[SimonSapin]: https://github.com/SimonSapin
6068
[issue/708]: https://github.com/apollographql/apollo-rs/issues/708
6169
[pull/727]: https://github.com/apollographql/apollo-rs/pull/727
70+
[pull/728]: https://github.com/apollographql/apollo-rs/pull/728
71+
72+
73+
# [1.0.0-beta.5](https://crates.io/crates/apollo-compiler/1.0.0-beta.5) - 2023-11-08
74+
75+
## Features
76+
- Diangostic struct is now public by [SimonSapin] in [11fe454]
77+
- Improve lowercase enum value diagnostic by [goto-bus-stop] in [pull/725]
78+
79+
## Maintenance
80+
- Simplify `SchemaBuilder` internals by [SimonSapin] in [pull/722]
81+
- Remove validation dead code by [SimonSapin] in [bd5d107]
82+
- Skip schema AST conversion in ExecutableDocument::validate by [SimonSapin] in [pull/726]
83+
84+
[SimonSapin]: https://github.com/SimonSapin
85+
[goto-bus-stop]: https://github.com/goto-bus-stop
86+
[11fe454]: https://github.com/apollographql/apollo-rs/commit/11fe454f81b4cfbada4884a22575fa5c812a6ed4
87+
[bd5d107]: https://github.com/apollographql/apollo-rs/commit/bd5d107eca14a7fc06dd885b2952346326e648cb
88+
[pull/722]: https://github.com/apollographql/apollo-rs/pull/722
89+
[pull/725]: https://github.com/apollographql/apollo-rs/pull/725
90+
[pull/726]: https://github.com/apollographql/apollo-rs/pull/726
91+
6292

6393
# [1.0.0-beta.4](https://crates.io/crates/apollo-compiler/1.0.0-beta.4) - 2023-10-16
6494

crates/apollo-compiler/src/ast/serialize.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use super::*;
2+
use crate::executable;
3+
use crate::schema;
24
use std::fmt;
35
use std::fmt::Display;
46

@@ -546,7 +548,7 @@ impl Directives {
546548
}
547549

548550
impl Directive {
549-
fn serialize_impl(&self, state: &mut State) -> fmt::Result {
551+
pub(crate) fn serialize_impl(&self, state: &mut State) -> fmt::Result {
550552
let Self { name, arguments } = self;
551553
state.write("@")?;
552554
state.write(name)?;
@@ -627,7 +629,7 @@ impl EnumValueDefinition {
627629
}
628630

629631
impl Selection {
630-
fn serialize_impl(&self, state: &mut State) -> fmt::Result {
632+
pub(crate) fn serialize_impl(&self, state: &mut State) -> fmt::Result {
631633
match self {
632634
Selection::Field(x) => x.serialize_impl(state),
633635
Selection::FragmentSpread(x) => x.serialize_impl(state),
@@ -637,7 +639,7 @@ impl Selection {
637639
}
638640

639641
impl Field {
640-
fn serialize_impl(&self, state: &mut State) -> fmt::Result {
642+
pub(crate) fn serialize_impl(&self, state: &mut State) -> fmt::Result {
641643
let Self {
642644
alias,
643645
name,
@@ -663,7 +665,7 @@ impl Field {
663665
}
664666

665667
impl FragmentSpread {
666-
fn serialize_impl(&self, state: &mut State) -> fmt::Result {
668+
pub(crate) fn serialize_impl(&self, state: &mut State) -> fmt::Result {
667669
let Self {
668670
fragment_name,
669671
directives,
@@ -675,7 +677,7 @@ impl FragmentSpread {
675677
}
676678

677679
impl InlineFragment {
678-
fn serialize_impl(&self, state: &mut State) -> fmt::Result {
680+
pub(crate) fn serialize_impl(&self, state: &mut State) -> fmt::Result {
679681
let Self {
680682
type_condition,
681683
directives,
@@ -780,7 +782,7 @@ fn comma_separated<T>(
780782
/// c
781783
/// }
782784
/// ```
783-
fn curly_brackets_space_separated<T>(
785+
pub(crate) fn curly_brackets_space_separated<T>(
784786
state: &mut State,
785787
values: &[T],
786788
serialize_one: impl Fn(&mut State, &T) -> fmt::Result,
@@ -918,4 +920,20 @@ impl_display! {
918920
Value
919921
crate::Schema
920922
crate::ExecutableDocument
923+
schema::Directives
924+
schema::ExtendedType
925+
schema::ScalarType
926+
schema::ObjectType
927+
schema::InterfaceType
928+
schema::EnumType
929+
schema::UnionType
930+
schema::InputObjectType
931+
executable::Operation
932+
executable::Fragment
933+
executable::SelectionSet
934+
executable::Selection
935+
executable::Field
936+
executable::InlineFragment
937+
executable::FragmentSpread
938+
executable::FieldSet
921939
}

crates/apollo-compiler/src/executable/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,16 @@ impl Operation {
362362
self.operation_type == OperationType::Query
363363
&& is_introspection_impl(document, &mut HashSet::new(), &self.selection_set)
364364
}
365+
366+
serialize_method!();
365367
}
366368

367369
impl Fragment {
368370
pub fn type_condition(&self) -> &NamedType {
369371
&self.selection_set.ty
370372
}
373+
374+
serialize_method!();
371375
}
372376

373377
impl SelectionSet {
@@ -425,6 +429,8 @@ impl SelectionSet {
425429
pub fn fields(&self) -> impl Iterator<Item = &Node<Field>> {
426430
self.selections.iter().filter_map(|sel| sel.as_field())
427431
}
432+
433+
serialize_method!();
428434
}
429435

430436
impl Selection {
@@ -435,6 +441,8 @@ impl Selection {
435441
Self::InlineFragment(sel) => &sel.directives,
436442
}
437443
}
444+
445+
serialize_method!();
438446
}
439447

440448
impl From<Node<Field>> for Selection {
@@ -577,6 +585,8 @@ impl Field {
577585
pub fn inner_type_def<'a>(&self, schema: &'a Schema) -> Option<&'a schema::ExtendedType> {
578586
schema.types.get(self.ty().inner_named_type())
579587
}
588+
589+
serialize_method!();
580590
}
581591

582592
impl InlineFragment {
@@ -623,6 +633,8 @@ impl InlineFragment {
623633
self.selection_set.extend(selections);
624634
self
625635
}
636+
637+
serialize_method!();
626638
}
627639

628640
impl FragmentSpread {
@@ -649,6 +661,8 @@ impl FragmentSpread {
649661
pub fn fragment_def<'a>(&self, document: &'a ExecutableDocument) -> Option<&'a Node<Fragment>> {
650662
document.fragments.get(&self.fragment_name)
651663
}
664+
665+
serialize_method!();
652666
}
653667

654668
impl FieldSet {
@@ -672,6 +686,8 @@ impl FieldSet {
672686
validation::validate_field_set(&mut errors, schema, self);
673687
errors.into_result()
674688
}
689+
690+
serialize_method!();
675691
}
676692

677693
impl fmt::Display for SelectionPath {

0 commit comments

Comments
 (0)