Exemplo n.º 1
0
        public void InvalidFieldError()
        {
            // arrange
            Schema       schema = CreateSchema();
            DocumentNode query  = Parser.Default.Parse(@"
                {
                    a
                    x:c
                    xyz
                }
            ");

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

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            // act
            var    fieldResolver = new FieldCollector(fragments, (f, s) => null);
            Action action        = () => fieldResolver
                                   .CollectFields(schema.GetType <ObjectType>("Foo"),
                                                  operation.SelectionSet, null);

            // assert
            Assert.Throws <QueryException>(action);
        }
Exemplo n.º 2
0
        public void FieldsAndFragmentDefinitionsInterfaceType()
        {
            // arrange
            Schema       schema = CreateSchema();
            DocumentNode query  = Utf8GraphQLParser.Parse(@"
                {
                    a
                    ... Test
                }

                fragment Test on IFoo {
                    x: a
                }
            ");

            var variables = new VariableValueCollection(
                TypeConversion.Default,
                new Dictionary <string, VariableValue>());
            var fragments = new FragmentCollection(schema, query);

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().First();

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

            IReadOnlyCollection <FieldSelection> fields_a = fieldResolver
                                                            .CollectFields(schema.GetType <ObjectType>("Foo"),
                                                                           operation.SelectionSet, null);
            IReadOnlyCollection <FieldSelection> fields_b = fieldResolver
                                                            .CollectFields(schema.GetType <ObjectType>("Fum"),
                                                                           operation.SelectionSet, null);

            // assert
            Assert.Collection(fields_a,
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            },
                              f =>
            {
                Assert.Equal("x", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });

            Assert.Collection(fields_b,
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });
        }
Exemplo n.º 3
0
        public void SkipOverIncludeFields()
        {
            // arrange
            Schema       schema = CreateSchema();
            DocumentNode query  = Parser.Default.Parse(@"
                {
                    a @include(if:true)
                    x:c @include(if:true) @skip(if:true)
                }
            ");

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

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            // act
            var fieldResolver = new FieldCollector(fragments, (f, s) => null);
            IReadOnlyCollection <FieldSelection> fields = fieldResolver
                                                          .CollectFields(schema.GetType <ObjectType>("Foo"),
                                                                         operation.SelectionSet, null);

            // assert
            Assert.Collection(fields.Where(t => t.IsVisible(variables)),
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });
        }
Exemplo n.º 4
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 VariableValueCollection(
                TypeConversion.Default,
                new Dictionary <string, VariableValue>());

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

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

            // assert
            Assert.Throws <QueryException>(action).Errors.MatchSnapshot();
        }
Exemplo n.º 5
0
        public ExecutionContext(
            ISchema schema,
            IOperation operation,
            IRequestContext requestContext,
            CancellationToken requestAborted)
        {
            Schema = schema ??
                     throw new ArgumentNullException(nameof(schema));
            Operation = operation ??
                        throw new ArgumentNullException(nameof(operation));
            _requestContext = requestContext
                              ?? throw new ArgumentNullException(nameof(requestContext));

            RequestAborted = requestAborted;

            _cachedQuery = _requestContext.CachedQuery;

            ErrorHandler = requestContext.ServiceScope.ServiceProvider
                           .GetRequiredService <IErrorHandler>();

            Result = new QueryResult();

            var fragments = new FragmentCollection(
                schema, operation.Document);

            Converter = _requestContext.ServiceScope
                        .ServiceProvider.GetTypeConversion();

            _fieldCollector = new FieldCollector(
                fragments, requestContext.ResolveMiddleware, Converter);

            Activator = new Activator(
                requestContext.ServiceScope.ServiceProvider);
        }
Exemplo n.º 6
0
        public void SkipFields()
        {
            // arrange
            Schema       schema = CreateSchema();
            DocumentNode query  = Parser.Default.Parse(@"
                {
                    a
                    x:c @skip(if:true)
                }
            ");

            VariableCollection variables = new VariableCollection(
                new Dictionary <string, object>());
            FragmentCollection fragments = new FragmentCollection(schema, query);

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            // act
            FieldCollector fieldResolver = new FieldCollector(variables, fragments);
            IReadOnlyCollection <FieldSelection> fields = fieldResolver
                                                          .CollectFields(schema.GetType <ObjectType>("Foo"),
                                                                         operation.SelectionSet, e => { });

            // assert
            Assert.Collection(fields,
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });
        }
