Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support introspection object types #1240

Merged
merged 6 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Description! And a link to a [reference](http://url)
By [@USERNAME](https://github.com/USERNAME) in https://github.com/apollographql/router/pull/PULL_NUMBER
-->


# [0.9.5] (unreleased) - 2022-mm-dd
## ❗ BREAKING ❗
## 🚀 Features ( :rocket: )
Expand All @@ -40,6 +39,14 @@ export APOLLO_UPLINK_ENDPOINTS="https://aws.uplink.api.apollographql.com/, https
By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/872

## 🐛 Fixes ( :bug: )

### Support introspection object types ([PR #1240](https://github.com/apollographql/router/pull/1240))

Introspection queries can use a set of object types defined in the specification. The query parsing code was not recognizing them,
resulting in some introspection queries not working.

By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/1240

## 🛠 Maintenance ( :hammer_and_wrench: )
## 📚 Documentation ( :books: )
## 🐛 Fixes ( :bug: )
## 🐛 Fixes ( :bug: )
94 changes: 94 additions & 0 deletions apollo-router/src/spec/introspection_types.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#source: https://spec.graphql.org/October2021/#sec-Schema-Introspection.Schema-Introspection-Schema

type __Schema {
description: String
types: [__Type!]!
queryType: __Type!
mutationType: __Type
subscriptionType: __Type
directives: [__Directive!]!
}

type __Type {
kind: __TypeKind!
name: String
description: String
# must be non-null for OBJECT and INTERFACE, otherwise null.
fields(includeDeprecated: Boolean = false): [__Field!]
# must be non-null for OBJECT and INTERFACE, otherwise null.
interfaces: [__Type!]
# must be non-null for INTERFACE and UNION, otherwise null.
possibleTypes: [__Type!]
# must be non-null for ENUM, otherwise null.
enumValues(includeDeprecated: Boolean = false): [__EnumValue!]
# must be non-null for INPUT_OBJECT, otherwise null.
inputFields: [__InputValue!]
# must be non-null for NON_NULL and LIST, otherwise null.
ofType: __Type
# may be non-null for custom SCALAR, otherwise null.
specifiedByURL: String
}

enum __TypeKind {
SCALAR
OBJECT
INTERFACE
UNION
ENUM
INPUT_OBJECT
LIST
NON_NULL
}

type __Field {
name: String!
description: String
args: [__InputValue!]!
type: __Type!
isDeprecated: Boolean!
deprecationReason: String
}

type __InputValue {
name: String!
description: String
type: __Type!
defaultValue: String
}

type __EnumValue {
name: String!
description: String
isDeprecated: Boolean!
deprecationReason: String
}

type __Directive {
name: String!
description: String
locations: [__DirectiveLocation!]!
args: [__InputValue!]!
isRepeatable: Boolean!
}

enum __DirectiveLocation {
QUERY
MUTATION
SUBSCRIPTION
FIELD
FRAGMENT_DEFINITION
FRAGMENT_SPREAD
INLINE_FRAGMENT
VARIABLE_DEFINITION
SCHEMA
SCALAR
OBJECT
FIELD_DEFINITION
ARGUMENT_DEFINITION
INTERFACE
UNION
ENUM
ENUM_VALUE
INPUT_OBJECT
INPUT_FIELD_DEFINITION
}
42 changes: 42 additions & 0 deletions apollo-router/src/spec/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4568,4 +4568,46 @@ mod tests {
}},
);
}

#[test]
fn parse_introspection_query() {
let schema = "type Query {
foo: String
stuff: Bar
array: [Bar]
baz: String
}
type Bar {
bar: String
baz: String
}";

let schema = with_supergraph_boilerplate(schema)
.parse::<Schema>()
.expect("could not parse schema");
let api_schema = schema.api_schema();

let query = "{
__type(name: \"Bar\") {
name
fields {
name
type {
name
}
}
}
}}";
let _ = Query::parse(query, api_schema).unwrap();

let query = "query {
__schema {
queryType {
name
}
}
}";

let _ = Query::parse(query, api_schema).unwrap();
}
}
11 changes: 10 additions & 1 deletion apollo-router/src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ impl std::str::FromStr for Schema {
}

fn parse(schema: &str) -> Result<Schema, SchemaError> {
let parser = apollo_parser::Parser::new(schema);
let schema_with_introspection = Schema::with_introspection(schema);
let parser = apollo_parser::Parser::new(&schema_with_introspection);
let tree = parser.parse();

// Trace log recursion limit data
Expand Down Expand Up @@ -430,6 +431,14 @@ impl Schema {
pub fn boxed(self) -> Box<Self> {
Box::new(self)
}

fn with_introspection(schema: &str) -> String {
format!(
"{}\n{}",
schema,
include_str!("introspection_types.graphql")
)
}
}

#[derive(Debug)]
Expand Down
6 changes: 4 additions & 2 deletions apollo-router/src/spec/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ impl Selection {

let field_type = if field_name.as_str() == "__typename" {
FieldType::String
} else if field_name.starts_with("__") {
FieldType::Introspection(field_name.clone())
} else if field_name == "__schema" {
FieldType::Introspection("__Schema".to_string())
} else if field_name == "__type" {
FieldType::Introspection("__Type".to_string())
} else {
current_type
.inner_type_name()
Expand Down