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

graphql: update introspection schema #4676

Merged
merged 5 commits into from
Sep 13, 2023
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
13 changes: 13 additions & 0 deletions graph/src/data/graphql/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ impl DocumentExt for Document {
}
}

pub trait DefinitionExt {
fn is_root_query_type(&self) -> bool;
}

impl DefinitionExt for Definition {
fn is_root_query_type(&self) -> bool {
match self {
Definition::TypeDefinition(TypeDefinition::Object(t)) => t.name == "Query",
_ => false,
}
}
}

pub trait TypeExt {
fn get_base_type(&self) -> &str;
fn is_list(&self) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion graph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub mod prelude {
});
static_graphql!(s, schema, {
Field, Directive, InterfaceType, ObjectType, Value, TypeDefinition,
EnumType, Type, Document, ScalarType, InputValue, DirectiveDefinition,
EnumType, Type, Definition, Document, ScalarType, InputValue, DirectiveDefinition,
UnionType, InputObjectType, EnumValue,
});

Expand Down
35 changes: 29 additions & 6 deletions graph/src/schema/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use inflector::Inflector;
use lazy_static::lazy_static;

use crate::components::store::EntityType;
use crate::data::graphql::ObjectOrInterface;
use crate::data::graphql::{ObjectOrInterface, ObjectTypeExt};
use crate::schema::{ast, META_FIELD_NAME, META_FIELD_TYPE};

use crate::data::graphql::ext::{DirectiveExt, DocumentExt, ValueExt};
use crate::data::graphql::ext::{DefinitionExt, DirectiveExt, DocumentExt, ValueExt};
use crate::prelude::s::{Value, *};
use crate::prelude::*;
use thiserror::Error;
Expand Down Expand Up @@ -223,7 +223,7 @@ impl ApiSchema {
}

#[cfg(debug_assertions)]
pub fn definitions(&self) -> impl Iterator<Item = &s::Definition<'static, String>> {
pub fn definitions(&self) -> impl Iterator<Item = &s::Definition> {
self.schema.document.definitions.iter()
}
}
Expand All @@ -233,8 +233,24 @@ lazy_static! {
let schema = include_str!("introspection.graphql");
parse_schema(schema).expect("the schema `introspection.graphql` is invalid")
};
pub static ref INTROSPECTION_QUERY_TYPE: ast::ObjectType = {
let root_query_type = INTROSPECTION_SCHEMA
.get_root_query_type()
.expect("Schema does not have a root query type");
ast::ObjectType::from(Arc::new(root_query_type.clone()))
};
}

pub fn is_introspection_field(name: &str) -> bool {
INTROSPECTION_QUERY_TYPE.field(name).is_some()
}

/// Extend `schema` with the definitions from the introspection schema and
/// modify the root query type to contain the fields from the introspection
/// schema's root query type.
///
/// This results in a schema that combines the original schema with the
/// introspection schema
fn add_introspection_schema(schema: &mut Document) {
fn introspection_fields() -> Vec<Field> {
// Generate fields for the root query fields in an introspection schema,
Expand Down Expand Up @@ -274,9 +290,16 @@ fn add_introspection_schema(schema: &mut Document) {
]
}

schema
.definitions
.extend(INTROSPECTION_SCHEMA.definitions.iter().cloned());
// Add all definitions from the introspection schema to the schema,
// except for the root query type as that qould clobber the 'real' root
// query type
schema.definitions.extend(
INTROSPECTION_SCHEMA
.definitions
.iter()
.filter(|dfn| !dfn.is_root_query_type())
.cloned(),
);

let query_type = schema
.definitions
Expand Down
22 changes: 16 additions & 6 deletions graph/src/schema/introspection.graphql
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# A GraphQL introspection schema for inclusion in a subgraph's API schema.
# The schema differs from the 'standard' introspection schema in that it
# doesn't have a Query type nor scalar declarations as they come from the
# API schema.

type Query {
__schema: __Schema!
__type(name: String!): __Type
}

type __Schema {
description: String
types: [__Type!]!
queryType: __Type!
mutationType: __Type
Expand Down Expand Up @@ -33,12 +36,15 @@ type __Type {

# NON_NULL and LIST only
ofType: __Type

# may be non-null for custom SCALAR, otherwise null.
specifiedByURL: String
}

type __Field {
name: String!
description: String
args: [__InputValue!]!
args(includeDeprecated: Boolean = false): [__InputValue!]!
type: __Type!
isDeprecated: Boolean!
deprecationReason: String
Expand All @@ -49,6 +55,8 @@ type __InputValue {
description: String
type: __Type!
defaultValue: String
isDeprecated: Boolean!
deprecationReason: String
}

type __EnumValue {
Expand All @@ -73,7 +81,8 @@ type __Directive {
name: String!
description: String
locations: [__DirectiveLocation!]!
args: [__InputValue!]!
args(includeDeprecated: Boolean = false): [__InputValue!]!
isRepeatable: Boolean!
dotansimha marked this conversation as resolved.
Show resolved Hide resolved
}

enum __DirectiveLocation {
Expand All @@ -84,6 +93,7 @@ enum __DirectiveLocation {
FRAGMENT_DEFINITION
FRAGMENT_SPREAD
INLINE_FRAGMENT
VARIABLE_DEFINITION
SCHEMA
SCALAR
OBJECT
Expand All @@ -95,4 +105,4 @@ enum __DirectiveLocation {
ENUM_VALUE
INPUT_OBJECT
INPUT_FIELD_DEFINITION
}
}
2 changes: 1 addition & 1 deletion graph/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub mod ast;
mod fulltext;
mod input_schema;

pub use api::{api_schema, APISchemaError};
pub use api::{api_schema, is_introspection_field, APISchemaError, INTROSPECTION_QUERY_TYPE};

pub use api::{ApiSchema, ErrorPolicy};
pub use fulltext::{FulltextAlgorithm, FulltextConfig, FulltextDefinition, FulltextLanguage};
Expand Down
3 changes: 1 addition & 2 deletions graphql/src/execution/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use graph::{
value::{Object, Word},
},
prelude::{s, CheapClone},
schema::META_FIELD_NAME,
schema::{is_introspection_field, INTROSPECTION_QUERY_TYPE, META_FIELD_NAME},
util::{lfu_cache::EvictStats, timed_rw_lock::TimedMutex},
};
use lazy_static::lazy_static;
Expand All @@ -24,7 +24,6 @@ use graph::util::{lfu_cache::LfuCache, stable_hash_glue::impl_stable_hash};

use super::QueryHash;
use crate::execution::ast as a;
use crate::introspection::{is_introspection_field, INTROSPECTION_QUERY_TYPE};
use crate::prelude::*;

lazy_static! {
Expand Down
2 changes: 0 additions & 2 deletions graphql/src/introspection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod resolver;
mod schema;

pub use self::resolver::IntrospectionResolver;
pub use self::schema::{is_introspection_field, INTROSPECTION_DOCUMENT, INTROSPECTION_QUERY_TYPE};
132 changes: 0 additions & 132 deletions graphql/src/introspection/schema.rs

This file was deleted.