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 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 new QueryException(ErrorBuilder.New()
                                     .SetMessage(
                                         StitchingResources.FieldScopedVariableResolver_InvalidFieldName,
                                         variable.Name.Value)
                                     .SetCode(ErrorCodes.Stitching.FieldNotDefined)
                                     .SetPath(context.Path)
                                     .AddLocation(context.FieldSelection)
                                     .Build());
        }
        public ScopedVariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            IInputType targetType)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            if (_resolvers.TryGetValue(variable.Scope.Value, out IScopedVariableResolver? resolver))
            {
                return(resolver.Resolve(context, variable, targetType));
            }

            throw ThrowHelper.RootScopedVariableResolver_ScopeNotSupported(
                      variable.Scope.Value,
                      context.Selection.SyntaxNode,
                      context.Path);
        }
        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 (_resolvers.TryGetValue(variable.Scope.Value, out IScopedVariableResolver? resolver))
            {
                return(resolver.Resolve(context, variable, targetType));
            }

            throw new QueryException(ErrorBuilder.New()
                                     .SetMessage(
                                         StitchingResources.RootScopedVariableResolver_ScopeNotSupported,
                                         variable.Scope.Value)
                                     .SetCode(ErrorCodes.Stitching.ScopeNotDefined)
                                     .SetPath(context.Path)
                                     .AddLocation(context.FieldSelection)
                                     .Build());
        }
        public ScopedVariableValue 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 var value))
                {
                    InputFormatter formatter = context.Service <InputFormatter>();

                    if (value is IValueNode v)
                    {
                        valueLiteral = v;
                    }
                    else if (field.Type.IsInputType() && field.Type is IInputType type)
                    {
                        valueLiteral = formatter.FormatValue(value, type, field.Name);
                    }
                }

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

            throw ThrowHelper.FieldScopedVariableResolver_InvalidFieldName(
                      variable.Name.Value,
                      context.Selection.SyntaxNode,
                      context.Path);
        }
        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.Stitching.ArgumentNotDefined)
                                         .SetPath(context.Path)
                                         .AddLocation(context.FieldSelection)
                                         .Build());
            }

            return(new VariableValue
                   (
                       variable.ToVariableName(),
                       argument.Type.ToTypeNode(),
                       context.ArgumentLiteral <IValueNode>(variable.Name.Value),
                       argument.Type.IsNonNullType() && argument.DefaultValue.IsNull()
                    ? null
                    : argument.DefaultValue
                   ));
        }
        public ScopedVariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            IInputType 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(
                          ContextDataScopedVariableResolver_CannotHandleVariable,
                          nameof(variable));
            }

            context.ContextData.TryGetValue(variable.Name.Value, out var data);
            InputFormatter formatter = context.Service <InputFormatter>();

            IValueNode literal = data switch
            {
                IValueNode l => l,
                       null => NullValueNode.Default,
                       _ => formatter.FormatValue(data, targetType, Path.New(variable.Name.Value))
            };

            return(new ScopedVariableValue
                   (
                       variable.ToVariableName(),
                       targetType.ToTypeNode(),
                       literal,
                       null
                   ));
        }
    }
        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 (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

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

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

            IValueNode literal = data switch
            {
                IValueNode l => l,
                       null => NullValueNode.Default,
                       _ => targetType.ParseValue(data)
            };

            return(new VariableValue
                   (
                       variable.ToVariableName(),
                       targetType.ToTypeNode(),
                       literal,
                       null
                   ));
        }
    }
Beispiel #8
0
        public VariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            IInputType targetType)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

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

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

            if (argument is null)
            {
                throw ArgumentScopedVariableResolver_InvalidArgumentName(
                          variable.Name.Value,
                          context.FieldSelection,
                          context.Path);
            }

            return(new VariableValue
                   (
                       variable.ToVariableName(),
                       argument.Type.ToTypeNode(),
                       context.ArgumentLiteral <IValueNode>(variable.Name.Value),
                       argument.Type.IsNonNullType() && argument.DefaultValue.IsNull()
                    ? null
                    : argument.DefaultValue
                   ));
        }
Beispiel #9
0
        public ScopedVariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            IInputType targetType)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

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

            if (!context.Selection.Field.Arguments.TryGetField(
                    variable.Name.Value,
                    out IInputField? argument))
            {
                throw ArgumentScopedVariableResolver_InvalidArgumentName(
                          variable.Name.Value,
                          context.Selection.SyntaxNode,
                          context.Path);
            }

            return(new ScopedVariableValue
                   (
                       variable.ToVariableName(),
                       argument.Type.ToTypeNode(),
                       context.ArgumentLiteral <IValueNode>(variable.Name.Value),
                       argument.Type.IsNonNullType() && argument.DefaultValue.IsNull()
                    ? null
                    : argument.DefaultValue
                   ));
        }