Beispiel #1
0
        public PreparedSelection(
            IObjectType declaringType,
            IObjectField field,
            FieldNode selection,
            FieldDelegate resolverPipeline,
            NameString?responseName = null,
            IReadOnlyDictionary <NameString, PreparedArgument>?arguments = null,
            SelectionIncludeCondition?includeCondition = null,
            bool internalSelection = false)
        {
            DeclaringType = declaringType
                            ?? throw new ArgumentNullException(nameof(declaringType));
            Field = field
                    ?? throw new ArgumentNullException(nameof(field));
            Selection = selection
                        ?? throw new ArgumentNullException(nameof(selection));
            ResponseName = responseName ??
                           (selection.Alias is null
                    ? selection.Name.Value
                    : selection.Alias.Value);
            ResolverPipeline = resolverPipeline ??
                               throw new ArgumentNullException(nameof(resolverPipeline));
            Arguments = arguments is null
                ? _emptyArguments
                : new PreparedArgumentMap(arguments);
            Selections    = _emptySelections;
            InclusionKind = internalSelection
                ? SelectionInclusionKind.Internal
                : SelectionInclusionKind.Always;

            if (includeCondition is { })
 private MappingElement CreateTargetElement(SyntaxNode globalTargetAccessor, IObjectField target, SyntaxGenerator syntaxGenerator)
 {
     return(new MappingElement
     {
         Expression = (ExpressionSyntax)CreateAccessPropertyExpression(globalTargetAccessor, target, syntaxGenerator),
         ExpressionType = target.Type
     });
 }
        protected virtual ISelectionVisitorAction VisitChildren(
            ISelection selection,
            TContext context)
        {
            IObjectField field = selection.Field;

            return(Visit(field, context));
        }
Beispiel #4
0
        private static bool IsMatched(string targetName, IObjectField source, string sourceAccessPrefix)
        {
            var sanitizedName           = SanitizeName(source.Name);
            var sanitizedTargetName     = SanitizeName(targetName);
            var sanitizedNameWithPrefix = SanitizeName($"{sourceAccessPrefix}{source.Name}");

            return(sanitizedName.Equals(sanitizedTargetName, StringComparison.OrdinalIgnoreCase) || sanitizedNameWithPrefix.Equals(sanitizedTargetName, StringComparison.OrdinalIgnoreCase));
        }
        public static string GetPropertyName(this IObjectField field)
        {
            if (field.Name.Value.Length == 1)
            {
                return(field.Name.Value.ToUpperInvariant());
            }

            return(field.Name.Value.Substring(0, 1).ToUpperInvariant() +
                   field.Name.Value.Substring(1));
        }
        /// <inheritdoc/>
        public override bool TryHandleEnter(
            MongoDbProjectionVisitorContext context,
            ISelection selection,
            [NotNullWhen(true)] out ISelectionVisitorAction?action)
        {
            IObjectField field = selection.Field;

            context.Path.Push(field.GetName());
            action = SelectionVisitor.Continue;
            return(true);
        }
Beispiel #7
0
        public void Accept(IObjectField field)
        {
            IOutputType      type         = field.Type;
            SelectionSetNode?selectionSet = Context.FieldSelection.SelectionSet;

            (type, selectionSet) = UnwrapPaging(type, selectionSet);
            IType elementType = type.IsListType() ? type.ElementType() : type;

            Closures.Push(new SelectionClosure(elementType.ToRuntimeType(), "e"));
            VisitSelections(type, selectionSet);
        }
Beispiel #8
0
        private PureFieldDelegate?TryCreatePureField(
            IObjectField field,
            FieldNode selection)
        {
            if (field.PureResolver is not null && selection.Directives.Count == 0)
            {
                return(field.PureResolver);
            }

            return(null);
        }
        public static string GetName(
            this IObjectField field)
        {
            string fieldName = field.Name;

            if (field.Member is { } p)
            {
                fieldName = p.Name;
            }

            return(fieldName);
        }
Beispiel #10
0
 public static GraphQLException OffsetPagingHandler_NoBoundariesSet(
     IObjectField field,
     Path path)
 => new GraphQLException(
     ErrorBuilder.New()
     .SetMessage(
         "You must provide take to properly paginate the `{0}`.",
         field.Type.NamedType().Name.Value)
     .SetCode(ErrorCodes.Paging.NoPagingBoundaries)
     .SetPath(path)
     .SetSyntaxNode(field.SyntaxNode)
     .SetExtension(nameof(field), field.Coordinate.ToString())
     .Build());
Beispiel #11
0
 public static GraphQLException PagingHandler_NoBoundariesSet(
     IObjectField field,
     Path path)
 => new GraphQLException(
     ErrorBuilder.New()
     .SetMessage(
         ThrowHelper_PagingHandler_NoBoundariesSet,
         field.Type.NamedType().Name.Value)
     .SetCode(ErrorCodes.Paging.NoPagingBoundaries)
     .SetPath(path)
     .SetSyntaxNode(field.SyntaxNode)
     .SetExtension(nameof(field), field.Coordinate.ToString())
     .Build());
        /// <inheritdoc/>
        public override bool TryHandleEnter(
            Neo4JProjectionVisitorContext context,
            ISelection selection,
            out ISelectionVisitorAction action)
        {
            IObjectField field = selection.Field;

            action = SelectionVisitor.SkipAndLeave;

            if (!context.StartNodes.Any())
            {
                context.Projections.Add(field.GetName());
                return(true);
            }

            if (context.StartNodes.Count != context.EndNodes.Count)
            {
                context.EndNodes.Push(Cypher.NamedNode(selection.DeclaringType.Name.Value));
            }

            if (context.StartNodes.Count != context.Relationships.Count)
            {
                Neo4JRelationshipAttribute rel = context.RelationshipTypes.Peek();
                Node startNode = context.StartNodes.Peek();
                Node endNode   = context.EndNodes.Peek();


                Relationship direction = rel.Direction switch
                {
                    Incoming => startNode.RelationshipFrom(endNode, rel.Name),

                    Outgoing => startNode.RelationshipTo(endNode, rel.Name),

                    None => startNode.RelationshipBetween(endNode, rel.Name),

                    _ => throw new InvalidOperationException(
                              Neo4JResources.Projection_RelationshipDirectionNotSet)
                };

                context.Relationships.Push(direction);
            }

            context
            .RelationshipProjections[context.CurrentLevel]
            .Enqueue(selection.Field.GetName());

            return(true);
        }
    }
        /// <inheritdoc/>
        public override bool TryHandleEnter(
            MongoDbProjectionVisitorContext context,
            ISelection selection,
            out ISelectionVisitorAction?action)
        {
            IObjectField field = selection.Field;

            context.Path.Push(field.GetName());
            context.Projections.Push(
                new MongoDbIncludeProjectionOperation(context.GetPath()));
            context.Path.Pop();

            action = SelectionVisitor.SkipAndLeave;
            return(true);
        }
