private IValueNode ReplaceVariable(
            VariableNode variable,
            IInputType type)
        {
            if (_variables.TryGetVariable(
                    variable.Name.Value,
                    out object v))
            {
                if (!type.ClrType.IsInstanceOfType(v) &&
                    !_typeConversion.TryConvert(
                        typeof(object),
                        type.ClrType,
                        v,
                        out v))
                {
                    throw new QueryException(
                              ErrorBuilder.New()
                              .SetMessage(CoreResources.VarRewriter_CannotConvert)
                              .SetCode(ErrorCodes.Utilities.NoConverter)
                              .AddLocation(variable)
                              .Build());
                }

                return(type.ParseValue(v));
            }

            return(type.ParseValue(null));
        }
Ejemplo n.º 2
0
        private IValueNode ReplaceVariable(
            VariableNode variable,
            IInputType type)
        {
            if (_variables.TryGetVariable(
                    variable.Name.Value,
                    out object v))
            {
                if (!type.ClrType.IsInstanceOfType(v) &&
                    !_typeConversion.TryConvert(
                        typeof(object),
                        type.ClrType,
                        v,
                        out v))
                {
                    // TODO : resource
                    // TODO : path?
                    throw new QueryException(
                              ErrorBuilder.New()
                              .SetMessage(
                                  "Unable to convert the specified " +
                                  "variable value.")
                              .Build());
                }

                return(type.ParseValue(v));
            }

            return(type.ParseValue(null));
        }
Ejemplo n.º 3
0
        public static IValueNode CreateDefaultValue(
            ICompletionContext context,
            ArgumentDefinition definition,
            IInputType fieldType)
        {
            try
            {
                if (definition.NativeDefaultValue != null)
                {
                    return(fieldType.ParseValue(
                               definition.NativeDefaultValue));
                }

                return(definition.DefaultValue.IsNull()
                    ? NullValueNode.Default
                    : definition.DefaultValue);
            }
            catch (Exception ex)
            {
                // TODO : RESOURCES
                context.ReportError(SchemaErrorBuilder.New()
                                    .SetMessage(
                                        "Could not parse the native value of input field " +
                                        $"`{context.Type.Name}.{definition.Name}`.")
                                    .SetCode(TypeErrorCodes.MissingType)
                                    .SetTypeSystemObject(context.Type)
                                    .AddSyntaxNode(definition.SyntaxNode)
                                    .SetException(ex)
                                    .Build());
                return(NullValueNode.Default);
            }
        }
Ejemplo n.º 4
0
 public static IValueNode CreateDefaultValue(
     ITypeCompletionContext context,
     ArgumentDefinition argumentDefinition,
     IInputType argumentType,
     FieldCoordinate argumentCoordinate)
 {
     try
     {
         return(argumentDefinition.NativeDefaultValue != null
             ? argumentType.ParseValue(argumentDefinition.NativeDefaultValue)
             : argumentDefinition.DefaultValue);
     }
     catch (Exception ex)
     {
         context.ReportError(SchemaErrorBuilder.New()
                             .SetMessage(
                                 TypeResources.FieldInitHelper_InvalidDefaultValue,
                                 argumentCoordinate)
                             .SetCode(ErrorCodes.Schema.MissingType)
                             .SetTypeSystemObject(context.Type)
                             .AddSyntaxNode(argumentDefinition.SyntaxNode)
                             .SetException(ex)
                             .Build());
         return(NullValueNode.Default);
     }
 }
Ejemplo n.º 5
0
        public static IValueNode CreateDefaultValue(
            ICompletionContext context,
            ArgumentDefinition definition,
            IInputType fieldType)
        {
            try
            {
                if (definition.NativeDefaultValue != null)
                {
                    return(fieldType.ParseValue(
                               definition.NativeDefaultValue));
                }

                return(definition.DefaultValue.IsNull()
                    ? NullValueNode.Default
                    : definition.DefaultValue);
            }
            catch (Exception ex)
            {
                context.ReportError(SchemaErrorBuilder.New()
                                    .SetMessage(TypeResources.FieldInitHelper_InvalidDefaultValue)
                                    .SetCode(TypeErrorCodes.MissingType)
                                    .SetTypeSystemObject(context.Type)
                                    .AddSyntaxNode(definition.SyntaxNode)
                                    .SetException(ex)
                                    .Build());
                return(NullValueNode.Default);
            }
        }
Ejemplo n.º 6
0
 private void CompleteDefaultValue(
     IInputType type,
     Action <SchemaError> reportError,
     INamedType parentType)
 {
     try
     {
         if (DefaultValue == null)
         {
             if (_nativeDefaultValue == null)
             {
                 DefaultValue = new NullValueNode();
             }
             else
             {
                 DefaultValue = type.ParseValue(_nativeDefaultValue);
             }
         }
     }
     catch (Exception ex)
     {
         reportError(new SchemaError(
                         "Could not parse the native value for input field " +
                         $"`{parentType.Name}.{Name}`.", parentType, ex));
     }
 }
