apollographql / apollo-rs

Spec compliant GraphQL Tools in Rust.
Apache License 2.0
576 stars 45 forks source link

API refactor to make it harder to ignore errors #752

Closed SimonSapin closed 11 months ago

SimonSapin commented 11 months ago

Fixes https://github.com/apollographql/apollo-rs/issues/709

BREAKING

Highlight of signature changes:

+struct Valid<T>(T); // Implements `Deref` and `AsRef` but not `DerefMut` or `AsMut`
+
+struct WithErrors<T> {
+    partial: T, // Errors may cause components to be missing
+    errors: DiagnosticList,
+}

-pub fn parse_mixed(…) -> (Schema, ExecutableDocument)
+pub fn parse_mixed_validate(…)
+    -> Result<(Valid<Schema>, Valid<ExecutableDocument>), DiagnosticList>

  impl ast::Document {
-    pub fn parse(…) -> Self
+    pub fn parse(…) -> Result<Self, WithErrors<Self>>

-    pub fn to_schema(&self) -> Schema
+    pub fn to_schema(&self) -> Result<Schema, WithErrors<Schema>>

-    pub fn to_executable(&self) -> ExecutableDocument
+    pub fn to_executable(&self) -> Result<ExecutableDocument, WithErrors<ExecutableDocument>>

-    pub fn to_mixed(&self) -> (Schema, ExecutableDocument)
+    pub fn to_mixed_validate(
+        &self,
+    ) -> Result<(Valid<Schema>, Valid<ExecutableDocument>), DiagnosticList>
  }

  impl Schema {
-    pub fn parse(…) -> Self
-    pub fn validate(&self) -> Result<DiagnosticList, DiagnosticList>

+    pub fn parse_and_validate(…) -> Result<Valid<Self>, WithErrors<Self>>
+    pub fn parse(…) -> Result<Self, WithErrors<Self>>
+    pub fn validate(self) -> Result<Valid<Self>, WithErrors<Self>>
  }

  impl SchemaBuilder {
-    pub fn build(self) -> Schema
+    pub fn build(self) -> Result<Schema, WithErrors<Schema>>
  }

  impl ExecutableDocument {
-    pub fn parse(schema: &Schema, …) -> Self
-    pub fn validate(&self, schema: &Schema) -> Result<(), DiagnosticList>

+    pub fn parse_and_validate(schema: &Valid<Schema>, …) -> Result<Valid<Self>, WithErrors<Self>>
+    pub fn parse(schema: &Valid<Schema>, …) -> Result<Self, WithErrors<Self>>
+    pub fn validate(self, schema: &Valid<Schema>) -> Result<Valid<Self>, WithErrors<Self>>
  }

Features

SimonSapin commented 11 months ago

~TODO:~ add this escape hatch:

impl<T> Valid<T> {
    pub fn assert_valid(value: T) -> Self {
        Self(value)
    }
}