public void InvalidScope()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            ObjectField field = schema.GetType <ObjectType>("Query").Fields["foo"];

            var selection = new Mock <IFieldSelection>(MockBehavior.Strict);

            selection.SetupGet(t => t.Field).Returns(field);

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.Selection).Returns(selection.Object);
            context.Setup(t => t.ArgumentValue <object>(It.IsAny <NameString>())).Returns("Baz");

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("foo"),
                new NameNode("b"));

            // act
            var    resolver = new ArgumentScopedVariableResolver();
            Action a        = () => resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Equal("variable", Assert.Throws <ArgumentException>(a).ParamName);
        }
        public void InvalidScope()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var context = new Mock <IMiddlewareContext>();

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("foo"),
                new NameNode("b"));

            // act
            var    resolver = new ContextDataScopedVariableResolver();
            Action a        = () => resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Equal("variable", Assert.Throws <ArgumentException>(a).ParamName);
        }
        public void ContextIsNull()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("scopedContextData"),
                new NameNode("b"));

            // act
            var resolver = new ScopedContextDataScopedVariableResolver();

            void Action() => resolver.Resolve(
                null !,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Equal("context", Assert.Throws <ArgumentNullException>(Action).ParamName);
        }
        public void ContextDataEntryDoesNotExist()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var contextData = new Dictionary <string, object>();

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ContextData).Returns(contextData);

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("contextData"),
                new NameNode("a"));

            // act
            var           resolver = new ContextDataScopedVariableResolver();
            VariableValue value    = resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Null(value.DefaultValue);
            Assert.Equal("contextData_a", value.Name);
            Assert.Equal("String", Assert.IsType <NamedTypeNode>(value.Type).Name.Value);
            Assert.Equal(NullValueNode.Default, value.Value);
        }
Ejemplo n.º 5
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 (_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.ScopeNotDefined)
                                     .SetPath(context.Path)
                                     .AddLocation(context.FieldSelection)
                                     .Build());
        }
Ejemplo n.º 6
0
        public void InvalidScope()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var context = new Mock <IMiddlewareContext>();

            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Argument <object>(It.IsAny <string>()))
            .Returns("Baz");

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("foo"),
                new NameNode("b"));

            // act
            var    resolver = new ArgumentScopedVariableResolver();
            Action a        = () => resolver.Resolve(context.Object, scopedVariable,
                                                     new NamedTypeNode(new NameNode("abc")));

            // assert
            Assert.Equal("variable",
                         Assert.Throws <ArgumentException>(a).ParamName);
        }
Ejemplo n.º 7
0
        public void ContextIsNull()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("arguments"),
                new NameNode("b"));

            // act
            var    resolver = new ArgumentScopedVariableResolver();
            Action a        = () => resolver.Resolve(null, scopedVariable,
                                                     new NamedTypeNode(new NameNode("abc")));

            // assert
            Assert.Equal("context",
                         Assert.Throws <ArgumentNullException>(a).ParamName);
        }
Ejemplo n.º 8
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 (Resolvers.TryGetValue(variable.Scope.Value,
                                      out IScopedVariableResolver resolver))
            {
                return(resolver.Resolve(context, variable, targetType));
            }

            throw new QueryException(QueryError.CreateFieldError(
                                         string.Format(CultureInfo.InvariantCulture,
                                                       StitchingResources
                                                       .RootScopedVariableResolver_ScopeNotSupported,
                                                       variable.Scope.Value),
                                         context.Path,
                                         context.FieldSelection)
                                     .WithCode(ErrorCodes.ScopeNotDefined));
        }
Ejemplo n.º 9
0
        public void InvalidScope()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            var context = new Mock <IMiddlewareContext>();

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("foo"),
                new NameNode("b"));

            // act
            var    resolver = new ContextDataScopedVariableResolver();
            Action a        = () => resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Equal("variable", Assert.Throws <ArgumentException>(a).ParamName);
        }