Beispiel #14
0
 private void CollectQueryDirectives(
     HashSet <string> processed,
     List <IDirective> directives,
     IObjectField field,
     FieldNode selection)
 {
     foreach (IDirective directive in GetFieldSelectionDirectives(field, selection))
     {
         if (!directive.Type.IsRepeatable && !processed.Add(directive.Name))
         {
             directives.Remove(directives.First(t => t.Type == directive.Type));
         }
         directives.Add(directive);
     }
 }
Beispiel #15
0
 public static GraphQLException PagingHandler_MaxPageSize(
     int requestedItems,
     int maxAllowedItems,
     IObjectField field,
     Path path)
 => new GraphQLException(
     ErrorBuilder.New()
     .SetMessage(ThrowHelper_PagingHandler_MaxPageSize)
     .SetCode(ErrorCodes.Paging.MaxPaginationItems)
     .SetPath(path)
     .SetSyntaxNode(field.SyntaxNode)
     .SetExtension(nameof(field), field.Coordinate.ToString())
     .SetExtension(nameof(requestedItems), requestedItems)
     .SetExtension(nameof(maxAllowedItems), maxAllowedItems)
     .Build());
Beispiel #16
0
 private static void CollectTypeSystemDirectives(
     HashSet <string> processed,
     List <IDirective> directives,
     IObjectField field)
 {
     for (var i = 0; i < field.ExecutableDirectives.Count; i++)
     {
         IDirective directive = field.ExecutableDirectives[i];
         if (!directive.Type.IsRepeatable && !processed.Add(directive.Name))
         {
             directives.Remove(directives.First(t => t.Type == directive.Type));
         }
         directives.Add(directive);
     }
 }
