Пример #1
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.Arguments.Equals(variable.Scope.Value))
            {
                throw new ArgumentException(StitchingResources
                                            .ArgumentScopedVariableResolver_CannotHandleVariable,
                                            nameof(variable));
            }

            IInputField argument = context.Field.Arguments.FirstOrDefault(t =>
                                                                          t.Name.Value.EqualsOrdinal(variable.Name.Value));

            if (argument == null)
            {
                throw new QueryException(QueryError.CreateFieldError(
                                             string.Format(CultureInfo.InvariantCulture,
                                                           StitchingResources
                                                           .ArgumentScopedVariableResolver_InvalidArgumentName,
                                                           variable.Name.Value),
                                             context.Path,
                                             context.FieldSelection)
                                         .WithCode(ErrorCodes.ArgumentNotDefined));
            }

            return(new VariableValue
                   (
                       variable.ToVariableName(),
                       argument.Type.ToTypeNode(),
                       context.Argument <object>(variable.Name.Value),
                       argument.Type.IsNonNullType() &&
                       argument.DefaultValue.IsNull()
                    ? null
                    : argument.DefaultValue
                   ));
        }
Пример #2
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());
        }
Пример #3
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));
        }
        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.Arguments.Equals(variable.Scope.Value))
            {
                throw new ArgumentException(
                          StitchingResources.ArgumentScopedVariableResolver_CannotHandleVariable,
                          nameof(variable));
            }

            IInputField argument = context.Field.Arguments.FirstOrDefault(t =>
                                                                          t.Name.Value.EqualsOrdinal(variable.Name.Value));

            if (argument == null)
            {
                throw new QueryException(ErrorBuilder.New()
                                         .SetMessage(
                                             StitchingResources.ArgumentScopedVariableResolver_InvalidArgumentName,
                                             variable.Name.Value)
                                         .SetCode(ErrorCodes.ArgumentNotDefined)
                                         .SetPath(context.Path)
                                         .AddLocation(context.FieldSelection)
                                         .Build());
            }

            return(new VariableValue
                   (
                       variable.ToVariableName(),
                       argument.Type.ToTypeNode(),
                       context.Argument <IValueNode>(variable.Name.Value),
                       argument.Type.IsNonNullType() && argument.DefaultValue.IsNull()
                    ? null
                    : argument.DefaultValue
                   ));
        }
Пример #5
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 (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

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

            context.ContextData.TryGetValue(variable.Name.Value,
                                            out object data);

            return(new VariableValue
                   (
                       variable.ToVariableName(),
                       targetType,
                       data,
                       null
                   ));
        }