Exemplo n.º 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 VariableCollection(
                TypeConversion.Default,
                new Dictionary <string, object>());

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

            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();
        }
Exemplo n.º 8
0
 public FieldCollector(
     FragmentCollection fragments,
     Func <ObjectField, FieldNode, FieldDelegate> middlewareFactory)
 {
     _fragments = fragments
                  ?? throw new ArgumentNullException(nameof(fragments));
     _factory = middlewareFactory
                ?? throw new ArgumentNullException(nameof(middlewareFactory));
 }
Exemplo n.º 9
0
 public FieldCollector(
     IVariableCollection variables,
     FragmentCollection fragments)
 {
     _variables = variables
                  ?? throw new ArgumentNullException(nameof(variables));
     _fragments = fragments
                  ?? throw new ArgumentNullException(nameof(fragments));
 }
Exemplo n.º 10
0
        public void FieldsAndFragmentDefinitionsUnionType()
        {
            // arrange
            Schema       schema = CreateSchema();
            DocumentNode query  = Parser.Default.Parse(@"
                {
                    a
                    ... Test
                }

                fragment Test on FooUnion {
                    x: a
                }
            ");

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

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            // act
            var fieldResolver = new FieldCollector(variables, fragments);
            IReadOnlyCollection <FieldSelection> fields_a = fieldResolver
                                                            .CollectFields(schema.GetType <ObjectType>("Foo"),
                                                                           operation.SelectionSet, e => { });
            IReadOnlyCollection <FieldSelection> fields_b = fieldResolver
                                                            .CollectFields(schema.GetType <ObjectType>("Fum"),
                                                                           operation.SelectionSet, e => { });

            // assert
            Assert.Collection(fields_a,
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            },
                              f =>
            {
                Assert.Equal("x", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });

            Assert.Collection(fields_b,
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });
        }
Exemplo n.º 11
0
 public FieldCollector(
     Schema schema,
     VariableCollection variables,
     FragmentCollection fragments)
 {
     _schema = schema
               ?? throw new ArgumentNullException(nameof(schema));
     _variables = variables
                  ?? throw new ArgumentNullException(nameof(variables));
     _fragments = fragments
                  ?? throw new ArgumentNullException(nameof(fragments));
 }