Beispiel #17
0
        private FieldDelegate CreateFieldMiddleware(IObjectField field, FieldNode selection)
        {
            FieldDelegate pipeline = field.Middleware;

            if (field.ExecutableDirectives.Count > 0 || selection.Directives.Count > 0)
            {
                IReadOnlyList <IDirective> directives = CollectDirectives(field, selection);

                if (directives.Count > 0)
                {
                    pipeline = Compile(pipeline, directives);
                }
            }

            return(pipeline);
        }
Beispiel #18
0
        private IReadOnlyDictionary <NameString, ArgumentValue>?CoerceArgumentValues(
            IObjectField field,
            FieldNode selection,
            string responseName)
        {
            if (field.Arguments.Count == 0)
            {
                return(null);
            }

            var arguments = new Dictionary <NameString, ArgumentValue>();

            for (var i = 0; i < selection.Arguments.Count; i++)
            {
                ArgumentNode argumentValue = selection.Arguments[i];
                if (field.Arguments.TryGetField(
                        argumentValue.Name.Value,
                        out IInputField? argument))
                {
                    arguments[argument.Name.Value] =
                        CreateArgumentValue(
                            responseName,
                            argument,
                            argumentValue,
                            argumentValue.Value,
                            false);
                }
            }

            for (var i = 0; i < field.Arguments.Count; i++)
            {
                IInputField argument = field.Arguments[i];
                if (!arguments.ContainsKey(argument.Name))
                {
                    arguments[argument.Name.Value] =
                        CreateArgumentValue(
                            responseName,
                            argument,
                            null,
                            argument.DefaultValue ?? NullValueNode.Default,
                            true);
                }
            }

            return(arguments);
        }
Beispiel #19
0
 private IEnumerable <IDirective> GetFieldSelectionDirectives(
     IObjectField field,
     FieldNode selection)
 {
     for (var i = 0; i < selection.Directives.Count; i++)
     {
         DirectiveNode directive = selection.Directives[i];
         if (_schema.TryGetDirectiveType(directive.Name.Value,
                                         out DirectiveType? directiveType) &&
             directiveType.HasMiddleware)
         {
             yield return(Directive.FromDescription(
                              directiveType,
                              new DirectiveDefinition(directive),
                              field));
         }
     }
 }
Beispiel #20
0
            public void Optimize(SelectionSetOptimizerContext context)
            {
                if (context.FieldContext.TryPeek(out IObjectField field) &&
                    field.Name.Equals("bar"))
                {
                    IObjectField  baz          = context.TypeContext.Fields["baz"];
                    FieldNode     bazSelection = Utf8GraphQLParser.Syntax.ParseField("baz { text }");
                    FieldDelegate bazPipeline  = context.CompileResolverPipeline(baz, bazSelection);

                    var compiledSelection = new PreparedSelection(
                        context.TypeContext,
                        baz,
                        bazSelection,
                        bazPipeline,
                        internalSelection: true);

                    context.Fields[compiledSelection.ResponseName] = compiledSelection;
                }
            }
