示例#1
0
        private async Task <object> CompleteNonNullValueAsync(
            IExecutorContext executorContext,
            ObjectType objectType,
            IField field,
            ObjectType actualType,
            GraphQLFieldSelection selection,
            List <GraphQLFieldSelection> fields,
            object value, Dictionary <string, object> coercedVariableValues,
            NodePath path,
            NonNull nonNull)
        {
            var innerType       = nonNull.WrappedType;
            var completedResult = await CompleteValueAsync(
                executorContext,
                objectType,
                field,
                innerType,
                actualType,
                selection,
                fields,
                value,
                coercedVariableValues,
                path).ConfigureAwait(false);

            if (completedResult == null)
            {
                throw new NullValueForNonNullException(
                          objectType.Name,
                          selection.Name.Value,
                          path,
                          selection);
            }

            return(completedResult);
        }
示例#2
0
        public virtual async Task <object> CompleteValueAsync(IExecutorContext executorContext,
                                                              ObjectType objectType,
                                                              IField field,
                                                              IType fieldType,
                                                              GraphQLFieldSelection selection,
                                                              List <GraphQLFieldSelection> fields,
                                                              Dictionary <string, object> coercedVariableValues,
                                                              NodePath path)
        {
            object completedValue = null;

            completedValue = await CompleteValueAsync(
                executorContext,
                objectType,
                field,
                fieldType,
                ActualType,
                selection,
                fields,
                Value,
                coercedVariableValues,
                path).ConfigureAwait(false);

            return(completedValue);
        }
示例#3
0
        public static IReadOnlyDictionary <string, object> CoerceArgumentValues(
            ISchema schema,
            ObjectType objectType,
            GraphQLFieldSelection field,
            IReadOnlyDictionary <string, object> coercedVariableValues)
        {
            var coercedValues = new Dictionary <string, object>();

            var argumentValues      = field.Arguments?.ToList() ?? new List <GraphQLArgument>();
            var fieldName           = field.Name.Value;
            var argumentDefinitions = schema.GetField(objectType.Name, fieldName).Arguments;

            if (argumentDefinitions == null)
            {
                return(coercedValues);
            }

            foreach (var argumentDefinitionPair in argumentDefinitions)
            {
                var argumentDefinition = argumentDefinitionPair.Value;
                var argumentName       = argumentDefinitionPair.Key;
                var argument           = argumentValues.SingleOrDefault(a => a.Name.Value == argumentName);
                coercedValues[argumentName] = CoerceArgumentValue(
                    schema,
                    coercedVariableValues,
                    argumentName,
                    argumentDefinition,
                    argument);
            }

            return(coercedValues);
        }
示例#4
0
        private IEdmEntitySet GetEntitySet(GraphQLFieldSelection selection)
        {
            var  listGraphType = (ListGraphType)_schema.Query.Fields.Single(f => f.Name == selection.Name.Value).ResolvedType;
            Type entityType    = listGraphType.ResolvedType.GetType().GetGenericArguments()[0];

            return(OeEdmClrHelper.GetEntitySet(_edmModel, entityType));
        }
示例#5
0
        public override GraphQLFieldSelection BeginVisitFieldSelection(GraphQLFieldSelection selection)
        {
            var type = this.GetUnderlyingType(this.GetLastType());

            if (type != null)
            {
                var fieldName = selection.Name.Value;
                var fieldDef  = this.GetField(type, fieldName);

                if (fieldDef == null)
                {
                    var suggestedTypeNames  = this.GetSuggestedTypeNames(this.Schema, type, fieldName);
                    var suggestedFieldNames = suggestedTypeNames.Any()
                        ? Enumerable.Empty <string>()
                        : this.GetSuggestedFieldNames(this.Schema, type, fieldName);

                    var errorMessage = this.ComposeErrorMessage(
                        fieldName,
                        type.Name,
                        suggestedTypeNames,
                        suggestedFieldNames);

                    this.Errors.Add(new GraphQLException(errorMessage, new[] { selection }));
                }
            }

            return(base.BeginVisitFieldSelection(selection));
        }
