コード例 #1
0
        public void Coerce_NonNullString_ToAbc()
        {
            // arrange
            DocumentNode document =
                Utf8GraphQLParser.Parse("{ bar (a: \"abc\") }");
            OperationDefinitionNode operation = document.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            ISchema schema    = CreateSchema();
            var     fragments = new FragmentCollection(
                schema,
                document);

            var variables = new VariableCollection(
                TypeConversion.Default,
                new Dictionary <string, object>());

            var collector = new FieldCollector(fragments, (f, s) => null);
            IReadOnlyCollection <FieldSelection> selections =
                collector.CollectFields(schema.QueryType,
                                        operation.SelectionSet, Path.New("bar"));
            FieldSelection selection = selections.First();
            var            path      = Path.New("bar");

            // act
            IReadOnlyDictionary <NameString, ArgumentValue> arguments =
                selection.CoerceArguments(variables);

            // assert
            MatchSnapshot(arguments);
        }
コード例 #2
0
        public void Coerce_InputObject_NonNullFieldIsNull()
        {
            // arrange
            DocumentNode document =
                Utf8GraphQLParser.Parse("{ foo(a: {  a: { } }) }");
            OperationDefinitionNode operation = document.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            ISchema schema    = CreateSchema();
            var     fragments = new FragmentCollection(
                schema,
                document);

            var variables = new VariableCollection(
                TypeConversion.Default,
                new Dictionary <string, object>());

            var collector = new FieldCollector(fragments, (f, s) => null);
            IReadOnlyCollection <FieldSelection> selections =
                collector.CollectFields(schema.QueryType,
                                        operation.SelectionSet, null);
            FieldSelection selection = selections.First();

            // act
            Action action = () => selection.CoerceArguments(variables);

            // assert
            Assert.Throws <QueryException>(action).Errors.MatchSnapshot();
        }
コード例 #3
0
        private void Initialize(
            IExecutionContext executionContext,
            FieldSelection fieldSelection,
            IImmutableStack <object> source,
            IDictionary <string, object> serializedResult)
        {
            _executionContext = executionContext;
            _serializedResult = serializedResult;
            _fieldSelection   = fieldSelection;

            IsRoot            = true;
            Path              = Path.New(fieldSelection.ResponseName);
            Source            = source;
            SourceObject      = executionContext.Operation.RootValue;
            ScopedContextData = ImmutableDictionary <string, object> .Empty;

            _arguments = fieldSelection.CoerceArguments(
                executionContext.Variables,
                executionContext.Converter);

            string responseName = fieldSelection.ResponseName;

            PropagateNonNullViolation = () =>
            {
                serializedResult[responseName] = null;
            };
        }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        private void Initialize(
            FieldSelection fieldSelection,
            IImmutableStack <object> source,
            object sourceObject,
            ResolverContext sourceContext,
            FieldData serializedResult,
            Path path,
            Action propagateNonNullViolation)
        {
            _executionContext = sourceContext._executionContext;
            _serializedResult = serializedResult;
            _fieldSelection   = fieldSelection;

            _arguments = fieldSelection.CoerceArguments(
                sourceContext._executionContext.Variables,
                sourceContext._executionContext.Converter);

            Path              = path;
            Source            = source;
            SourceObject      = sourceObject;
            ScopedContextData = sourceContext.ScopedContextData;
            LocalContextData  = ImmutableDictionary <string, object> .Empty;

            bool   isNonNullType = fieldSelection.Field.Type.IsNonNullType();
            Action parentPropagateNonNullViolation = sourceContext.PropagateNonNullViolation;

            PropagateNonNullViolation = () =>
            {
                if (isNonNullType)
                {
                    if (propagateNonNullViolation != null)
                    {
                        propagateNonNullViolation.Invoke();
                    }
                    else if (parentPropagateNonNullViolation != null)
                    {
                        parentPropagateNonNullViolation.Invoke();
                    }
                }
                serializedResult.SetFieldValue(
                    fieldSelection.ResponseIndex,
                    fieldSelection.ResponseName,
                    null);
            };
        }
コード例 #6
0
        private void Initialize(
            FieldSelection fieldSelection,
            IImmutableStack <object> source,
            object sourceObject,
            ResolverContext sourceContext,
            IDictionary <string, object> serializedResult,
            Path path,
            Action propagateNonNullViolation)
        {
            _executionContext = sourceContext._executionContext;
            _serializedResult = serializedResult;
            _fieldSelection   = fieldSelection;

            _arguments = fieldSelection.CoerceArguments(
                sourceContext._executionContext.Variables);

            Path              = path;
            Source            = source;
            SourceObject      = sourceObject;
            ScopedContextData = sourceContext.ScopedContextData;

            bool   isNonNullType = fieldSelection.Field.Type.IsNonNullType();
            string responseName  = fieldSelection.ResponseName;
            Action parentPropagateNonNullViolation =
                sourceContext.PropagateNonNullViolation;

            PropagateNonNullViolation = () =>
            {
                if (isNonNullType)
                {
                    if (propagateNonNullViolation != null)
                    {
                        propagateNonNullViolation.Invoke();
                    }
                    else if (parentPropagateNonNullViolation != null)
                    {
                        parentPropagateNonNullViolation.Invoke();
                    }
                }
                serializedResult[responseName] = null;
            };
        }
コード例 #7
0
        public void Coerce_NonNullString_ToNull()
        {
            // arrange
            DocumentNode document =
                Utf8GraphQLParser.Parse("{ bar }");
            OperationDefinitionNode operation = document.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            ISchema schema    = CreateSchema();
            var     fragments = new FragmentCollection(
                schema,
                document);

            var variables = new VariableValueCollection(
                TypeConversion.Default,
                new Dictionary <string, object>());

            var collector = new FieldCollector(
                fragments,
                (f, s) => null,
                TypeConversion.Default,
                Array.Empty <IArgumentCoercionHandler>());

            IReadOnlyCollection <FieldSelection> selections =
                collector.CollectFields(schema.QueryType,
                                        operation.SelectionSet, Path.New("bar"));
            FieldSelection selection = selections.First();
            var            path      = Path.New("bar");

            // act
            Action action = () =>
                            selection.CoerceArguments(variables, TypeConversion.Default);

            // assert
            Assert.Throws <QueryException>(action).Errors.MatchSnapshot();
        }