Beispiel #21
0
        private IReadOnlyList <IDirective> CollectDirectives(
            IObjectField field,
            FieldNode selection)
        {
            var processed  = new HashSet <string>();
            var directives = new List <IDirective>();

            CollectTypeSystemDirectives(
                processed,
                directives,
                field);

            CollectQueryDirectives(
                processed,
                directives,
                field,
                selection);

            return(directives.AsReadOnly());
        }
        public Expression HandleLeave(
            SelectionVisitorContext context,
            IFieldSelection selection,
            Expression expression)
        {
            IObjectField field = context.FieldSelection.Field;

            if (field.ContextData.ContainsKey(_contextDataKey) &&
                field.Member is PropertyInfo propertyInfo)
            {
                Type elementType = ExtendedType.Tools.GetElementType(propertyInfo.PropertyType) !;

                return(Expression.Call(
                           typeof(Enumerable),
                           "Take",
                           new[] { elementType },
                           expression,
                           Expression.Constant(_take)));
            }

            return(expression);
        }
Beispiel #23
0
        public Selection(
            IObjectType declaringType,
            IObjectField field,
            FieldNode selection,
            FieldDelegate resolverPipeline,
            NameString?responseName = null,
            IReadOnlyDictionary <NameString, ArgumentValue>?arguments = null,
            SelectionIncludeCondition?includeCondition = null,
            bool internalSelection = false)
        {
            DeclaringType = declaringType
                            ?? throw new ArgumentNullException(nameof(declaringType));
            Field = field
                    ?? throw new ArgumentNullException(nameof(field));
            SyntaxNode = selection
                         ?? throw new ArgumentNullException(nameof(selection));
            ResponseName = responseName ??
                           (selection.Alias is null
                    ? selection.Name.Value
                    : selection.Alias.Value);
            ResolverPipeline = resolverPipeline ??
                               throw new ArgumentNullException(nameof(resolverPipeline));
            Arguments = arguments is null
                ? _emptyArguments
                : new ArgumentMap(arguments);
            InclusionKind = internalSelection
                ? SelectionInclusionKind.Internal
                : SelectionInclusionKind.Always;

            if (includeCondition is not null)
            {
                _includeConditions = new List <SelectionIncludeCondition> {
                    includeCondition
                };
                ModifyCondition(true);
            }
        }
Beispiel #24
0
 public WrapperInfo(IObjectField unwrappingObjectField)
 {
     UnwrappingObjectField = unwrappingObjectField;
     Type = WrapperInfoType.ObjectField;
 }
 /// <summary>
 /// Allows to compile the field resolver pipeline for a field.
 /// </summary>
 /// <param name="field">The field.</param>
 /// <param name="selection">The selection of the field.</param>
 /// <returns>
 /// Returns a <see cref="FieldDelegate" /> representing the field resolver pipeline.
 /// </returns>
 public FieldDelegate CompileResolverPipeline(IObjectField field, FieldNode selection) =>
 _compileResolverPipeline(field, selection);
 /// <summary>
 /// Allows to compile the field resolver pipeline for a field.
 /// </summary>
 /// <param name="field">The field.</param>
 /// <param name="selection">The selection of the field.</param>
 /// <returns>
 /// Returns a <see cref="FieldDelegate" /> representing the field resolver pipeline.
 /// </returns>
 public FieldDelegate CompileResolverPipeline(
     IObjectField field,
     FieldNode selection) =>
 _compiler.CreateFieldMiddleware(field, selection);
 public static string GetTypeName(this IObjectField field, string @namespace)
 {
     return(CreateTypeName(field.Type, @namespace));
 }
 private static SyntaxNode CreateAccessPropertyExpression(SyntaxNode globalTargetAccessor, IObjectField property, SyntaxGenerator generator)
 {
     if (globalTargetAccessor == null)
     {
         return(SyntaxFactory.IdentifierName(property.Name));
     }
     return(generator.MemberAccessExpression(globalTargetAccessor, property.Name));
 }