Create __InputValue type

This commit is contained in:
Tobe O 2018-08-03 13:45:40 -04:00
parent 9b9b8018d7
commit 66e1b4a9b1
4 changed files with 64 additions and 11 deletions

View file

@ -4,10 +4,11 @@ class GraphQLFieldArgument<Value, Serialized> {
final String name; final String name;
final GraphQLType<Value, Serialized> type; final GraphQLType<Value, Serialized> type;
final Value defaultValue; final Value defaultValue;
final String description;
/// If [defaultValue] is `null`, and `null` is a valid value for this argument, set this to `true`. /// If [defaultValue] is `null`, and `null` is a valid value for this argument, set this to `true`.
final bool defaultsToNull; final bool defaultsToNull;
GraphQLFieldArgument(this.name, this.type, GraphQLFieldArgument(this.name, this.type,
{this.defaultValue, this.defaultsToNull: false}); {this.defaultValue, this.defaultsToNull: false, this.description});
} }

View file

@ -30,7 +30,9 @@ final GraphQLScalarType<double, double> graphQLFloat =
abstract class GraphQLScalarType<Value, Serialized> abstract class GraphQLScalarType<Value, Serialized>
extends GraphQLType<Value, Serialized> extends GraphQLType<Value, Serialized>
with _NonNullableMixin<Value, Serialized> {} with _NonNullableMixin<Value, Serialized> {
Type get valueType => Value;
}
typedef bool _NumVerifier(x); typedef bool _NumVerifier(x);

View file

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:graphql_parser/graphql_parser.dart'; import 'package:graphql_parser/graphql_parser.dart';
import 'package:graphql_schema/graphql_schema.dart'; import 'package:graphql_schema/graphql_schema.dart';
import 'introspection.dart'; import 'introspection.dart';
class GraphQL { class GraphQL {
@ -305,6 +306,7 @@ class GraphQL {
} }
if (fieldType is GraphQLScalarType) { if (fieldType is GraphQLScalarType) {
try {
var validation = fieldType.validate(fieldName, result); var validation = fieldType.validate(fieldName, result);
if (!validation.successful) { if (!validation.successful) {
@ -312,6 +314,10 @@ class GraphQL {
} else { } else {
return validation.value; return validation.value;
} }
} on TypeError {
throw new GraphQLException(
'Value of field "$fieldName" must be ${fieldType.valueType}, got $result instead.');
}
} }
if (fieldType is GraphQLObjectType) { if (fieldType is GraphQLObjectType) {

View file

@ -36,6 +36,8 @@ GraphQLSchema reflectSchema(GraphQLSchema schema, List<GraphQLType> allTypes) {
typeType, typeType,
schemaType, schemaType,
_reflectFields(), _reflectFields(),
_reflectDirectiveType(),
_reflectInputValueType(),
]); ]);
var fields = <GraphQLField>[ var fields = <GraphQLField>[
@ -86,6 +88,7 @@ GraphQLObjectType _reflectSchemaTypes() {
); );
var fieldType = _reflectFields(); var fieldType = _reflectFields();
var inputValueType = _reflectInputValueType();
var typeField = fieldType.fields var typeField = fieldType.fields
.firstWhere((f) => f.name == 'type', orElse: () => null); .firstWhere((f) => f.name == 'type', orElse: () => null);
@ -98,6 +101,19 @@ GraphQLObjectType _reflectSchemaTypes() {
), ),
); );
} }
typeField = fieldType.fields
.firstWhere((f) => f.name == 'type', orElse: () => null);
if (typeField == null) {
inputValueType.fields.add(
field(
'type',
type: _reflectSchemaTypes(),
resolve: (f, _) => (f as GraphQLFieldArgument).type,
),
);
}
} }
return _typeType; return _typeType;
@ -155,6 +171,8 @@ GraphQLObjectType _reflectFields() {
} }
GraphQLObjectType _createFieldType() { GraphQLObjectType _createFieldType() {
var inputValueType = _reflectInputValueType();
return objectType('__Field', fields: [ return objectType('__Field', fields: [
field( field(
'name', 'name',
@ -163,7 +181,7 @@ GraphQLObjectType _createFieldType() {
), ),
field( field(
'isDeprecated', 'isDeprecated',
type: graphQLString, type: graphQLBoolean,
resolve: (f, _) => (f as GraphQLField).isDeprecated, resolve: (f, _) => (f as GraphQLField).isDeprecated,
), ),
field( field(
@ -173,13 +191,38 @@ GraphQLObjectType _createFieldType() {
), ),
field( field(
'args', 'args',
type: listType(graphQLString.nonNullable()).nonNullable(), // TODO: Input value type type: listType(inputValueType.nonNullable()).nonNullable(),
resolve: (f, _) => (f as GraphQLField).arguments, resolve: (f, _) => (f as GraphQLField).arguments,
), ),
]); ]);
} }
GraphQLObjectType _inputValueType;
GraphQLObjectType _reflectInputValueType() {
return _inputValueType ??= objectType('__InputValue', fields: [
field(
'name',
type: graphQLString,
resolve: (obj, _) => (obj as GraphQLFieldArgument).name,
),
field(
'description',
type: graphQLString,
resolve: (obj, _) => (obj as GraphQLFieldArgument).description,
),
field(
'defaultValue',
type: graphQLString,
resolve: (obj, _) =>
(obj as GraphQLFieldArgument).defaultValue?.toString(),
),
]);
}
GraphQLObjectType _reflectDirectiveType() { GraphQLObjectType _reflectDirectiveType() {
var inputValueType = _reflectInputValueType();
// TODO: What actually is this??? // TODO: What actually is this???
return objectType('__Directive', fields: [ return objectType('__Directive', fields: [
field( field(
@ -194,12 +237,13 @@ GraphQLObjectType _reflectDirectiveType() {
), ),
field( field(
'locations', 'locations',
type: listType(graphQLString.nonNullable()).nonNullable(), // TODO: Enum directiveLocation type: listType(graphQLString.nonNullable()).nonNullable(),
// TODO: Enum directiveLocation
resolve: (obj, _) => [], resolve: (obj, _) => [],
), ),
field( field(
'args', 'args',
type: listType(graphQLString.nonNullable()).nonNullable(), type: listType(inputValueType.nonNullable()).nonNullable(),
resolve: (obj, _) => [], resolve: (obj, _) => [],
), ),
]); ]);