diff --git a/current/advanced/index.html b/current/advanced/index.html index 4a5eb4e4..aad31557 100644 --- a/current/advanced/index.html +++ b/current/advanced/index.html @@ -72,7 +72,7 @@
@@ -139,9 +139,10 @@

Advanced Topics

The chapters below cover some more advanced scenarios.

@@ -155,7 +156,7 @@ - @@ -173,7 +174,7 @@ - diff --git a/current/advanced/introspection.html b/current/advanced/introspection.html new file mode 100644 index 00000000..7a1421f3 --- /dev/null +++ b/current/advanced/introspection.html @@ -0,0 +1,263 @@ + + + + + + Introspection - Juniper - GraphQL Server for Rust + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

Introspection

+

GraphQL defines a special built-in top-level field called __schema. Querying +for this field allows one to introspect the schema +at runtime to see what queries and mutations the GraphQL server supports.

+

Because introspection queries are just regular GraphQL queries, Juniper supports +them natively. For example, to get all the names of the types supported one +could execute the following query against Juniper:

+
{
+  __schema {
+    types {
+      name
+    }
+  }
+}
+
+

Schema introspection output as JSON

+

Many client libraries and tools in the GraphQL ecosystem require a complete +representation of the server schema. Often this representation is in JSON and +referred to as schema.json. A complete representation of the schema can be +produced by issuing a specially crafted introspection query.

+

Juniper provides a convenience function to introspect the entire schema. The +result can then be converted to JSON for use with tools and libraries such as +graphql-client:

+
use juniper::{EmptyMutation, FieldResult, IntrospectionFormat};
+
+// Define our schema.
+
+#[derive(juniper::GraphQLObject)]
+struct Example {
+  id: String,
+}
+
+struct Context;
+impl juniper::Context for Context {}
+
+struct Query;
+
+#[juniper::object(
+  Context = Context,
+)]
+impl Query {
+   fn example(id: String) -> FieldResult<Example> {
+       unimplemented!()
+   }
+}
+
+type Schema = juniper::RootNode<'static, Query, EmptyMutation<Context>>;
+
+fn main() {
+    // Create a context object.
+    let ctx = Context{};
+
+    // Run the built-in introspection query.
+    let (res, _errors) = juniper::introspect(
+        &Schema::new(Query, EmptyMutation::new()),
+        &ctx,
+        IntrospectionFormat::default(),
+    ).unwrap();
+
+    // Convert introspection result to json.
+    let json_result = serde_json::to_string_pretty(&res);
+    assert!(json_result.is_ok());
+}
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/current/advanced/multiple_ops_per_request.html b/current/advanced/multiple_ops_per_request.html index 2e4f25fc..951c0734 100644 --- a/current/advanced/multiple_ops_per_request.html +++ b/current/advanced/multiple_ops_per_request.html @@ -72,7 +72,7 @@
diff --git a/current/advanced/non_struct_objects.html b/current/advanced/non_struct_objects.html index 94c70b1a..80fa8ba8 100644 --- a/current/advanced/non_struct_objects.html +++ b/current/advanced/non_struct_objects.html @@ -72,7 +72,7 @@
@@ -157,21 +157,22 @@ enum SignUpResult { Error(Vec<ValidationError>), } -juniper::graphql_object!(SignUpResult: () |&self| { - field user() -> Option<&User> { +#[juniper::object] +impl SignUpResult { + fn user(&self) -> Option<&User> { match *self { SignUpResult::Ok(ref user) => Some(user), SignUpResult::Error(_) => None, } } - field error() -> Option<&Vec<ValidationError>> { + fn error(&self) -> Option<&Vec<ValidationError>> { match *self { SignUpResult::Ok(_) => None, SignUpResult::Error(ref errors) => Some(errors) } } -}); +} # fn main() {} @@ -191,7 +192,7 @@ be used, and how to model expected errors.