Ejemplo n.º 10
0
        public void CreateVariableValue()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var         context = new Mock <IResolverContext>(MockBehavior.Strict);
            ObjectField field   = schema.GetType <ObjectType>("Query").Fields["foo"];

            context.SetupGet(t => t.Field).Returns(field);
            context.Setup(t => t.Argument <IValueNode>("a")).Returns(new StringValueNode("baz"));

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("arguments"),
                new NameNode("a"));

            // act
            var           resolver = new ArgumentScopedVariableResolver();
            VariableValue value    = resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Equal("bar", Assert.IsType <StringValueNode>(value.DefaultValue).Value);
            Assert.Equal("arguments_a", value.Name);
            Assert.Equal("String", Assert.IsType <NamedTypeNode>(value.Type).Name.Value);
            Assert.Equal("baz", value.Value.Value);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Determines whether the specified <see cref="ScopedVariableNode"/>
        /// is equal to the current <see cref="ScopedVariableNode"/>.
        /// </summary>
        /// <param name="other">
        /// The <see cref="ScopedVariableNode"/> to compare with the current
        /// <see cref="ScopedVariableNode"/>.
        /// </param>
        /// <returns>
        /// <c>true</c> if the specified <see cref="ScopedVariableNode"/>
        /// is equal to the current <see cref="ScopedVariableNode"/>;
        /// otherwise, <c>false</c>.
        /// </returns>
        public bool Equals(ScopedVariableNode other)
        {
            if (other is null)
            {
                return(false);
            }

            if (ReferenceEquals(other, this))
            {
                return(true);
            }

            return(other.Value.Equals(Value, StringComparison.Ordinal));
        }
        public void FieldDoesNotExist()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ObjectType).Returns(
                schema.GetType <ObjectType>("Query"));
            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Parent <IReadOnlyDictionary <string, object> >())
            .Returns(new Dictionary <string, object> {
                { "a", "baz" }
            });
            context.Setup(t => t.FieldSelection)
            .Returns(new FieldNode(
                         null,
                         new NameNode("foo"),
                         null,
                         Array.Empty <DirectiveNode>(),
                         Array.Empty <ArgumentNode>(),
                         null));
            context.Setup(t => t.Path).Returns(Path.New("foo"));

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("fields"),
                new NameNode("b"));

            // act
            var    resolver = new FieldScopedVariableResolver();
            Action a        = () => resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Collection(
                Assert.Throws <QueryException>(a).Errors,
                t => Assert.Equal(ErrorCodes.FieldNotDefined, t.Code));
        }
Ejemplo n.º 13
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
                   ));
        }
Ejemplo n.º 14
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());
        }
Ejemplo n.º 15
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 void ArgumentDoesNotExist()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            var selection = new Mock <IFieldSelection>(MockBehavior.Strict);

            selection.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.Selection).Returns(selection.Object);

            context.Setup(t => t.ArgumentValue <object>(It.IsAny <NameString>()))
            .Returns("Baz");
            context.Setup(t => t.Selection.SyntaxNode)
            .Returns(new FieldNode(
                         null,
                         new NameNode("foo"),
                         null,
                         Array.Empty <DirectiveNode>(),
                         Array.Empty <ArgumentNode>(),
                         null));
            context.Setup(t => t.Path).Returns(Path.New("foo"));

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("arguments"),
                new NameNode("b"));

            // act
            var    resolver = new ArgumentScopedVariableResolver();
            Action a        = () => resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Collection(
                Assert.Throws <GraphQLException>(a).Errors,
                t => Assert.Equal(ErrorCodes.Stitching.ArgumentNotDefined, t.Code));
        }
        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
                   ));
        }
Ejemplo n.º 18
0
        public void TargetTypeIsNull()
        {
            // arrange
            var context = new Mock <IMiddlewareContext>();

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("contextData"),
                new NameNode("b"));

            // act
            var resolver = new ContextDataScopedVariableResolver();

            void Action() => resolver.Resolve(context.Object, scopedVariable, null !);

            // assert
            Assert.Equal("targetType", Assert.Throws <ArgumentNullException>(Action).ParamName);
        }
