private void CompleteInterfaces( ICompletionContext context, ObjectTypeDefinition definition) { if (ClrType != typeof(object)) { foreach (Type interfaceType in ClrType.GetInterfaces()) { if (context.TryGetType( new ClrTypeReference(interfaceType, TypeContext.Output), out InterfaceType type)) { _interfaces[type.Name] = type; } } } foreach (ITypeReference interfaceRef in definition.Interfaces) { if (!context.TryGetType(interfaceRef, out InterfaceType type)) { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage( "COULD NOT RESOLVE INTERFACE") .SetCode(ErrorCodes.Schema.MissingType) .SetTypeSystemObject(this) .AddSyntaxNode(SyntaxNode) .Build()); } _interfaces[type.Name] = type; } }
private void CompleteTypeSet( ICompletionContext context, UnionTypeDefinition definition) { var typeSet = new HashSet <ObjectType>(); OnCompleteTypeSet(context, definition, typeSet); foreach (ObjectType objectType in typeSet) { _typeMap[objectType.Name] = objectType; } if (typeSet.Count == 0) { // TODO : RESOURCES context.ReportError(SchemaErrorBuilder.New() .SetMessage("A Union type must define one or " + "more unique member types.") .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(this) .AddSyntaxNode(SyntaxNode) .Build()); } }
internal sealed override void CompleteName(ICompletionContext context) { if (_definition is null) { throw new InvalidOperationException( TypeResources.TypeSystemObjectBase_DefinitionIsNull); } context.Interceptor.OnBeforeCompleteName( context, _definition, _definition.ContextData); ExecuteConfigurations(context, _definition, ApplyConfigurationOn.Naming); OnCompleteName(context, _definition); if (Name.IsEmpty) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, TypeResources.TypeSystemObjectBase_NameIsNull, GetType().FullName)) .SetCode(ErrorCodes.Schema.NoName) .SetTypeSystemObject(this) .Build()); } base.CompleteName(context); context.Interceptor.OnAfterCompleteName( context, _definition, _definition.ContextData); }
private bool ValidateFields( ICompletionContext context, ObjectTypeDefinition definition) { ObjectFieldDefinition[] invalidFields = definition.Fields.Where(t => t.Type is null).ToArray(); foreach (ObjectFieldDefinition field in invalidFields) { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, "Unable to infer or resolve the type of " + "field {0}.{1}. Try to explicitly provide the " + "type like the following: " + "`descriptor.Field(\"field\")" + ".Type<List<StringType>>()`.", Name, field.Name)) .SetCode(ErrorCodes.Schema.NoFieldType) .SetTypeSystemObject(this) .SetPath(Path.New(Name).Append(field.Name)) .SetExtension(TypeErrorFields.Definition, field) .Build()); } return(invalidFields.Length == 0); }
protected override void OnCompleteType( ICompletionContext context, DirectiveTypeDefinition definition) { base.OnCompleteType(context, definition); _converter = context.Services.GetTypeConversion(); MiddlewareComponents = definition.MiddlewareComponents.ToList().AsReadOnly(); SyntaxNode = definition.SyntaxNode; ClrType = definition.ClrType == GetType() ? typeof(object) : definition.ClrType; IsRepeatable = definition.IsRepeatable; Locations = definition.Locations.ToList().AsReadOnly(); Arguments = new FieldCollection <Argument>( definition.Arguments.Select(t => new Argument(t))); IsExecutable = MiddlewareComponents.Count > 0; if (!Locations.Any()) { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage( $"The `{Name}` directive does not declare any " + "location on which it is valid.") .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.SyntaxNode) .Build()); } FieldInitHelper.CompleteFields(context, definition, Arguments); }
protected override void OnCompleteType( ICompletionContext context, DirectiveTypeDefinition definition) { base.OnCompleteType(context, definition); _converter = context.Services.GetTypeConversion(); MiddlewareComponents = definition.MiddlewareComponents.ToList().AsReadOnly(); SyntaxNode = definition.SyntaxNode; Locations = definition.Locations.ToList().AsReadOnly(); Arguments = new FieldCollection <Argument>( definition.Arguments.Select(t => new Argument(t))); IsExecutable = MiddlewareComponents.Count > 0; if (!Locations.Any()) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, TypeResources.DirectiveType_NoLocations, Name)) .SetCode(ErrorCodes.Schema.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.SyntaxNode) .Build()); } FieldInitHelper.CompleteFields(context, definition, Arguments); }
public static IValueNode CreateDefaultValue( ICompletionContext context, ArgumentDefinition definition, IInputType fieldType) { try { if (definition.NativeDefaultValue != null) { return(fieldType.ParseValue( definition.NativeDefaultValue)); } return(definition.DefaultValue.IsNull() ? NullValueNode.Default : definition.DefaultValue); } catch (Exception ex) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(TypeResources.FieldInitHelper_InvalidDefaultValue) .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.SyntaxNode) .SetException(ex) .Build()); return(NullValueNode.Default); } }
public static void CompleteFields <TTypeDef, TFieldType, TFieldDef>( ICompletionContext context, TTypeDef definition, IReadOnlyCollection <FieldBase <TFieldType, TFieldDef> > fields) where TTypeDef : DefinitionBase, IHasSyntaxNode where TFieldType : IType where TFieldDef : FieldDefinitionBase { if (context.Type is IType type && fields.Count == 0) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, TypeResources.FieldInitHelper_NoFields)) .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.SyntaxNode) .Build()); return; } foreach (FieldBase <TFieldType, TFieldDef> field in fields) { field.CompleteField(context); } }
protected override void OnCompleteType( ICompletionContext context, EnumTypeDefinition definition) { base.OnCompleteType(context, definition); SyntaxNode = definition.SyntaxNode; foreach (EnumValue enumValue in definition.Values .Select(t => new EnumValue(t))) { _nameToValues[enumValue.Name] = enumValue; _valueToValues[enumValue.Value] = enumValue; enumValue.CompleteValue(context); } if (!Values.Any()) { context.ReportError( SchemaErrorBuilder.New() .SetMessage(TypeResources.EnumType_NoValues, Name) .SetCode(ErrorCodes.Schema.NoEnumValues) .SetTypeSystemObject(this) .AddSyntaxNode(SyntaxNode) .Build()); } }
public static void CompleteFields <TTypeDef, TFieldType, TFieldDef>( ICompletionContext context, TTypeDef definition, IReadOnlyCollection <FieldBase <TFieldType, TFieldDef> > fields) where TTypeDef : DefinitionBase, IHasSyntaxNode where TFieldType : IType where TFieldDef : FieldDefinitionBase { if (context.Type is IType type && fields.Count == 0) { // TODO : RESOURCES context.ReportError(SchemaErrorBuilder.New() .SetMessage($"{type.Kind} `{definition.Name}` has no fields declared.") .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.SyntaxNode) .Build()); return; } foreach (FieldBase <TFieldType, TFieldDef> field in fields) { field.CompleteField(context); } }
public static IValueNode CreateDefaultValue( ICompletionContext context, ArgumentDefinition definition, IInputType fieldType) { try { if (definition.NativeDefaultValue != null) { return(fieldType.ParseValue( definition.NativeDefaultValue)); } return(definition.DefaultValue.IsNull() ? NullValueNode.Default : definition.DefaultValue); } catch (Exception ex) { // TODO : RESOURCES context.ReportError(SchemaErrorBuilder.New() .SetMessage( "Could not parse the native value of input field " + $"`{context.Type.Name}.{definition.Name}`.") .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.SyntaxNode) .SetException(ex) .Build()); return(NullValueNode.Default); } }
private bool TryCompleteDirective( ICompletionContext context, DirectiveDefinition definition, ISet <string> processed, out Directive directive) { DirectiveType directiveType = context.GetDirectiveType(definition.Reference); directive = null; if (directiveType != null) { if (!processed.Add(directiveType.Name) && !directiveType.IsRepeatable) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, TypeResources.DirectiveCollection_DirectiveIsUnique, directiveType.Name)) .SetCode(ErrorCodes.Schema.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.ParsedDirective) .SetExtension("Source", _source) .Build()); } else if (directiveType.Locations.Contains(_location)) { directive = Directive.FromDescription(directiveType, definition, _source); } else { context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, TypeResources.DirectiveCollection_LocationNotAllowed, directiveType.Name, _location)) .SetCode(ErrorCodes.Schema.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.ParsedDirective) .SetExtension("Source", _source) .Build()); } } return(directive != null); }
private void CompleteDirective( ICompletionContext context, DirectiveDefinition definition, ISet <string> processed) { DirectiveType directiveType = context.GetDirectiveType(definition.Reference); if (directiveType != null) { if (!processed.Add(directiveType.Name) && !directiveType.IsRepeatable) { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage( $"The specified directive `@{directiveType.Name}` " + "is unique and cannot be added twice.") .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.ParsedDirective) .Build()); } else if (directiveType.Locations.Contains(_location)) { _directives.Add(Directive.FromDescription( directiveType, definition, _source)); } else { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage( $"The specified directive `@{directiveType.Name}` " + "is not allowed on the current location " + $"`{_location}`.") .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.ParsedDirective) .Build()); } } }
private void CompleteResolver( ICompletionContext context, ObjectFieldDefinition definition) { bool isIntrospectionField = IsIntrospectionField || DeclaringType.IsIntrospectionType(); Resolver = definition.Resolver; if (!isIntrospectionField || Resolver == null) { // gets resolvers that were provided via type extensions, // explicit resolver results or are provided through the // resolver compiler. FieldResolver resolver = context.GetResolver(definition.Name); Resolver = GetMostSpecificResolver( context.Type.Name, Resolver, resolver); } IReadOnlySchemaOptions options = context.DescriptorContext.Options; bool skipMiddleware = options.FieldMiddleware == FieldMiddlewareApplication.AllFields ? false : isIntrospectionField; Middleware = FieldMiddlewareCompiler.Compile( context.GlobalComponents, definition.MiddlewareComponents.ToArray(), Resolver, skipMiddleware); if (Resolver == null && Middleware == null) { if (_executableDirectives.Any()) { Middleware = ctx => Task.CompletedTask; } else { context.ReportError(SchemaErrorBuilder.New() .SetMessage( $"The field `{context.Type.Name}.{Name}` " + "has no resolver.") .SetCode(ErrorCodes.Schema.NoResolver) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.SyntaxNode) .Build()); } } }
private void CompleteResolver( ICompletionContext context, ObjectFieldDefinition definition) { bool isIntrospectionField = IsIntrospectionField || DeclaringType.IsIntrospectionType(); Resolver = definition.Resolver; if (Resolver == null || !isIntrospectionField) { var fieldReference = new FieldReference( context.Type.Name, definition.Name); FieldResolver resolver = context.GetResolver(fieldReference); if (resolver != null) { Resolver = resolver.Resolver; } } Middleware = FieldMiddlewareCompiler.Compile( context.GlobalComponents, definition.MiddlewareComponents.ToArray(), Resolver, isIntrospectionField); if (Resolver == null && Middleware == null) { if (_executableDirectives.Any()) { Middleware = ctx => Task.CompletedTask; } else { context.ReportError(SchemaErrorBuilder.New() .SetMessage( $"The field `{context.Type.Name}.{Name}` " + "has no resolver.") .SetCode(TypeErrorCodes.NoResolver) .SetTypeSystemObject(context.Type) .AddSyntaxNode(definition.SyntaxNode) .Build()); } } }
internal sealed override void CompleteName(ICompletionContext context) { ExecuteConfigurations(context, ApplyConfigurationOn.Naming); OnCompleteName(context, _definition); if (Name.IsEmpty) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, TypeResources.TypeSystemObjectBase_NameIsNull, GetType().FullName)) .SetCode(TypeErrorCodes.NoName) .SetTypeSystemObject(this) .Build()); } base.CompleteName(context); }
public static void Complete( ICompletionContext context, IComplexOutputTypeDefinition definition, Type clrType, ICollection <InterfaceType> interfaces, ITypeSystemObject interfaceOrObject, ISyntaxNode?node) { if (clrType != typeof(object)) { TryInferInterfaceUsageFromClrType(context, clrType, interfaces); } if (definition.KnownClrTypes.Count > 0) { definition.KnownClrTypes.Remove(typeof(object)); foreach (Type type in definition.KnownClrTypes.Distinct()) { TryInferInterfaceUsageFromClrType(context, type, interfaces); } } foreach (ITypeReference interfaceRef in definition.Interfaces) { if (!context.TryGetType(interfaceRef, out InterfaceType type)) { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage("COULD NOT RESOLVE INTERFACE") .SetCode(ErrorCodes.Schema.MissingType) .SetTypeSystemObject(interfaceOrObject) .AddSyntaxNode(node) .Build()); } if (!interfaces.Contains(type)) { interfaces.Add(type); } } }
protected override void OnCompleteField( ICompletionContext context, ArgumentDefinition definition) { if (definition.Type == null) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, TypeResources.Argument_TypeIsNull, definition.Name)) .SetTypeSystemObject(context.Type) .Build()); } else { base.OnCompleteField(context, definition); DefaultValue = FieldInitHelper.CreateDefaultValue(context, definition, Type); } }
private void CompleteInterfaces( ICompletionContext context, ObjectTypeDefinition definition) { if (definition.Name == "Some") { } if (ClrType != typeof(object)) { TryInferInterfaceUsageFromClrType(context, ClrType); } if (definition.KnownClrTypes.Count > 0) { definition.KnownClrTypes.Remove(typeof(object)); foreach (Type clrType in definition.KnownClrTypes.Distinct()) { TryInferInterfaceUsageFromClrType(context, clrType); } } foreach (ITypeReference interfaceRef in definition.Interfaces) { if (!context.TryGetType(interfaceRef, out InterfaceType type)) { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage("COULD NOT RESOLVE INTERFACE") .SetCode(ErrorCodes.Schema.MissingType) .SetTypeSystemObject(this) .AddSyntaxNode(SyntaxNode) .Build()); } if (!_interfaces.Contains(type)) { _interfaces.Add(type); } } }
private void MergeValues( ICompletionContext context, EnumTypeDefinition extension, EnumTypeDefinition type) { foreach (EnumValueDefinition enumValue in extension.Values.Where(t => t.Value != null)) { if (type.ClrType.IsAssignableFrom(enumValue.Value.GetType())) { EnumValueDefinition existingValue = type.Values.FirstOrDefault(t => enumValue.Value.Equals(t.Value)); if (existingValue == null) { type.Values.Add(enumValue); } else { TypeExtensionHelper.MergeDirectives( context, enumValue.Directives, existingValue.Directives); } } else { context.ReportError( SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, TypeResources.EnumTypeExtension_ValueTypeInvalid, enumValue.Value)) .SetTypeSystemObject(this) .Build()); } } }
private void CompleteTypeSet( ICompletionContext context, UnionTypeDefinition definition) { var typeSet = new HashSet <ObjectType>(); OnCompleteTypeSet(context, definition, typeSet); foreach (ObjectType objectType in typeSet) { _typeMap[objectType.Name] = objectType; } if (typeSet.Count == 0) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(TypeResources.UnionType_MustHaveTypes) .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(this) .AddSyntaxNode(SyntaxNode) .Build()); } }
protected virtual void OnCompleteTypeSet( ICompletionContext context, UnionTypeDefinition definition, ISet <ObjectType> typeSet) { foreach (ITypeReference typeReference in definition.Types) { if (context.TryGetType(typeReference, out ObjectType ot)) { typeSet.Add(ot); } else { context.ReportError(SchemaErrorBuilder.New() .SetMessage(TypeResources.UnionType_UnableToResolveType) .SetCode(TypeErrorCodes.MissingType) .SetTypeSystemObject(this) .SetExtension(_typeReference, typeReference) .AddSyntaxNode(SyntaxNode) .Build()); } } }
private bool ValidateFields( ICompletionContext context, ObjectTypeDefinition definition) { ObjectFieldDefinition[] invalidFields = definition.Fields.Where(t => t.Type is null).ToArray(); foreach (ObjectFieldDefinition field in invalidFields) { context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, "Unable to infer or resolve the type of field {0}.{1}.", Name, field.Name)) .SetCode(TypeErrorCodes.NoFieldType) .SetTypeSystemObject(this) .SetPath(Path.New(Name).Append(field.Name)) .Build()); } return(invalidFields.Length == 0); }
private void MergeValues( ICompletionContext context, EnumTypeDefinition extension, EnumTypeDefinition type) { // TODO : we have to rework this once directive support is in. foreach (EnumValueDefinition enumValue in extension.Values.Where(t => t.Value != null)) { if (type.ClrType.IsAssignableFrom(enumValue.Value.GetType())) { type.Values.Add(enumValue); } else { // TODO : resources context.ReportError( SchemaErrorBuilder.New() .SetMessage("Invalid Type") .SetTypeSystemObject(this) .Build()); } } }
private void ValidateArguments(ICompletionContext context, Directive directive) { Dictionary <string, ArgumentNode> arguments = directive.ToNode().Arguments.ToDictionary(t => t.Name.Value); foreach (ArgumentNode argument in arguments.Values) { if (directive.Type.Arguments.TryGetField( argument.Name.Value, out Argument arg)) { if (!arg.Type.IsInstanceOfType(argument.Value)) { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, "The argument `{0}` value type is wrong.", arg.Name)) .SetCode(ErrorCodes.Schema.ArgumentValueTypeWrong) .SetTypeSystemObject(context.Type) .AddSyntaxNode(directive.ToNode()) .Build()); } } else { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, "The argument `{0}` does not exist on the " + "directive `{1}`.", argument.Name.Value, directive.Type.Name)) .SetCode(ErrorCodes.Schema.InvalidArgument) .SetTypeSystemObject(context.Type) .AddSyntaxNode(directive.ToNode()) .Build()); } } foreach (Argument argument in directive.Type.Arguments .Where(a => a.Type.IsNonNullType())) { if (!arguments.TryGetValue(argument.Name, out ArgumentNode arg) || arg.Value is NullValueNode) { // TODO : resources context.ReportError(SchemaErrorBuilder.New() .SetMessage(string.Format( CultureInfo.InvariantCulture, "The argument `{0}` of directive `{1}` " + "mustn't be null.", argument.Name.Value, directive.Type.Name)) .SetCode(ErrorCodes.Schema.NonNullArgument) .SetTypeSystemObject(context.Type) .AddSyntaxNode(directive.ToNode()) .Build()); } } }