Ejemplo n.º 7
0
        private EventDescription CreateEvent(
            IExecutionContext executionContext)
        {
            IReadOnlyCollection <FieldSelection> selections = executionContext
                                                              .FieldHelper.CollectFields(
                executionContext.Operation.RootType,
                executionContext.Operation.Definition.SelectionSet);

            if (selections.Count == 1)
            {
                FieldSelection selection = selections.Single();
                Dictionary <string, ArgumentValue> argumentValues = selection
                                                                    .CoerceArgumentValues(executionContext.Variables);
                var arguments = new List <ArgumentNode>();

                foreach (KeyValuePair <string, ArgumentValue> argumentValue in
                         argumentValues)
                {
                    IInputType argumentType = argumentValue.Value.Type;
                    object     value        = argumentValue.Value.Value;

                    arguments.Add(new ArgumentNode(
                                      argumentValue.Key,
                                      argumentType.ParseValue(value)));
                }

                return(new EventDescription(selection.Field.Name, arguments));
            }
            else
            {
                // TODO : Error message
                throw new QueryException();
            }
        }
        private static EventDescription CreateEvent(
            IExecutionContext executionContext)
        {
            IReadOnlyCollection <FieldSelection> selections = executionContext
                                                              .CollectFields(
                executionContext.Operation.RootType,
                executionContext.Operation.Definition.SelectionSet,
                null);

            if (selections.Count == 1)
            {
                FieldSelection selection = selections.Single();
                IReadOnlyDictionary <NameString, ArgumentValue> argumentValues =
                    selection.CoerceArguments(executionContext.Variables);
                var arguments = new List <ArgumentNode>();

                foreach (KeyValuePair <NameString, ArgumentValue> argValue in
                         argumentValues)
                {
                    IInputType argumentType = argValue.Value.Type;
                    object     value        = argValue.Value.Value;

                    arguments.Add(new ArgumentNode(
                                      argValue.Key,
                                      argumentType.ParseValue(value)));
                }

                return(new EventDescription(selection.Field.Name, arguments));
            }
            else
            {
                throw new QueryException(
                          CoreResources.Subscriptions_SingleRootField);
            }
        }
Ejemplo n.º 9
0
        private IValueNode CreateValue(ITypeRegistry typeRegistry)
        {
            if (DefaultValue != null)
            {
                return(DefaultValue);
            }

            if (NativeDefaultValue != null)
            {
                IInputType type = CreateType(typeRegistry);
                return(type.ParseValue(NativeDefaultValue));
            }

            return(new NullValueNode());
        }
Ejemplo n.º 10
0
        public IValueNode ParseValue(object value)
        {
            if (_isInputType)
            {
                if (value == null)
                {
                    throw new ArgumentException(
                              "A non null type cannot parse null values.");
                }

                return(_inputType.ParseValue(value));
            }

            throw new InvalidOperationException(
                      "The specified type is not an input type.");
        }
Ejemplo n.º 11
0
        public static IValueNode CreateDefaultValue(
            ICompletionContext context,
            ArgumentDefinition definition,
            IInputType fieldType)
        {
            try
            {
                if (definition.NativeDefaultValue != null)
                {
                    return(fieldType.ParseValue(
                               definition.NativeDefaultValue));
                }

                return(definition.DefaultValue is { }
                    ? definition.DefaultValue
                    : null);
            }
Ejemplo n.º 12
0
        public IValueNode ParseValue(object value)
        {
            if (_isInputType)
            {
                if (value == null)
                {
                    throw new ArgumentException(
                              TypeResources.NonNullType_ValueIsNull,
                              nameof(value));
                }

                return(_inputType.ParseValue(value));
            }

            throw new InvalidOperationException(
                      TypeResources.NonNullType_NotAnInputType);
        }
        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
                   ));
        }
    }
Ejemplo n.º 14
0
        public IValueNode ParseValue(object value)
        {
            if (_isInputType)
            {
                if (value == null)
                {
                    return(new NullValueNode(null));
                }

                if (value is IEnumerable e)
                {
                    var items = new List <IValueNode>();
                    foreach (object v in e)
                    {
                        items.Add(_inputType.ParseValue(value));
                    }
                    return(new ListValueNode(null, items));
                }
            }

            throw new InvalidOperationException(
                      "The specified type is not an input type.");
        }
 private static IValueNode ParseScalar(
     IInputType type, object fieldValue)
 {
     return(type.ParseValue(fieldValue));
 }