示例#6
0
        public static object Handle(
            IExecutorContext context,
            ObjectType objectType,
            string fieldName,
            IType fieldType,
            GraphQLFieldSelection fieldSelection,
            object completedValue,
            Exception error,
            NodePath path)
        {
            if (!(error is QueryExecutionException))
            {
                error = new QueryExecutionException(
                    "",
                    error,
                    path,
                    fieldSelection);
            }

            if (fieldType is NonNull)
            {
                throw error;
            }

            context.AddError(error);
            return(completedValue);
        }
        public override GraphQLFieldSelection BeginVisitFieldSelection(GraphQLFieldSelection selection)
        {
            this.fieldStack.Push(selection.Name.Value);

            this.argumentTypes.Clear();
            if (selection.Name?.Value != null)
            {
                var arguments = this.translator.GetObjectTypeTranslatorFor(this.typeStack.Peek()).GetField(selection.Name.Value).Arguments;
                if (arguments != null)
                {
                    foreach (var argument in this.translator.GetObjectTypeTranslatorFor(this.typeStack.Peek()).GetField(selection.Name.Value).Arguments)
                    {
                        this.argumentTypes.Add(argument.Key, argument.Value);
                    }
                }
            }

            var obj = this.translator.GetObjectTypeTranslatorFor(this.typeStack.Peek()).GetField(selection.Name.Value).Type as GraphQLObjectType;

            if (obj != null)
            {
                this.typeStack.Push(obj);
            }

            return(base.BeginVisitFieldSelection(selection));
        }
示例#8
0
        private async Task RegisterSubscription(
            GraphQLFieldSelection fieldSelection,
            GraphQLSubscriptionType type,
            GraphQLDocument document,
            FieldScope scope)
        {
            var fieldInfo = type.GetFieldInfo(fieldSelection.Name.Value) as GraphQLSubscriptionTypeFieldInfo;

            Expression <Func <object, bool> > filter = null;

            if (fieldInfo.Filter != null)
            {
                filter = entity => (bool)scope.InvokeWithArgumentsSync(
                    fieldSelection.Arguments.ToList(), fieldInfo.Filter, entity);
            }

            await type.EventBus.Subscribe(EventBusSubscription.Create(
                                              fieldInfo.Channel,
                                              this.clientId,
                                              this.subscriptionId.Value,
                                              this.Operation?.Name?.Value ?? "Anonymous",
                                              this.variables,
                                              filter,
                                              this.ast));
        }
示例#9
0
        private async Task <object> CompleteNonNullValueAsync(
            IExecutorContext executorContext,
            ObjectType objectType,
            IField field,
            ObjectType actualType,
            GraphQLFieldSelection selection,
            IReadOnlyCollection <GraphQLFieldSelection> fields,
            object value,
            NodePath path,
            NonNull nonNull)
        {
            var innerType       = nonNull.OfType;
            var completedResult = await CompleteValueAsync(
                executorContext,
                objectType,
                field,
                innerType,
                actualType,
                selection,
                fields,
                value,
                path).ConfigureAwait(false);

            if (completedResult == null)
            {
                throw new NullValueForNonNullException(
                          objectType.Name,
                          selection.Name.Value,
                          path,
                          selection);
            }

            return(completedResult);
        }
示例#10
0
        public override GraphQLFieldSelection BeginVisitFieldSelection(
            GraphQLFieldSelection selection)
        {
            Tracker.EnterFieldSelection?.Invoke(selection);

            return(base.BeginVisitFieldSelection(selection));
        }
示例#11
0
        private object CompleteValue(object input, GraphQLFieldSelection selection, IList <GraphQLArgument> arguments)
        {
            if (input == null)
            {
                return(null);
            }

            if (input is GraphQLObjectType)
            {
                return(this.CompleteObjectType((GraphQLObjectType)input, selection, arguments, this.parent));
            }

            if (ReflectionUtilities.IsCollection(input.GetType()))
            {
                return(this.CompleteCollectionType((IEnumerable)input, selection, arguments));
            }

            var schemaValue = this.typeTranslator.GetType(input.GetType());

            if (schemaValue is GraphQLObjectType)
            {
                return(this.CompleteObjectType((GraphQLObjectType)schemaValue, selection, arguments, input));
            }

            if (ReflectionUtilities.IsEnum(input.GetType()))
            {
                return(input.ToString());
            }

            return(input);
        }
        public override GraphQLFieldSelection EndVisitFieldSelection(GraphQLFieldSelection selection)
        {
            this.fieldStack.Pop();
            this.argumentTypes.Clear();

            return(base.EndVisitFieldSelection(selection));
        }
        public override GraphQLFieldSelection EndVisitFieldSelection(GraphQLFieldSelection selection)
        {
            this.fieldStack.Pop();
            this.typeStack.Pop();

            return(base.EndVisitFieldSelection(selection));
        }
