Example #1
0
        /// <summary>
        /// Determines whether the the provided type to check is IDictionary{ExpectedKey, AnyValue} ensuring that it can be
        /// used as an input to a batch processor that supplies data to multiple waiting items.
        /// </summary>
        /// <param name="typeToCheck">The type being checked.</param>
        /// <param name="expectedKeyType">The expected key type.</param>
        /// <param name="batchedItemType">Type of the batched item. if the type to check returns an IEnumerable per
        /// key this type must represent the individual item of the set. (i.e. the 'T' of IEnumerable{T}).</param>
        /// <returns><c>true</c> if the type represents a dictionary keyed on the expected key type; otherwise, <c>false</c>.</returns>
        public static bool IsBatchDictionaryType(Type typeToCheck, Type expectedKeyType, Type batchedItemType)
        {
            if (typeToCheck == null || expectedKeyType == null || batchedItemType == null)
            {
                return(false);
            }

            var enumeratedType = typeToCheck.GetEnumerableUnderlyingType();

            if (enumeratedType == null || !enumeratedType.IsGenericType || typeof(KeyValuePair <,>) != enumeratedType.GetGenericTypeDefinition())
            {
                return(false);
            }

            var paramSet = enumeratedType.GetGenericArguments();

            if (paramSet.Length != 2)
            {
                return(false);
            }

            if (paramSet[0] != expectedKeyType)
            {
                return(false);
            }

            var unwrapped = GraphValidation.EliminateWrappersFromCoreType(paramSet[1]);

            if (unwrapped != batchedItemType)
            {
                return(false);
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Ensures the provided <see cref="IGraphType" /> exists in this collection (adding it if it is missing)
        /// and that the given type reference is assigned to it. An exception will be thrown if the type reference is already assigned
        /// to a different <see cref="IGraphType" />. No dependents or additional types will be added.
        /// </summary>
        /// <param name="graphType">Type of the graph.</param>
        /// <param name="associatedType">The concrete type to associate to the graph type.</param>
        /// <returns><c>true</c> if type had to be added, <c>false</c> if it already existed in the collection.</returns>
        public bool EnsureGraphType(IGraphType graphType, Type associatedType = null)
        {
            Validation.ThrowIfNull(graphType, nameof(graphType));

            associatedType = GraphValidation.EliminateWrappersFromCoreType(associatedType);
            GraphValidation.EnsureValidGraphTypeOrThrow(associatedType);

            // attempt to create a relationship between the graph type and the associated type
            // the concrete type collection will throw an exception if that relationship fails or isnt
            // updated to the new data correctly.
            var justAdded = _graphTypesByName.TryAdd(graphType.Name, graphType);

            _concreteTypes.EnsureRelationship(graphType, associatedType);
            _extendableGraphTypeTracker.MonitorGraphType(graphType);

            // dequeue and add any extension fields if present
            if (associatedType != null)
            {
                var unregisteredFields = _typeQueue.DequeueFields(associatedType);
                if (graphType is IExtendableGraphType objType)
                {
                    foreach (var field in unregisteredFields)
                    {
                        _extendableGraphTypeTracker.AddFieldExtension(objType, field);
                    }
                }
            }

            return(justAdded);
        }
Example #3
0
        /// <summary>
        /// Adds the given type to the schema as a graph type or a registered controller depending
        /// on the type.
        /// </summary>
        /// <param name="type">The concrete type to add.</param>
        /// <param name="kind">The kind of graph type to create from the supplied concrete type. If not supplied the concrete type will
        /// attempt to auto assign a type of scalar, enum or object as necessary.</param>
        public void EnsureGraphType(Type type, TypeKind?kind = null)
        {
            if (Validation.IsCastable <GraphController>(type))
            {
                if (GraphQLProviders.TemplateProvider.ParseType(type) is IGraphControllerTemplate controllerDefinition)
                {
                    this.AddController(controllerDefinition);
                }

                return;
            }

            type = GraphValidation.EliminateWrappersFromCoreType(type);

            // if the type is already registered, early exit no point in running it through again
            var actualKind = GraphValidation.ResolveTypeKindOrThrow(type, kind);

            if (this.Schema.KnownTypes.Contains(type, actualKind))
            {
                return;
            }

            var maker = GraphQLProviders.GraphTypeMakerProvider.CreateTypeMaker(this.Schema, actualKind);

            if (maker != null)
            {
                var result = maker.CreateGraphType(type);
                if (result != null)
                {
                    this.Schema.KnownTypes.EnsureGraphType(result.GraphType, result.ConcreteType);
                    this.EnsureDependents(result);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Builds out this field template parsing out the required type declarations unions, nameing scheme etc.  This method should be
        /// overridden in any child classes for additional decalration requirements.
        /// </summary>
        protected override void ParseTemplateDefinition()
        {
            // ensure type extension right out of the gate
            // the field can't be built otherwise
            _typeAttrib = this.SingleAttributeOfTypeOrDefault <TypeExtensionAttribute>();
            if (_typeAttrib == null)
            {
                return;
            }

            _sourceType = _typeAttrib.TypeToExtend;

            base.ParseTemplateDefinition();

            var returnType = GraphValidation.EliminateWrappersFromCoreType(this.DeclaredReturnType);

            if (returnType != typeof(IGraphActionResult))
            {
                // inspect the return type, if its a valid dictionary extract the return type from the value
                // and set the type modifiers and method type based on the value of each dictionary entry
                if (_typeAttrib.ExecutionMode == FieldResolutionMode.Batch)
                {
                    returnType          = returnType.GetValueTypeOfDictionary();
                    this.ObjectType     = GraphValidation.EliminateWrappersFromCoreType(returnType);
                    this.TypeExpression = GraphValidation.GenerateTypeExpression(returnType, this);
                    this.PossibleTypes.Insert(0, this.ObjectType);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Type extensions used as batch methods required a speceial input and output signature for the runtime
        /// to properly supply and retrieve data from the batch. This method ensures the signature coorisponds to those requirements or
        /// throws an exception indicating the problem if one is found.
        /// </summary>
        private void ValidateBatchMethodSignatureOrThrow()
        {
            if (this.Mode != FieldResolutionMode.Batch)
            {
                return;
            }

            // the method MUST accept a parameter of type IEnumerable<TypeToExtend> in its signature somewhere
            // when declared in batch mode
            var requiredEnumerable = typeof(IEnumerable <>).MakeGenericType(this.SourceObjectType);

            if (this.Arguments.All(arg => arg.DeclaredArgumentType != requiredEnumerable))
            {
                throw new GraphTypeDeclarationException(
                          $"Invalid batch method signature. The field '{this.InternalFullName}' declares itself as batch method but does not accept a batch " +
                          $"of data as an input parameter. This method must accept a parameter of type '{requiredEnumerable.FriendlyName()}' somewhere in its method signature to " +
                          $"be used as a batch extension for the type '{this.SourceObjectType.FriendlyName()}'.");
            }

            var declaredType = GraphValidation.EliminateWrappersFromCoreType(this.DeclaredReturnType);

            if (declaredType == typeof(IGraphActionResult))
            {
                return;
            }

            // when a batch method doesn't return an action result, indicating the developer
            // opts to specify his return types explicitly; ensure that their chosen return type is a dictionary
            // keyed on the type being extended allowing the runtime to seperate the batch
            // for proper segmentation in the object graph.
            // --
            // when the return type is a graph action this check is deferred after results of the batch are produced
            if (!BatchResultProcessor.IsBatchDictionaryType(declaredType, this.SourceObjectType, this.ObjectType))
            {
                throw new GraphTypeDeclarationException(
                          $"Invalid batch method signature. The field '{this.InternalFullName}' declares a return type of '{declaredType.FriendlyName()}', however; " +
                          $"batch methods must return either an '{typeof(IGraphActionResult).FriendlyName()}' or a dictionary keyed " +
                          "on the provided source data (e.g. 'IDictionary<SourceType, ResultsPerSourceItem>').");
            }

            // ensure any possible type declared via attribution matches the value type of the resultant dictionary
            // e.g.. if they supply a union for the field but declare a dictionary of IDictionary<T,K>
            // each member of the union must be castable to 'K' in order for the runtime to properly seperate
            // and process the batch results
            var dictionaryValue = GraphValidation.EliminateWrappersFromCoreType(declaredType.GetValueTypeOfDictionary());

            foreach (var type in this.PossibleTypes)
            {
                if (!Validation.IsCastable(type, dictionaryValue))
                {
                    throw new GraphTypeDeclarationException(
                              $"The field '{this.InternalFullName}' returns '{this.ObjectType.FriendlyName()}' and declares a possible type of '{type.FriendlyName()}' " +
                              $"but that type is not castable to '{this.ObjectType.FriendlyName()}' and therefore not returnable by this field. Due to the strongly-typed nature of C# any possible type on a field " +
                              "must be castable to the type of the field in order to ensure its not inadvertantly nulled out during processing. If this field returns a union " +
                              $"of multiple, disperate types consider returning '{typeof(object).Name}' from the field to ensure each possible return type can be successfully processed.");
                }
            }
        }
        /// <summary>
        /// Parses the primary metadata about the method.
        /// </summary>
        public void Parse()
        {
            DirectiveLifeCycle lifeCycle;

            switch (this.Method.Name)
            {
            case Constants.ReservedNames.DIRECTIVE_BEFORE_RESOLUTION_METHOD_NAME:
                lifeCycle = DirectiveLifeCycle.BeforeResolution;
                break;

            case Constants.ReservedNames.DIRECTIVE_AFTER_RESOLUTION_METHOD_NAME:
                lifeCycle = DirectiveLifeCycle.AfterResolution;
                break;

            default:
                return;
            }

            this.IsValidDirectiveMethod = (this.Method.ReturnType == typeof(IGraphActionResult) || this.Method.ReturnType == typeof(Task <IGraphActionResult>)) &&
                                          !this.Method.IsGenericMethod;

            this.Description  = this.Method.SingleAttributeOrDefault <DescriptionAttribute>()?.Description;
            this.DeclaredType = this.Method.ReturnType;
            this.IsAsyncField = Validation.IsCastable <Task>(this.Method.ReturnType);

            // is the method asyncronous? if so ensure that a Task<T> is returned
            // and not an empty task
            if (this.IsAsyncField && this.Method.ReturnType.IsGenericType)
            {
                // for any ssync field attempt to pull out the T in Task<T>
                var genericArgs = this.Method.ReturnType.GetGenericArguments();
                if (genericArgs.Any())
                {
                    this.DeclaredType = genericArgs[0];
                }
            }

            this.ObjectType     = GraphValidation.EliminateWrappersFromCoreType(this.DeclaredType);
            this.LifeCycle      = lifeCycle;
            this.Route          = this.GenerateRoute();
            this.TypeExpression = new GraphTypeExpression(this.ObjectType.FriendlyName());

            // parse all input parameters into the method
            foreach (var parameter in this.Method.GetParameters())
            {
                var argTemplate = new GraphFieldArgumentTemplate(this, parameter);
                argTemplate.Parse();
                _arguments.Add(argTemplate);
            }

            this.MethodSignature = this.GenerateMethodSignatureString();

            this.ExpectedReturnType = GraphValidation.EliminateWrappersFromCoreType(
                this.DeclaredType,
                false,
                true,
                false);
        }
Example #7
0
        /// <summary>
        /// Updates the field resolver used by this graph field.
        /// </summary>
        /// <param name="newResolver">The new resolver this field should use.</param>
        /// <param name="mode">The new resolution mode used by the runtime to invoke the resolver.</param>
        public void UpdateResolver(IGraphFieldResolver newResolver, FieldResolutionMode mode)
        {
            this.Resolver = newResolver;
            this.Mode     = mode;

            var unrwrappedType = GraphValidation.EliminateWrappersFromCoreType(this.Resolver?.ObjectType);

            this.IsLeaf = this.Resolver?.ObjectType != null && GraphQLProviders.ScalarProvider.IsLeaf(unrwrappedType);
        }
Example #8
0
        /// <summary>
        /// When overridden in a child class this method builds out the template according to its own individual requirements.
        /// </summary>
        protected override void ParseTemplateDefinition()
        {
            base.ParseTemplateDefinition();

            this.ExpectedReturnType = GraphValidation.EliminateWrappersFromCoreType(
                this.DeclaredReturnType,
                false,
                true,
                false);
        }
        /// <summary>
        /// Invokes this middleware component allowing it to perform its work against the supplied context.
        /// </summary>
        /// <param name="context">The context containing the request passed through the pipeline.</param>
        /// <param name="next">The delegate pointing to the next piece of middleware to be invoked.</param>
        /// <param name="cancelToken">The cancel token.</param>
        /// <returns>Task.</returns>
        public Task InvokeAsync(GraphFieldExecutionContext context, GraphMiddlewareInvocationDelegate <GraphFieldExecutionContext> next, CancellationToken cancelToken)
        {
            // ensure that the data items on teh request match the field they are being executed against
            var field = context.Field;
            var expectedFieldGraphType = _schema.KnownTypes.FindGraphType(field);
            var dataSource             = context.Request.DataSource;

            // ensure the source being supplied
            // matches the expected source of the field being resolved
            if (context.InvocationContext.ExpectedSourceType != null)
            {
                var expectedSourceType = context.InvocationContext.ExpectedSourceType;
                var actualSourceType   = GraphValidation.EliminateWrappersFromCoreType(dataSource.Value.GetType());
                if (expectedSourceType != actualSourceType)
                {
                    var expectedSourceGraphType = _schema.KnownTypes.FindGraphType(expectedSourceType, TypeKind.OBJECT);
                    var analysis = _schema.KnownTypes.AnalyzeRuntimeConcreteType(expectedSourceGraphType, actualSourceType);
                    if (!analysis.ExactMatchFound)
                    {
                        throw new GraphExecutionException(
                                  $"Operation failed. The field execution context for '{field.Route.Path}' was passed " +
                                  $"a source item of type '{dataSource.Value.GetType().FriendlyName()}' which could not be coerced " +
                                  $"to '{context.InvocationContext.ExpectedSourceType}' as requested by the target graph type '{expectedFieldGraphType.Name}'.");
                    }

                    if (context.Field.Mode == FieldResolutionMode.Batch && !(dataSource.GetType() is IEnumerable))
                    {
                        throw new GraphExecutionException(
                                  $"Operation failed. The field execution context for '{field.Route.Path}' was executed in batch mode " +
                                  $"but was not passed an {nameof(IEnumerable)} for its source data.");
                    }
                }
            }

            if (dataSource.Items.Count == 0)
            {
                var expected = context.InvocationContext.Field.Mode == FieldResolutionMode.PerSourceItem ? "1" :
                               "at least 1";

                throw new GraphExecutionException(
                          $"Operation failed. The field execution context for '{field.Route.Path}' was passed " +
                          $"0 items but expected {expected}.  (Field Mode: {field.Mode.ToString()})");
            }

            if (context.InvocationContext.Field.Mode == FieldResolutionMode.PerSourceItem &&
                dataSource.Items.Count != 1)
            {
                throw new GraphExecutionException(
                          $"Operation failed. The field execution context for '{field.Route.Path}' was passed " +
                          $"{dataSource.Items.Count} items to resolve but expected 1. (Field Mode: {field.Mode.ToString()})");
            }

            return(next(context, cancelToken));
        }
        /// <summary>
        /// Parses the template contents according to the rules of the template.
        /// </summary>
        public virtual void Parse()
        {
            this.DeclaredArgumentType = this.Parameter.ParameterType;
            this.ObjectType           = GraphValidation.EliminateWrappersFromCoreType(this.Parameter.ParameterType);

            // set the name
            _fieldDeclaration = this.Parameter.SingleAttributeOrDefault <FromGraphQLAttribute>();
            string name = null;

            if (_fieldDeclaration != null)
            {
                name = _fieldDeclaration?.ArgumentName?.Trim();
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                name = Constants.Routing.PARAMETER_META_NAME;
            }

            name       = name.Replace(Constants.Routing.PARAMETER_META_NAME, this.Parameter.Name);
            this.Route = new GraphArgumentFieldPath(this.Parent.Route, name);

            this.Description = this.Parameter.SingleAttributeOrDefault <DescriptionAttribute>()?.Description?.Trim();

            if (this.Parameter.HasDefaultValue && this.Parameter.DefaultValue != null)
            {
                // enums will present their default value as a raw int
                if (this.ObjectType.IsEnum)
                {
                    this.DefaultValue = Enum.ToObject(this.ObjectType, this.Parameter.DefaultValue);
                }
                else
                {
                    this.DefaultValue = this.Parameter.DefaultValue;
                }
            }

            // set appropriate meta data about this parameter for inclusion in the type system
            this.TypeExpression = GraphValidation.GenerateTypeExpression(this.DeclaredArgumentType, this);
            this.TypeExpression = this.TypeExpression.CloneTo(GraphTypeNames.ParseName(this.ObjectType, TypeKind.INPUT_OBJECT));

            // when this argument accepts the same data type as the data returned by its owners target source type
            // i.e. if the source data supplied to the field for resolution is the same as this argument
            // then assume this argument is to contain the source data
            // since the source data will be an OBJECT type (not INPUT_OBJECT) there is no way the user could have supplied it
            if (this.IsSourceDataArgument())
            {
                this.ArgumentModifiers = this.ArgumentModifiers
                                         | GraphArgumentModifiers.ParentFieldResult
                                         | GraphArgumentModifiers.Internal;
            }
        }
        /// <summary>
        /// When overridden in a child class this method builds out the template according to its own individual requirements.
        /// </summary>
        protected override void ParseTemplateDefinition()
        {
            base.ParseTemplateDefinition();

            // parse all input parameters from the method signature
            foreach (var parameter in this.Method.GetParameters())
            {
                var argTemplate = this.CreateGraphFieldArgument(parameter);
                argTemplate.Parse();
                _arguments.Add(argTemplate);
            }

            this.ExpectedReturnType = GraphValidation.EliminateWrappersFromCoreType(
                this.DeclaredReturnType,
                false,
                true,
                false);
        }
Example #12
0
        /// <summary>
        /// Validates the completed field context to ensure it is "correct" against the specification before finalizing its reslts.
        /// </summary>
        /// <param name="context">The context containing the resolved field.</param>
        /// <returns><c>true</c> if the node is valid, <c>false</c> otherwise.</returns>
        public override bool Execute(FieldValidationContext context)
        {
            var dataObject = context.ResultData;

            // did they forget to await a task and accidentally returned Task<T> from their resolver?
            if (dataObject is Task)
            {
                this.ValidationError(
                    context,
                    $"A field resolver for '{context.FieldPath}' yielded an invalid data object. See exception " +
                    "for details. ",
                    new GraphExecutionException(
                        $"The field '{context.FieldPath}' yielded a {nameof(Task)} as its result but expected a value. " +
                        "Did you forget to await an async method?"));

                context.DataItem.InvalidateResult();
            }
            else
            {
                // is the concrete type of the data item not known to the schema?
                var strippedSourceType = GraphValidation.EliminateWrappersFromCoreType(dataObject.GetType());
                var graphType          = context.Schema.KnownTypes.FindGraphType(strippedSourceType);
                if (graphType == null)
                {
                    this.ValidationError(
                        context,
                        $"A field resolver for '{context.Field.Route.Path}' generated a result " +
                        "object type not known to the target schema. See exception for " +
                        "details",
                        new GraphExecutionException(
                            $"The class '{strippedSourceType.FriendlyName()}' is not not mapped to a graph type " +
                            "on the target schema."));

                    context.DataItem.InvalidateResult();
                }
            }

            return(true);
        }
Example #13
0
        /// <summary>
        /// Validates the completed field context to ensure it is "correct" against the specification before finalizing its reslts.
        /// </summary>
        /// <param name="context">The context containing the resolved field.</param>
        /// <returns><c>true</c> if the node is valid, <c>false</c> otherwise.</returns>
        public override bool Execute(FieldValidationContext context)
        {
            var typeExpression = context.TypeExpression;

            if (typeExpression.IsRequired && context.ResultData == null)
            {
                // 6.4.3 section 1c
                this.ValidationError(
                    context,
                    $"Field '{context.FieldPath}' expected a non-null result but received {{null}}.");

                context.DataItem.InvalidateResult();
            }

            // 6.4.3 section 2
            if (context.ResultData == null)
            {
                return(true);
            }

            // 6.4.3 section 3, ensure an IEnumerable for a type expression that is a list
            if (typeExpression.IsListOfItems && !GraphValidation.IsValidListType(context.ResultData.GetType()))
            {
                this.ValidationError(
                    context,
                    $"Field '{context.FieldPath}' was expected to return a list of items but instead returned a single item.");

                context.DataItem.InvalidateResult();
                return(true);
            }

            var graphType = context.Schema?.KnownTypes.FindGraphType(typeExpression?.TypeName);

            if (graphType == null)
            {
                this.ValidationError(
                    context,
                    $"The graph type for field '{context.FieldPath}' ({typeExpression?.TypeName}) does not exist on the target schema. The field" +
                    $"cannot be properly evaluated.");

                context.DataItem.InvalidateResult();
            }
            else if (!typeExpression.Matches(context.ResultData, graphType.ValidateObject))
            {
                // generate a valid, properly cased type expression reference for the data that was provided
                var actualExpression = GraphValidation.GenerateTypeExpression(context.ResultData.GetType());
                var coreType         = GraphValidation.EliminateWrappersFromCoreType(context.ResultData.GetType());
                var actualType       = context.Schema.KnownTypes.FindGraphType(coreType);
                if (actualType != null)
                {
                    actualExpression = actualExpression.CloneTo(actualType.Name);
                }

                // 6.4.3  section 4 & 5
                this.ValidationError(
                    context,
                    $"The resolved value for field '{context.FieldPath}' does not match the required type expression. " +
                    $"Expected {typeExpression} but got {actualExpression}.");

                context.DataItem.InvalidateResult();
            }

            return(true);
        }
        /// <summary>
        /// Validates the completed field context to ensure it is "correct" against the specification before finalizing its reslts.
        /// </summary>
        /// <param name="context">The context containing the resolved field.</param>
        /// <returns><c>true</c> if the node is valid, <c>false</c> otherwise.</returns>
        public override bool Execute(FieldValidationContext context)
        {
            var dataObject             = context.ResultData;
            var dataItemTypeExpression = context.DataItem.TypeExpression;

            // did they forget to await a task and accidentally returned Task<T> from their resolver?
            // put in a special error message for better feed back
            if (dataObject is Task)
            {
                this.ValidationError(
                    context,
                    $"A field resolver for '{context.FieldPath}' yielded an invalid data object. See exception " +
                    "for details. ",
                    new GraphExecutionException(
                        $"The field '{context.FieldPath}' yielded a {nameof(Task)} as its result but expected a value. " +
                        "Did you forget to await an async method?"));

                context.DataItem.InvalidateResult();
                return(true);
            }

            var expectedGraphType = context.Schema?.KnownTypes.FindGraphType(context.Field);

            if (expectedGraphType == null)
            {
                this.ValidationError(
                    context,
                    $"The graph type for field '{context.FieldPath}' ({dataItemTypeExpression?.TypeName}) does not exist on the target schema. The field" +
                    "cannot be properly evaluated.");

                context.DataItem.InvalidateResult();
                return(true);
            }

            // virtual graph types aren't real, they can be safely skipped
            if (expectedGraphType.IsVirtual)
            {
                return(true);
            }

            var rootSourceType = GraphValidation.EliminateWrappersFromCoreType(dataObject.GetType());

            // Check that the actual .NET type of the result data IS (or can be cast to) the expected .NET
            // type for the graphtype of the field being checked
            var analysisResult = context.Schema.KnownTypes.AnalyzeRuntimeConcreteType(expectedGraphType, rootSourceType);

            if (!analysisResult.ExactMatchFound)
            {
                string foundTypeNames;
                if (!analysisResult.FoundTypes.Any())
                {
                    foundTypeNames = "~none~";
                }
                else
                {
                    foundTypeNames = string.Join(", ", analysisResult.FoundTypes.Select(x => x.FriendlyName()));
                }

                this.ValidationError(
                    context,
                    $"A field resolver for '{context.Field.Route.Path}' generated a result " +
                    "object type not known to the target schema. See exception for " +
                    "details",
                    new GraphExecutionException(
                        $"For target field of '{context.FieldPath}' (Graph Type: {expectedGraphType.Name}, Kind: {expectedGraphType.Kind}), a supplied object " +
                        $"of class '{rootSourceType.FriendlyName()}' attempted to fill the request but graphql was not able to determine which " +
                        $"of the matched concrete types to use and cannot resolve the field. Matched Types: [{foundTypeNames}]"));

                context.DataItem.InvalidateResult();
            }
            else
            {
                // once the type is confirmed, confirm the actual value
                // for scenarios such as where a number may be masqurading as an enum but the enum doesn't define
                // a label for said number.
                var isValidValue = expectedGraphType.ValidateObject(dataObject);
                if (!isValidValue)
                {
                    string actual = string.Empty;
                    if (expectedGraphType is ObjectGraphType)
                    {
                        actual = dataObject.GetType().Name;
                    }
                    else
                    {
                        actual = dataObject.ToString();
                    }

                    this.ValidationError(
                        context,
                        $"A resolved value for field '{context.FieldPath}' does not match the required graph type. " +
                        $"Expected '{expectedGraphType.Name}' but got '{actual}'.");

                    context.DataItem.InvalidateResult();
                }
            }

            return(true);
        }
Example #15
0
        /// <summary>
        /// When overridden in a child class this method builds out the template according to its own individual requirements.
        /// </summary>
        protected override void ParseTemplateDefinition()
        {
            _fieldDeclaration = this.SingleAttributeOfTypeOrDefault <GraphFieldAttribute>();

            // ------------------------------------
            // Common Metadata
            // ------------------------------------
            this.Route       = this.GenerateFieldPath();
            this.Mode        = _fieldDeclaration?.ExecutionMode ?? FieldResolutionMode.PerSourceItem;
            this.Complexity  = _fieldDeclaration?.Complexity;
            this.Description = this.SingleAttributeOfTypeOrDefault <DescriptionAttribute>()?.Description;
            var depreciated = this.SingleAttributeOfTypeOrDefault <DeprecatedAttribute>();

            if (depreciated != null)
            {
                this.IsDeprecated      = true;
                this.DeprecationReason = depreciated.Reason?.Trim();
            }

            var objectType     = GraphValidation.EliminateWrappersFromCoreType(this.DeclaredReturnType);
            var typeExpression = GraphValidation.GenerateTypeExpression(this.DeclaredReturnType, this);

            typeExpression = typeExpression.CloneTo(GraphTypeNames.ParseName(objectType, this.Parent.Kind));

            // ------------------------------------
            // Gather Possible Types and/or union definition
            // ------------------------------------
            this.BuildUnionProxyInstance();
            if (this.UnionProxy != null)
            {
                this.PossibleTypes = new List <Type>(this.UnionProxy.Types);
            }
            else
            {
                this.PossibleTypes = new List <Type>();

                // the possible types attribte is optional but expects taht the concrete types are added
                // to the schema else where lest a runtime exception occurs of a missing graph type.
                var typesAttrib = this.SingleAttributeOfTypeOrDefault <PossibleTypesAttribute>();
                if (typesAttrib != null)
                {
                    foreach (var type in typesAttrib.PossibleTypes)
                    {
                        this.PossibleTypes.Add(type);
                    }
                }

                // add any types decalred on the primary field declaration
                if (_fieldDeclaration != null)
                {
                    foreach (var type in _fieldDeclaration.Types)
                    {
                        var strippedType = GraphValidation.EliminateWrappersFromCoreType(type);
                        if (strippedType != null)
                        {
                            this.PossibleTypes.Add(strippedType);
                        }
                    }
                }

                if (objectType != null && !Validation.IsCastable <IGraphActionResult>(objectType) && GraphValidation.IsValidGraphType(objectType))
                {
                    this.PossibleTypes.Insert(0, objectType);
                }
            }

            this.PossibleTypes = this.PossibleTypes.Distinct().ToList();

            // ------------------------------------
            // Adjust the action result to the actual return type this field returns
            // ------------------------------------
            if (Validation.IsCastable <IGraphActionResult>(objectType))
            {
                if (this.UnionProxy != null)
                {
                    // if a union was decalred preserve whatever modifer elements
                    // were decalred but alter the return type to object (a known common element among all members of the union)
                    objectType = typeof(object);
                }
                else if (_fieldDeclaration != null && _fieldDeclaration.Types.Count > 0)
                {
                    objectType     = _fieldDeclaration.Types[0];
                    typeExpression = GraphValidation.GenerateTypeExpression(objectType, this)
                                     .CloneTo(GraphTypeNames.ParseName(objectType, this.Parent.Kind));
                    objectType = GraphValidation.EliminateWrappersFromCoreType(objectType);
                }
                else if (this.PossibleTypes.Count > 0)
                {
                    objectType     = this.PossibleTypes[0];
                    typeExpression = GraphValidation.GenerateTypeExpression(objectType, this)
                                     .CloneTo(GraphTypeNames.ParseName(objectType, this.Parent.Kind));
                    objectType = GraphValidation.EliminateWrappersFromCoreType(objectType);
                }
                else
                {
                    objectType = typeof(object);
                }
            }

            this.ObjectType = objectType;

            if (this.UnionProxy != null)
            {
                this.TypeExpression = typeExpression.CloneTo(this.UnionProxy.Name);
            }
            else
            {
                this.TypeExpression = typeExpression;
            }

            // ------------------------------------
            // Async Requirements
            // ------------------------------------
            this.IsAsyncField = Validation.IsCastable <Task>(this.DeclaredReturnType);

            // ------------------------------------
            // Security Policies
            // ------------------------------------
            _securityPolicies = FieldSecurityGroup.FromAttributeCollection(this.AttributeProvider);
        }
Example #16
0
        /// <summary>
        /// Validates the completed field context to ensure it is "correct" against the specification before finalizing its reslts.
        /// </summary>
        /// <param name="context">The context containing the resolved field.</param>
        /// <returns><c>true</c> if the node is valid, <c>false</c> otherwise.</returns>
        public override bool Execute(FieldValidationContext context)
        {
            var dataObject = context.ResultData;

            // 6.4.3 section 1c
            // This is a quick short cut and customed error message for a common top-level null mismatch.
            var dataItemTypeExpression = context.DataItem.TypeExpression;

            if (dataItemTypeExpression.IsRequired && dataObject == null)
            {
                this.ValidationError(
                    context,
                    $"Field '{context.FieldPath}' expected a non-null result but received {{null}}.");

                context.DataItem.InvalidateResult();
            }

            // 6.4.3 section 2
            if (dataObject == null)
            {
                return(true);
            }

            // 6.4.3 section 3, ensure list type in the result object for a type expression that is a list.
            // This is a quick short cut and customed error message for a common top-level list mismatch.
            if (dataItemTypeExpression.IsListOfItems && !GraphValidation.IsValidListType(dataObject.GetType()))
            {
                this.ValidationError(
                    context,
                    $"Field '{context.FieldPath}' was expected to return a list of items but instead returned a single item.");

                context.DataItem.InvalidateResult();
                return(true);
            }

            var expectedGraphType = context.Schema?.KnownTypes.FindGraphType(context.Field);

            if (expectedGraphType == null)
            {
                this.ValidationError(
                    context,
                    $"The graph type for field '{context.FieldPath}' ({dataItemTypeExpression?.TypeName}) does not exist on the target schema. The field" +
                    "cannot be properly evaluated.");

                context.DataItem.InvalidateResult();
                return(true);
            }

            // Perform a deep check of the meta-type chain (list and nullability wrappers) against the result data.
            // For example, if the type expression is [[SomeType]] ensure the result object is List<List<T>> etc.)
            // however, use the type name of the actual data object, not the graph type itself
            // we only want to check the type expression wrappers in this step
            var rootSourceType        = GraphValidation.EliminateWrappersFromCoreType(dataObject.GetType());
            var mangledTypeExpression = dataItemTypeExpression.CloneTo(rootSourceType.Name);

            if (!mangledTypeExpression.Matches(dataObject))
            {
                // generate a valid, properly cased type expression reference for the data that was provided
                var actualExpression = GraphValidation.GenerateTypeExpression(context.ResultData.GetType());

                // Fake the type expression against the real graph type
                // this step only validates the meta graph types the actual type may be different (but castable to the concrete
                // type of the graphType). Don't confuse the user in this step.
                actualExpression = actualExpression.CloneTo(expectedGraphType.Name);

                // 6.4.3  section 4 & 5
                this.ValidationError(
                    context,
                    $"The resolved value for field '{context.FieldPath}' does not match the required type expression. " +
                    $"Expected {dataItemTypeExpression} but got {actualExpression}.");

                context.DataItem.InvalidateResult();
            }

            return(true);
        }