예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QueryDocument" /> class.
        /// </summary>
        /// <param name="messages">The messages to preload to the document.</param>
        /// <param name="operations">the operations to preload to the document.</param>
        /// <param name="maxDepth">The maximum depth achived by the document.</param>
        public QueryDocument(IGraphMessageCollection messages = null, IEnumerable <QueryOperation> operations = null, int maxDepth = 0)
        {
            this.MaxDepth   = maxDepth;
            this.Operations = new QueryOperationCollection();
            this.Messages   = new GraphMessageCollection();

            this.Operations.AddRange(operations);
            this.Messages.AddRange(messages);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FieldContextBuilder" /> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="user">The user.</param>
        /// <param name="graphField">The graph field.</param>
        /// <param name="schema">The schema.</param>
        /// <param name="graphMethod">The metadata describing the method/functon to be invoked by a resolver.</param>
        public FieldContextBuilder(
            IServiceProvider serviceProvider,
            ClaimsPrincipal user,
            IGraphField graphField,
            ISchema schema,
            IGraphMethod graphMethod)
        {
            _schema            = Validation.ThrowIfNullOrReturn(schema, nameof(schema));
            _graphField        = Validation.ThrowIfNullOrReturn(graphField, nameof(graphField));
            _user              = Validation.ThrowIfNullOrReturn(user, nameof(user));
            _messageCollection = new GraphMessageCollection();

            this.ServiceProvider = Validation.ThrowIfNullOrReturn(serviceProvider, nameof(serviceProvider));

            Type expectedInputType = null;

            if (!Validation.IsCastable <GraphDirective>(graphMethod.Parent.ObjectType) &&
                !Validation.IsCastable <GraphController>(graphMethod.Parent.ObjectType))
            {
                expectedInputType = graphMethod.Parent.ObjectType;
            }

            var metaData = new MetaDataCollection();

            _mockRequest           = new Mock <IGraphFieldRequest>();
            _mockInvocationContext = new Mock <IGraphFieldInvocationContext>();

            // fake the request for the field data (normally generated by the primary query exeuction context)
            var id = Guid.NewGuid().ToString("N");

            _mockRequest.Setup(x => x.Id).Returns(id);
            _mockRequest.Setup(x => x.Origin).Returns(SourceOrigin.None);
            _mockRequest.Setup(x => x.Items).Returns(metaData);
            _mockRequest.Setup(x => x.Field).Returns(_graphField);
            _mockRequest.Setup(x => x.InvocationContext).Returns(_mockInvocationContext.Object);

            _mockInvocationContext.Setup(x => x.ExpectedSourceType).Returns(expectedInputType);
            _mockInvocationContext.Setup(x => x.Field).Returns(_graphField);
            _mockInvocationContext.Setup(x => x.Arguments).Returns(_arguments);
            _mockInvocationContext.Setup(x => x.Name).Returns(_graphField.Name);
            _mockInvocationContext.Setup(x => x.Directives).Returns(new List <IDirectiveInvocationContext>());
            _mockInvocationContext.Setup(x => x.ChildContexts).Returns(new FieldInvocationContextCollection());
            _mockInvocationContext.Setup(x => x.Origin).Returns(SourceOrigin.None);

            this.GraphMethod = new Mock <IGraphMethod>();
            this.GraphMethod.Setup(x => x.Parent).Returns(graphMethod.Parent);
            this.GraphMethod.Setup(x => x.ObjectType).Returns(graphMethod.ObjectType);
            this.GraphMethod.Setup(x => x.ExpectedReturnType).Returns(graphMethod.ExpectedReturnType);
            this.GraphMethod.Setup(x => x.Method).Returns(graphMethod.Method);
            this.GraphMethod.Setup(x => x.IsAsyncField).Returns(graphMethod.IsAsyncField);
            this.GraphMethod.Setup(x => x.Name).Returns(graphMethod.Name);
            this.GraphMethod.Setup(x => x.InternalFullName).Returns(graphMethod.InternalFullName);
            this.GraphMethod.Setup(x => x.InternalName).Returns(graphMethod.InternalName);
            this.GraphMethod.Setup(x => x.Route).Returns(graphMethod.Route);
            this.GraphMethod.Setup(x => x.Arguments).Returns(graphMethod.Arguments);
        }
        /// <summary>
        /// Creates a new operation result from a collection of generated messages and optional raw data
        /// provided by a requestor.
        /// </summary>
        /// <param name="errorMessages">The collection of messages. Must be not null and contain at least one message.</param>
        /// <param name="queryData">The original query data.</param>
        /// <returns>GraphOperationResult.</returns>
        public static GraphOperationResult FromMessages(IGraphMessageCollection errorMessages, GraphQueryData queryData = null)
        {
            Validation.ThrowIfNull(errorMessages, nameof(errorMessages));
            if (errorMessages.Count < 1)
            {
                errorMessages.Critical("An unknown error occured.");
            }

            return(new GraphOperationResult(
                       new GraphOperationRequest(queryData),
                       errorMessages));
        }
예제 #4
0
        /// <summary>
        /// Creates an executable operation context from the given query document operation. This method
        /// marries the parsed requirements from a schema to the concrete method and property implementations
        /// in the user's sourcecode. All abstract graph types are materialized and only concrete, actionable methods
        /// will be be operationalized.
        /// </summary>
        /// <param name="operation">The query operation to generate an execution context for.</param>
        /// <returns>Task&lt;IGraphFieldExecutableOperation&gt;.</returns>
        public async Task <IGraphFieldExecutableOperation> Create(QueryOperation operation)
        {
            Validation.ThrowIfNull(operation, nameof(operation));
            _messages = new GraphMessageCollection();

            var topLevelFields = await this.CreateContextsForFieldSelectionSet(
                operation.GraphType,
                operation.FieldSelectionSet).ConfigureAwait(false);

            var result = new GraphFieldExecutableOperation(operation);

            foreach (var field in topLevelFields)
            {
                result.FieldContexts.Add(field);
            }

            result.Messages.AddRange(_messages);
            return(result);
        }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FieldValidationContext" /> class.
 /// </summary>
 /// <param name="schema">The schema in scope for this validation context.</param>
 /// <param name="dataItem">The data item to process.</param>
 /// <param name="messageCollection">An optional message collection to use for this context, if not supplied
 /// a new one will be generated.</param>
 public FieldValidationContext(ISchema schema, GraphDataItem dataItem, IGraphMessageCollection messageCollection = null)
 {
     this.Schema   = Validation.ThrowIfNullOrReturn(schema, nameof(schema));
     this.DataItem = dataItem;
     this.Messages = messageCollection ?? new GraphMessageCollection();
 }