Exemplo n.º 12
0
        public void FieldsAndFragmentDefinitions()
        {
            // arrange
            Schema       schema = CreateSchema();
            DocumentNode query  = Utf8GraphQLParser.Parse(@"
                {
                    a
                    ... Test
                }

                fragment Test on Foo {
                    x: a
                }

                fragment Test on Fa {
                    z: a
                }
            ");

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

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            // act
            var fieldResolver = new FieldCollector(
                fragments,
                (f, s) => null,
                TypeConversion.Default);

            IReadOnlyCollection <FieldSelection> fields = fieldResolver
                                                          .CollectFields(schema.GetType <ObjectType>("Foo"),
                                                                         operation.SelectionSet, null);

            // assert
            Assert.Collection(fields.Where(t => t.IsVisible(variables)),
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            },
                              f =>
            {
                Assert.Equal("x", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });
        }
Exemplo n.º 13
0
        private static IFieldHelper CreateFieldHelper(
            IVariableCollection variables,
            FragmentCollection fragments,
            DirectiveLookup directives,
            ICollection <IError> errors)
        {
            var fieldCollector = new FieldCollector(
                variables, fragments);

            return(new FieldHelper(
                       fieldCollector, directives,
                       variables, errors));
        }
Exemplo n.º 14
0
        public ExecutionContext(
            ISchema schema,
            DirectiveLookup directiveLookup,
            DocumentNode queryDocument,
            OperationDefinitionNode operation,
            OperationRequest request,
            VariableCollection variables,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            Schema = schema
                     ?? throw new ArgumentNullException(nameof(schema));
            _directiveLookup = directiveLookup
                               ?? throw new ArgumentNullException(nameof(directiveLookup));
            QueryDocument = queryDocument
                            ?? throw new ArgumentNullException(nameof(queryDocument));
            Operation = operation
                        ?? throw new ArgumentNullException(nameof(operation));
            Variables = variables
                        ?? throw new ArgumentNullException(nameof(variables));

            Services       = _serviceFactory.Services = request.Services;
            _session       = request.Session;
            _resolverCache = request.Session?.CustomContexts
                             .GetCustomContext <IResolverCache>();

            Fragments       = new FragmentCollection(schema, queryDocument);
            _fieldCollector = new FieldCollector(variables, Fragments);
            OperationType   = schema.GetOperationType(operation.Operation);
            RootValue       = ResolveRootValue(request.Services, schema,
                                               OperationType, request.InitialValue);

            if (RootValue == null)
            {
                RootValue         = CreateRootValue(Services, schema, OperationType);
                _disposeRootValue = true;
            }

            CancellationToken = cancellationToken;
            _clone            = c => new ExecutionContext(
                schema, directiveLookup, queryDocument,
                operation, CreateOperationRequest(
                    schema, request.VariableValues,
                    request.InitialValue),
                variables, c);
        }
Exemplo n.º 15
0
        public void MergeFieldsWithFragmentSpreads()
        {
            // arrange
            DocumentNode query = Utf8GraphQLParser.Parse(
                FileResource.Open("MergeQuery.graphql"));

            OperationDefinitionNode operation =
                query.Definitions.OfType <OperationDefinitionNode>().Single();

            var schema = Schema.Create(
                FileResource.Open("MergeSchema.graphql"),
                c => c.Use(next => context => Task.CompletedTask));

            var fragments = new FragmentCollection(schema, query);

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

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

            IReadOnlyCollection <FieldSelection> selections =
                collector.CollectFields(schema.QueryType,
                                        operation.SelectionSet, Path.New("foo"));

            // assert
            Assert.Collection(selections,
                              selection =>
            {
                Assert.Collection(selection.Selection.SelectionSet.Selections,
                                  selectionNode =>
                {
                    FragmentSpreadNode fragment = Assert.IsType <FragmentSpreadNode>(
                        selectionNode);
                    Assert.Equal("app", fragment.Name.Value);
                },
                                  selectionNode =>
                {
                    FragmentSpreadNode fragment = Assert.IsType <FragmentSpreadNode>(
                        selectionNode);
                    Assert.Equal("parts", fragment.Name.Value);
                });
            });
        }
Exemplo n.º 16
0
        public void Fields()
        {
            // arrange
            var          errorRaised = false;
            Schema       schema      = CreateSchema();
            DocumentNode query       = Utf8GraphQLParser.Parse(@"
                {
                    a
                    x:c
                }
            ");

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

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().First();

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

            IReadOnlyCollection <FieldSelection> fields = fieldResolver
                                                          .CollectFields(
                schema.GetType <ObjectType>("Foo"),
                operation.SelectionSet,
                null);

            // assert
            Assert.Collection(fields.Where(t => t.IsVisible(variables)),
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            },
                              f =>
            {
                Assert.Equal("x", f.ResponseName);
                Assert.Equal("c", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });
            Assert.False(errorRaised);
        }
Exemplo n.º 17
0
        public ExecutionContext(Schema schema, DocumentNode queryDocument,
                                OperationDefinitionNode operation, VariableCollection variables,
                                object rootValue, object userContext)
        {
            Schema = schema
                     ?? throw new ArgumentNullException(nameof(schema));
            QueryDocument = queryDocument
                            ?? throw new ArgumentNullException(nameof(queryDocument));
            Operation = operation
                        ?? throw new ArgumentNullException(nameof(operation));
            Variables = variables
                        ?? throw new ArgumentNullException(nameof(variables));
            RootValue   = rootValue;
            UserContext = userContext;

            Fragments       = new FragmentCollection(schema, queryDocument);
            _fieldCollector = new FieldCollector(schema, variables, Fragments);
        }
Exemplo n.º 18
0
        public void MergeFieldsWithFragmentSpreads()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse(
                FileResource.Open("MergeQuery.graphql"));

            OperationDefinitionNode operation =
                query.Definitions.OfType <OperationDefinitionNode>().Single();

            Schema schema = Schema.Create(
                FileResource.Open("MergeSchema.graphql"),
                c => c.Use(next => context => Task.CompletedTask));

            var fragments = new FragmentCollection(schema, query);

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

            // act
            var collector = new FieldCollector(variables, fragments);
            IReadOnlyCollection <FieldSelection> selections =
                collector.CollectFields(schema.QueryType,
                                        operation.SelectionSet, error => { });

            // assert
            Assert.Collection(selections,
                              selection =>
            {
                Assert.Collection(selection.Selection.SelectionSet.Selections,
                                  selectionNode =>
                {
                    var fragment = Assert.IsType <FragmentSpreadNode>(
                        selectionNode);
                    Assert.Equal("app", fragment.Name.Value);
                },
                                  selectionNode =>
                {
                    var fragment = Assert.IsType <FragmentSpreadNode>(
                        selectionNode);
                    Assert.Equal("parts", fragment.Name.Value);
                });
            });
        }
Exemplo n.º 19
0
        public void InvalidFieldError()
        {
            // arrange
            var          errorRaised = false;
            Schema       schema      = CreateSchema();
            DocumentNode query       = Parser.Default.Parse(@"
                {
                    a
                    x:c
                    xyz
                }
            ");

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

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().First();

            // act
            var fieldResolver = new FieldCollector(variables, fragments);
            IReadOnlyCollection <FieldSelection> fields = fieldResolver
                                                          .CollectFields(schema.GetType <ObjectType>("Foo"),
                                                                         operation.SelectionSet, e => { errorRaised = true; });

            // assert
            Assert.Collection(fields,
                              f =>
            {
                Assert.Equal("a", f.ResponseName);
                Assert.Equal("a", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            },
                              f =>
            {
                Assert.Equal("x", f.ResponseName);
                Assert.Equal("c", f.Field.Name);
                Assert.Equal("String", f.Field.Type.TypeName());
            });
            Assert.True(errorRaised);
        }
Exemplo n.º 20
0
        public FieldCollector(
            FragmentCollection fragments,
            Func <ObjectField, FieldNode, FieldDelegate> middlewareFactory,
            ITypeConversion converter,
            IEnumerable <IArgumentCoercionHandler> argumentCoercionHandlers)
        {
            _fragments = fragments
                         ?? throw new ArgumentNullException(nameof(fragments));
            _factory = middlewareFactory
                       ?? throw new ArgumentNullException(nameof(middlewareFactory));
            _converter = converter
                         ?? throw new ArgumentNullException(nameof(converter));

            _coercionHandlers = argumentCoercionHandlers is null
                ? Array.Empty <IArgumentCoercionHandler>()
                : argumentCoercionHandlers.ToArray();

            _coerceArgumentValue = CoerceArgumentValue;
        }
Exemplo n.º 21
0
        public ExecutionContext(Schema schema, DocumentNode queryDocument,
                                OperationDefinitionNode operation, VariableCollection variables,
                                IServiceProvider services, object rootValue, object userContext)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }

            if (queryDocument == null)
            {
                throw new ArgumentNullException(nameof(queryDocument));
            }

            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            if (variables == null)
            {
                throw new ArgumentNullException(nameof(variables));
            }

            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            Schema        = schema;
            QueryDocument = queryDocument;
            Operation     = operation;
            Variables     = variables;
            RootValue     = rootValue;
            UserContext   = userContext;
            _services     = services;

            Data          = new OrderedDictionary();
            Fragments     = new FragmentCollection(schema, queryDocument);
            FieldResolver = new FieldResolver(schema, variables, Fragments);
            Errors        = new List <IQueryError>();
            NextBatch     = new List <FieldResolverTask>();
        }
Exemplo n.º 22
0
        public void MergeMerged()
        {
            // arrange
            DocumentNode query = Utf8GraphQLParser.Parse(
                FileResource.Open("MergeQuery.graphql"));

            OperationDefinitionNode operation =
                query.Definitions.OfType <OperationDefinitionNode>().Single();

            var schema = Schema.Create(
                FileResource.Open("MergeSchema.graphql"),
                c => c.Use(next => context => Task.CompletedTask));

            var fragments = new FragmentCollection(schema, query);

            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("foo"));

            // act
            selections = collector.CollectFields(
                schema.GetType <ObjectType>("Application"),
                selections.Single().Selection.SelectionSet,
                Path.New("bat"));

            // assert
            Assert.Collection(selections,
                              selection => Assert.Equal("id", selection.ResponseName),
                              selection => Assert.Equal("name", selection.ResponseName),
                              selection => Assert.Equal("parts", selection.ResponseName));
        }
