forked from graphql-rust/juniper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the canonical introspection schema
Fixes graphql-rust#307. Add support for introspection
- Loading branch information
Showing
13 changed files
with
2,825 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Introspection | ||
|
||
GraphQL defines a special built-in top-level field called `__schema`. Querying | ||
for this field allows one to [inspect the schema](https://graphql.org/learn/introspection/) | ||
at runtime to see what queries and mutations the GraphQL server supports. | ||
|
||
Because inspection 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: | ||
|
||
```graphql | ||
{ | ||
__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 inspect the entire schema. The | ||
result can then be converted to JSON for use with tools and libraries such as | ||
[graphql-client](https://github.com/graphql-rust/graphql-client): | ||
|
||
```rust | ||
# // Only needed due to 2018 edition because the macro is not accessible. | ||
# extern crate juniper; | ||
# extern crate serde_json; | ||
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::graphql_object!(Query: Context |&self| { | ||
field example(&executor, 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()); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
extern crate skeptic; | ||
|
||
fn main() { | ||
let files = skeptic::markdown_files_of_directory("../content/types"); | ||
let files = skeptic::markdown_files_of_directory("../content/"); | ||
skeptic::generate_doc_tests(&files); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/// From <https://github.com/graphql/graphql-js/blob/8c96dc8276f2de27b8af9ffbd71a4597d483523f/src/utilities/introspectionQuery.js#L21>s | ||
pub(crate) const INTROSPECTION_QUERY: &str = include_str!("./query.graphql"); | ||
pub(crate) const INTROSPECTION_QUERY_WITHOUT_DESCRIPTIONS: &str = | ||
include_str!("./query_without_descriptions.graphql"); | ||
|
||
/// The desired GraphQL introspection format for the canonical query | ||
/// (<https://github.com/graphql/graphql-js/blob/8c96dc8276f2de27b8af9ffbd71a4597d483523f/src/utilities/introspectionQuery.js#L21>) | ||
pub enum IntrospectionFormat { | ||
/// The canonical GraphQL introspection query. | ||
All, | ||
/// The canonical GraphQL introspection query without descriptions. | ||
WithoutDescriptions, | ||
} | ||
|
||
impl Default for IntrospectionFormat { | ||
fn default() -> Self { | ||
IntrospectionFormat::All | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
query IntrospectionQuery { | ||
__schema { | ||
queryType { | ||
name | ||
} | ||
mutationType { | ||
name | ||
} | ||
subscriptionType { | ||
name | ||
} | ||
types { | ||
...FullType | ||
} | ||
directives { | ||
name | ||
description | ||
locations | ||
args { | ||
...InputValue | ||
} | ||
} | ||
} | ||
} | ||
fragment FullType on __Type { | ||
kind | ||
name | ||
description | ||
fields(includeDeprecated: true) { | ||
name | ||
description | ||
args { | ||
...InputValue | ||
} | ||
type { | ||
...TypeRef | ||
} | ||
isDeprecated | ||
deprecationReason | ||
} | ||
inputFields { | ||
...InputValue | ||
} | ||
interfaces { | ||
...TypeRef | ||
} | ||
enumValues(includeDeprecated: true) { | ||
name | ||
description | ||
isDeprecated | ||
deprecationReason | ||
} | ||
possibleTypes { | ||
...TypeRef | ||
} | ||
} | ||
fragment InputValue on __InputValue { | ||
name | ||
description | ||
type { | ||
...TypeRef | ||
} | ||
defaultValue | ||
} | ||
fragment TypeRef on __Type { | ||
kind | ||
name | ||
ofType { | ||
kind | ||
name | ||
ofType { | ||
kind | ||
name | ||
ofType { | ||
kind | ||
name | ||
ofType { | ||
kind | ||
name | ||
ofType { | ||
kind | ||
name | ||
ofType { | ||
kind | ||
name | ||
ofType { | ||
kind | ||
name | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.