Added introspection fields for each field on __Type

This commit is contained in:
Tobe O 2018-08-03 13:57:28 -04:00
parent 054b96d4f4
commit b0116ed8c6
3 changed files with 62 additions and 2 deletions

View file

@ -1,8 +1,12 @@
part of graphql_schema.src.schema;
GraphQLObjectType objectType(String name,
{String description, Iterable<GraphQLField> fields = const []}) =>
new GraphQLObjectType(name, description)..fields.addAll(fields ?? []);
{String description,
Iterable<GraphQLField> fields = const [],
Iterable<GraphQLObjectType> interfaces = const []}) =>
new GraphQLObjectType(name, description)
..fields.addAll(fields ?? [])
..interfaces.addAll(interfaces ?? []);
GraphQLField<T, Serialized> field<T, Serialized>(String name,
{Iterable<GraphQLFieldArgument<T, Serialized>> arguments: const [],

View file

@ -7,6 +7,9 @@ class GraphQLObjectType
final String description;
final List<GraphQLField> fields = [];
/// A list of other types that this object type is known to implement.
final List<GraphQLObjectType> interfaces = [];
GraphQLObjectType(this.name, this.description);
@override

View file

@ -38,6 +38,7 @@ GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) {
_reflectFields(),
_reflectDirectiveType(),
_reflectInputValueType(),
_reflectEnumValueType(),
]);
var fields = <GraphQLField>[
@ -87,6 +88,31 @@ GraphQLObjectType _reflectSchemaTypes() {
),
);
_typeType.fields.add(
field(
'interfaces',
type: listType(_reflectSchemaTypes().nonNullable()),
resolve: (type, _) {
if (type is GraphQLObjectType) {
return type.interfaces;
} else {
return <GraphQLType>[];
}
},
),
);
_typeType.fields.add(
field(
'possibleTypes',
type: listType(_reflectSchemaTypes().nonNullable()),
resolve: (type, _) {
// TODO: Interface and union types
return <GraphQLType>[];
},
),
);
var fieldType = _reflectFields();
var inputValueType = _reflectInputValueType();
var typeField = fieldType.fields
@ -120,7 +146,9 @@ GraphQLObjectType _reflectSchemaTypes() {
}
GraphQLObjectType _createTypeType() {
var enumValueType = _reflectEnumValueType();
var fieldType = _reflectFields();
var inputValueType = _reflectInputValueType();
return objectType('__Type', fields: [
field(
@ -169,6 +197,25 @@ GraphQLObjectType _createTypeType() {
.toList()
: [],
),
field(
'enumValues',
type: listType(enumValueType.nonNullable()),
arguments: [
new GraphQLFieldArgument(
'includeDeprecated',
graphQLBoolean,
defaultValue: false,
),
],
),
field(
'inputFields',
type: listType(inputValueType.nonNullable()),
resolve: (obj, _) {
// TODO: INPUT_OBJECT type
return <GraphQLFieldArgument>[];
},
),
]);
}
@ -261,6 +308,12 @@ GraphQLObjectType _reflectDirectiveType() {
]);
}
GraphQLObjectType _enumValueType;
GraphQLObjectType _reflectEnumValueType() {
return _enumValueType ?? objectType('__EnumValue', fields: []);
}
List<GraphQLObjectType> fetchAllTypes(GraphQLSchema schema) {
var typess = <GraphQLType>[];
typess.addAll(_fetchAllTypesFromObject(schema.query));