示例#1
0
        private static bool GetOnFragment(IResolverContext context)
        {
            IReadOnlyCollection <DirectiveLocation> locations =
                context.Parent <Directive>().Locations;

            return(Contains(locations, DirectiveLocation.InlineFragment) ||
                   Contains(locations, DirectiveLocation.FragmentSpread) ||
                   Contains(locations, DirectiveLocation.FragmentDefinition));
        }
示例#2
0
        private static bool GetOnOperation(IResolverContext context)
        {
            IReadOnlyCollection <DirectiveLocation> locations =
                context.Parent <Directive>().Locations;

            return(Contains(locations, DirectiveLocation.Query) ||
                   Contains(locations, DirectiveLocation.Mutation) ||
                   Contains(locations, DirectiveLocation.Subscription));
        }
        public VariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            IInputType targetType)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            if (!ScopeNames.Fields.Equals(variable.Scope.Value))
            {
                throw new ArgumentException(
                          FieldScopedVariableResolver_CannotHandleVariable,
                          nameof(variable));
            }

            if (context.ObjectType.Fields.TryGetField(variable.Name.Value, out IObjectField? field))
            {
                object parent = context.Parent <object>();

                IValueNode?valueLiteral = null;

                if (parent is IReadOnlyDictionary <string, object> dict &&
                    dict.TryGetValue(field.Name, out object?value))
                {
                    if (value is IValueNode v)
                    {
                        valueLiteral = v;
                    }
                    else if (field.Type.IsInputType() && field.Type is IInputType type)
                    {
                        valueLiteral = type.ParseValue(value);
                    }
                }

                return(new VariableValue
                       (
                           variable.ToVariableName(),
                           targetType.ToTypeNode(),
                           valueLiteral ?? NullValueNode.Default,
                           null
                       ));
            }

            throw ThrowHelper.FieldScopedVariableResolver_InvalidFieldName(
                      variable.Name.Value,
                      context.Selection.SyntaxNode,
                      context.Path);
        }
示例#4
0
        public static double GetHeight(
            IResolverContext context)
        {
            double height = context.Parent <ICharacter>().Height;

            if (context.Argument <Unit?>("unit") == Unit.Foot)
            {
                return(height * 3.28084d);
            }
            return(height);
        }
示例#5
0
        public static async Task <IEnumerable <Comment> > GetComments(IResolverContext context)
        {
            var post = context.Parent <Post>();

            var first  = context.Argument <int>("first");
            var offset = context.Argument <int>("offset");

            return(await context
                   .Service <ICommentQuery>()
                   .WithPost(post.CanonicalTitle)
                   .Fetch(first, offset));
        }
示例#6
0
        private static long GetTotalCount(
            IResolverContext context)
        {
            IConnection connection = context.Parent <IConnection>();

            if (connection.PageInfo.TotalCount.HasValue)
            {
                return(connection.PageInfo.TotalCount.Value);
            }

            throw new GraphQLException(
                      "The total count was not provided by the connection.");
        }
        private static IResolverResult <long> GetTotalCount(
            IResolverContext context)
        {
            IConnection connection = context.Parent <IConnection>();

            if (connection.PageInfo.TotalCount.HasValue)
            {
                return(ResolverResult.CreateValue(
                           connection.PageInfo.TotalCount.Value));
            }
            return(ResolverResult.CreateError <long>(
                       "The total count was not provided by the connection."));
        }
示例#8
0
        public static IEnumerable <ICharacter> GetCharacter(
            IResolverContext context)
        {
            ICharacter          character  = context.Parent <ICharacter>();
            CharacterRepository repository = context.Service <CharacterRepository>();

            foreach (string friendId in character.Friends)
            {
                ICharacter friend = repository.GetCharacter(friendId);
                if (friend != null)
                {
                    yield return(friend);
                }
            }
        }
示例#9
0
        public VariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            IInputType targetType)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            if (!ScopeNames.Fields.Equals(variable.Scope.Value))
            {
                throw new ArgumentException(
                          StitchingResources
                          .FieldScopedVariableResolver_CannotHandleVariable,
                          nameof(variable));
            }

            if (context.ObjectType.Fields.TryGetField(variable.Name.Value,
                                                      out ObjectField field))
            {
                IReadOnlyDictionary <string, object> obj =
                    context.Parent <IReadOnlyDictionary <string, object> >();

                return(new VariableValue
                       (
                           variable.ToVariableName(),
                           targetType.ToTypeNode(),
                           _converter.Convert(obj[field.Name], targetType, variable.Value),
                           null
                       ));
            }

            throw new QueryException(ErrorBuilder.New()
                                     .SetMessage(
                                         StitchingResources.FieldScopedVariableResolver_InvalidFieldName,
                                         variable.Name.Value)
                                     .SetCode(ErrorCodes.FieldNotDefined)
                                     .SetPath(context.Path)
                                     .AddLocation(context.FieldSelection)
                                     .Build());
        }
示例#10
0
        public VariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            ITypeNode targetType)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            if (!ScopeNames.Fields.Equals(variable.Scope.Value))
            {
                throw new ArgumentException(
                          StitchingResources
                          .FieldScopedVariableResolver_CannotHandleVariable,
                          nameof(variable));
            }

            if (context.ObjectType.Fields.TryGetField(variable.Name.Value,
                                                      out ObjectField field))
            {
                IReadOnlyDictionary <string, object> obj =
                    context.Parent <IReadOnlyDictionary <string, object> >();

                return(new VariableValue
                       (
                           variable.ToVariableName(),
                           field.Type.ToTypeNode(),
                           obj[field.Name],
                           null
                       ));
            }

            throw new QueryException(QueryError.CreateFieldError(
                                         string.Format(CultureInfo.InvariantCulture,
                                                       StitchingResources
                                                       .FieldScopedVariableResolver_InvalidFieldName,
                                                       variable.Name.Value),
                                         context.Path,
                                         context.FieldSelection)
                                     .WithCode(ErrorCodes.FieldNotDefined));
        }
示例#11
0
 public T Parent <T>() => _resolverContext.Parent <T>();
示例#12
0
 public void ResolverContext(IResolverContext context)
 {
     _context = context;
     Id       = context.Parent <Property>().Id;
     Last     = context.Argument <int?>("last");
 }