Exemplo n.º 23
0
        public ExecutionContext(
            ISchema schema,
            DirectiveLookup directiveLookup,
            DocumentNode queryDocument,
            OperationDefinitionNode operation,
            OperationRequest request,
            VariableCollection variables,
            CancellationToken requestAborted)
        {
            Schema = schema
                     ?? throw new ArgumentNullException(nameof(schema));
            _directiveLookup = directiveLookup
                               ?? throw new ArgumentNullException(nameof(directiveLookup));
            QueryDocument = queryDocument
                            ?? throw new ArgumentNullException(nameof(queryDocument));
            Operation = operation
                        ?? throw new ArgumentNullException(nameof(operation));
            Variables = variables
                        ?? throw new ArgumentNullException(nameof(variables));
            _request = request
                       ?? throw new ArgumentNullException(nameof(request));

            Services       = _serviceFactory.Services = request.Services;
            _resolverCache = request.Session?.CustomContexts
                             .GetCustomContext <IResolverCache>();
            ErrorHandler = Services.GetRequiredService <IErrorHandler>();

            Fragments       = new FragmentCollection(schema, queryDocument);
            _fieldCollector = new FieldCollector(variables, Fragments);
            OperationType   = schema.GetOperationType(operation.Operation);
            RootValue       = ResolveRootValue(request.Services, schema,
                                               OperationType, request.InitialValue);

            if (RootValue == null)
            {
                RootValue         = CreateRootValue(Services, schema, OperationType);
                _disposeRootValue = true;
            }

            RequestAborted = requestAborted;
        }