示例#14
0
        public virtual GraphQLFieldSelection BeginVisitFieldSelection(GraphQLFieldSelection selection)
        {
            BeginVisitNode(selection.Name);

            if (selection.Alias != null)
            {
                BeginVisitAlias((GraphQLName)BeginVisitNode(selection.Alias));
            }

            if (selection.Arguments != null)
            {
                BeginVisitArguments(selection.Arguments);
            }

            if (selection.SelectionSet != null)
            {
                BeginVisitNode(selection.SelectionSet);
            }

            if (selection.Directives != null)
            {
                BeginVisitDirectives(selection.Directives);
            }

            return(EndVisitFieldSelection(selection));
        }
示例#15
0
        private BinaryOperatorNode BuildFilterExpression(SingleResourceNode source, GraphQLFieldSelection selection)
        {
            BinaryOperatorNode compositeNode = null;
            IEdmEntityType     entityType    = source.NavigationSource.EntityType();

            foreach (GraphQLArgument argument in selection.Arguments)
            {
                IEdmProperty edmProperty = FindEdmProperty(entityType, argument.Name.Value);
                var          left        = new SingleValuePropertyAccessNode(source, edmProperty);

                Object value = GetArgumentValue(edmProperty.Type, argument.Value);
                var    right = new ConstantNode(value, ODataUriUtils.ConvertToUriLiteral(value, ODataVersion.V4));
                var    node  = new BinaryOperatorNode(BinaryOperatorKind.Equal, left, right);
                compositeNode = ComposeExpression(compositeNode, node);
            }

            //foreach (ASTNode astNode in selection.SelectionSet.Selections)
            //    if (astNode is GraphQLFieldSelection fieldSelection && fieldSelection.SelectionSet != null)
            //    {
            //        var navigationProperty = (IEdmNavigationProperty)FindEdmProperty(entityType, fieldSelection.Name.Value);
            //        if (navigationProperty.Type is IEdmCollectionTypeReference)
            //            continue;

            //        var parentSource = new SingleNavigationNode(source, navigationProperty, null);
            //        BinaryOperatorNode node = BuildFilterExpression(parentSource, fieldSelection);
            //        compositeNode = ComposeExpression(compositeNode, node);
            //    }

            return(compositeNode);
        }
示例#16
0
 private void AddToResultDictionaryIfNotAlreadyPresent(
     IDictionary <string, object> dictionary, string fieldName, GraphQLFieldSelection selection)
 {
     if (!dictionary.ContainsKey(fieldName))
     {
         dictionary.Add(fieldName, this.GetDefinitionAndExecuteField(this.type, selection));
     }
 }
示例#17
0
        private object GetDefinitionAndExecuteField(GraphQLObjectType type, GraphQLFieldSelection selection)
        {
            var arguments     = this.GetArgumentsFromSelection(selection);
            var fieldInfo     = this.GetFieldInfo(type, selection);
            var resolvedValue = this.ResolveField(fieldInfo, arguments, this.parent);

            return(this.CompleteValue(resolvedValue, selection, arguments));
        }
示例#18
0
        private List <GraphQLArgument> GetArgumentsFromSelection(GraphQLFieldSelection selection)
        {
            var arguments = this.arguments.ToList();

            arguments.RemoveAll(e => selection.Arguments.Any(arg => arg.Name.Value.Equals(e.Name.Value)));
            arguments.AddRange(selection.Arguments);

            return(arguments);
        }
示例#19
0
        private async Task <object> CompleteListValueAsync(
            IExecutorContext executorContext,
            ObjectType objectType,
            IField field,
            IType fieldType,
            ObjectType actualType,
            GraphQLFieldSelection selection,
            IReadOnlyCollection <GraphQLFieldSelection> fields,
            object value,
            NodePath path,
            List listType)
        {
            if (!(value is IEnumerable values))
            {
                throw new CompleteValueException(
                          $"Cannot complete value for list field '{selection.Name.Value}':'{fieldType}'. " +
                          "Resolved value is not a collection",
                          path,
                          selection);
            }

            var innerType = listType.OfType;
            var result    = new List <object>();
            int i         = 0;

            foreach (var resultItem in values)
            {
                var itemPath = path.Fork().Append(i++);
                try
                {
                    var completedResultItem = await CompleteValueAsync(
                        executorContext,
                        objectType,
                        field,
                        innerType,
                        actualType,
                        selection,
                        fields,
                        resultItem,
                        itemPath).ConfigureAwait(false);

                    result.Add(completedResultItem);
                }
                catch (Exception e)
                {
                    if (innerType is NonNull)
                    {
                        throw;
                    }

                    executorContext.AddError(e);
                    result.Add(null);
                }
            }

            return(result);
        }