Ejemplo n.º 19
0
        public void ArgumentDoesNotExist()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var context = new Mock <IMiddlewareContext>();

            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Argument <object>(It.IsAny <NameString>()))
            .Returns("Baz");
            context.Setup(t => t.FieldSelection)
            .Returns(new FieldNode(
                         null,
                         new NameNode("foo"),
                         null,
                         Array.Empty <DirectiveNode>(),
                         Array.Empty <ArgumentNode>(),
                         null));
            context.Setup(t => t.Path).Returns(Path.New("foo"));

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("arguments"),
                new NameNode("b"));

            // act
            var    resolver = new ArgumentScopedVariableResolver();
            Action a        = () => resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Collection(
                Assert.Throws <QueryException>(a).Errors,
                t => Assert.Equal(ErrorCodes.ArgumentNotDefined, t.Code));
        }
        public void CreateVariableValue()
        {
            // arrange
            var inputFormatter = new InputFormatter();

            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String a: String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ObjectType).Returns(
                schema.GetType <ObjectType>("Query"));
            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Parent <object>())
            .Returns(new Dictionary <string, object> {
                { "a", "baz" }
            });
            context.Setup(t => t.Service <InputFormatter>()).Returns(inputFormatter);

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("fields"),
                new NameNode("a"));

            // act
            var resolver = new FieldScopedVariableResolver();
            ScopedVariableValue value = resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Null(value.DefaultValue);
            Assert.Equal("__fields_a", value.Name);
            Assert.IsType <NamedTypeNode>(value.Type);
            Assert.Equal("baz", value.Value.Value);
        }
        public void CreateVariableValue()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String a: String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ObjectType).Returns(
                schema.GetType <ObjectType>("Query"));
            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Parent <IReadOnlyDictionary <string, object> >())
            .Returns(new Dictionary <string, object> {
                { "a", "baz" }
            });

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("fields"),
                new NameNode("a"));

            // act
            var           resolver = new FieldScopedVariableResolver();
            VariableValue value    = resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Null(value.DefaultValue);
            Assert.Equal("fields_a", value.Name);
            Assert.IsType <NamedTypeNode>(value.Type);
            Assert.Equal("baz", value.Value.Value);
        }
        public void CreateVariableValue()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            ObjectField field = schema.GetType <ObjectType>("Query").Fields["foo"];

            var selection = new Mock <IFieldSelection>(MockBehavior.Strict);

            selection.SetupGet(t => t.Field).Returns(field);

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.Selection).Returns(selection.Object);
            context.Setup(t => t.ArgumentLiteral <IValueNode>("a"))
            .Returns(new StringValueNode("baz"));

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("arguments"),
                new NameNode("a"));

            // act
            var resolver = new ArgumentScopedVariableResolver();
            ScopedVariableValue value = resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Equal("bar", Assert.IsType <StringValueNode>(value.DefaultValue).Value);
            Assert.Equal("__arguments_a", value.Name);
            Assert.Equal("String", Assert.IsType <NamedTypeNode>(value.Type).Name.Value);
            Assert.Equal("baz", value.Value !.Value);
        }
Ejemplo n.º 23
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
                   ));
        }
        public void CreateVariableValue()
        {
            // arrange
            var inputFormatter = new InputFormatter();

            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            ImmutableDictionary <string, object> contextData =
                ImmutableDictionary <string, object> .Empty
                .Add("a", "AbcDef");

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ScopedContextData).Returns(contextData);
            context.Setup(t => t.Service <InputFormatter>()).Returns(inputFormatter);

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("scopedContextData"),
                new NameNode("a"));

            // act
            var resolver = new ScopedContextDataScopedVariableResolver();
            ScopedVariableValue value = resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Null(value.DefaultValue);
            Assert.Equal("__scopedContextData_a", value.Name);
            Assert.Equal("String", Assert.IsType <NamedTypeNode>(value.Type).Name.Value);
            Assert.Equal("AbcDef", value.Value !.Value);
        }
        public void CreateVariableValue()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var contextData = ImmutableDictionary <string, object> .Empty
                              .Add("a", "AbcDef");

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ScopedContextData).Returns(contextData);

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("scopedContextData"),
                new NameNode("a"));

            // act
            var           resolver = new ScopedContextDataScopedVariableResolver();
            VariableValue value    = resolver.Resolve(
                context.Object, scopedVariable,
                new NamedTypeNode(new NameNode("abc")));

            // assert
            Assert.Null(value.DefaultValue);
            Assert.Equal("scopedContextData_a", value.Name);
            Assert.Equal("abc",
                         Assert.IsType <NamedTypeNode>(value.Type).Name.Value);
            Assert.Equal("AbcDef", value.Value);
        }