Exemplo n.º 24
0
        public void MergeMerged()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse(
                FileResource.Open("MergeQuery.graphql"));

            OperationDefinitionNode operation =
                query.Definitions.OfType <OperationDefinitionNode>().Single();

            Schema schema = Schema.Create(
                FileResource.Open("MergeSchema.graphql"),
                c => c.Use(next => context => Task.CompletedTask));

            var fragments = new FragmentCollection(schema, query);

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

            var collector = new FieldCollector(variables, fragments);

            IReadOnlyCollection <FieldSelection> selections =
                collector.CollectFields(schema.QueryType,
                                        operation.SelectionSet, error => { });

            // act
            selections = collector.CollectFields(
                schema.GetType <ObjectType>("Application"),
                selections.Single().Selection.SelectionSet,
                error => { });

            // assert
            Assert.Collection(selections,
                              selection => Assert.Equal("id", selection.ResponseName),
                              selection => Assert.Equal("name", selection.ResponseName),
                              selection => Assert.Equal("parts", selection.ResponseName));
        }
Exemplo n.º 25
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 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
            IReadOnlyDictionary <NameString, ArgumentValue> arguments =
                selection.CoerceArguments(variables, TypeConversion.Default);

            // assert
            MatchSnapshot(arguments);
        }
Exemplo n.º 26
0
        public ExecutionContext(
            ISchema schema,
            DocumentNode queryDocument,
            OperationDefinitionNode operation,
            OperationRequest request,
            VariableCollection variables)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            Schema = schema
                     ?? throw new ArgumentNullException(nameof(schema));

            Services = request.Services;
            _session = request.Session;

            QueryDocument = queryDocument
                            ?? throw new ArgumentNullException(nameof(queryDocument));
            Operation = operation
                        ?? throw new ArgumentNullException(nameof(operation));
            Variables = variables
                        ?? throw new ArgumentNullException(nameof(variables));

            Fragments       = new FragmentCollection(schema, queryDocument);
            _fieldCollector = new FieldCollector(variables, Fragments);
            OperationType   = schema.GetOperationType(operation.Operation);
            RootValue       = ResolveRootValue(request.Services, schema,
                                               OperationType, request.InitialValue);
            if (RootValue == null)
            {
                RootValue         = CreateRootValue(Services, schema, OperationType);
                _disposeRootValue = true;
            }
        }
Exemplo n.º 27
0
        public FieldResolver(
            Schema schema,
            VariableCollection variables,
            FragmentCollection fragments)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }

            if (variables == null)
            {
                throw new ArgumentNullException(nameof(variables));
            }

            if (fragments == null)
            {
                throw new ArgumentNullException(nameof(fragments));
            }

            _schema    = schema;
            _variables = variables;
            _fragments = fragments;
        }