示例#20
0
        private List <GraphQLArgument> GetArgumentsFromSelection(GraphQLFieldSelection selection, GraphQLFieldInfo fieldInfo)
        {
            if (fieldInfo == null || fieldInfo.Arguments?.Count == 0)
            {
                return(null);
            }

            return(this.MergeArgumentsWithDefault(selection.Arguments, fieldInfo.Arguments));
        }
示例#21
0
        public override GraphQLFieldSelection EndVisitFieldSelection(
            GraphQLFieldSelection selection)
        {
            {
                Tracker.LeaveFieldSelection?.Invoke(selection);
            }

            return(base.EndVisitFieldSelection(selection));
        }
示例#22
0
 public ResolverContextFacts()
 {
     _objectType  = new ObjectType("Test");
     _objectValue = null;
     _field       = new Field(ScalarType.ID);
     _selection   = new GraphQLFieldSelection();
     _schema      = new SchemaBuilder()
                    .Query(out _)
                    .Build();
 }
        public Field Field(GraphQLFieldSelection source)
        {
            var alias = source.Alias != null?Name(source.Alias) : null;

            var field = new Field(alias, Name(source.Name)).WithLocation(source, _body);

            field.Arguments    = Arguments(source.Arguments);
            field.Directives   = Directives(source.Directives);
            field.SelectionSet = SelectionSet(source.SelectionSet);
            return(field);
        }
示例#24
0
        private object CompleteCollectionType(IEnumerable input, GraphQLFieldSelection selection, IList <GraphQLArgument> arguments)
        {
            var result = new List <object>();

            foreach (var element in input)
            {
                result.Add(this.CompleteValue(element, selection, arguments));
            }

            return(result);
        }
示例#25
0
        public FieldSelection(GraphQLFieldSelection op)
        {
            this.op = op;
            Name    = op.Alias?.Value ?? op.Name.Value;
            if (op.SelectionSet != null)
            {
                Selection = new SetSelection(op.SelectionSet);
            }

            // to deduplocate part sof
        }
示例#26
0
        private IEdmEntitySet GetEntitySet(GraphQLFieldSelection selection)
        {
            foreach (FieldType fieldType in _context.Schema.Query.Fields)
            {
                if (String.Compare(fieldType.Name, selection.Name.Value, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return(OeEdmClrHelper.GetEntitySet(_edmModel, fieldType.Name));
                }
            }

            throw new InvalidOperationException("Field name " + selection.Name.Value + " not found in schema query");
        }
示例#27
0
        private async Task <object> TryResolveNull(object input, Type inputType, GraphQLFieldSelection selection, IEnumerable <object> path)
        {
            return(await Task.Run(() =>
            {
                if (input == null)
                {
                    return (object)null;
                }

                return INVALID_RESULT;
            }));
        }
示例#28
0
        private async Task <object> TryResolveUnion(object input, Type inputType, GraphQLFieldSelection selection, IEnumerable <object> path)
        {
            if (ReflectionUtilities.IsDescendant(inputType, typeof(GraphQLUnionType)))
            {
                var unionSchemaType = this.context.SchemaRepository.GetSchemaTypeFor(inputType) as GraphQLUnionType;
                input = await this.CompleteValue(input, unionSchemaType.ResolveType(input), selection, this.arguments, path);

                return(input);
            }

            return(INVALID_RESULT);
        }
        private bool HaveSameArguments(GraphQLFieldSelection selection1, GraphQLFieldSelection selection2)
        {
            if (selection1.Arguments.Count() != selection2.Arguments.Count())
            {
                return(false);
            }

            return(selection1.Arguments
                   .All(arg1 => selection2.Arguments
                        .Any(arg2 => arg1.Name.Value == arg2.Name.Value &&
                             arg1.Value.ToString() == arg2.Value.ToString())));
        }
示例#30
0
        private FilterClause BuildFilterClause(IEdmEntitySet entitySet, GraphQLFieldSelection selection)
        {
            ResourceRangeVariable resourceVariable = GetResorceVariable(entitySet);
            var resourceNode = new ResourceRangeVariableReferenceNode("", resourceVariable);
            BinaryOperatorNode filterExpression = BuildFilterExpression(resourceNode, selection);

            if (filterExpression == null)
            {
                return(null);
            }

            return(new FilterClause(filterExpression, resourceVariable));
        }