Example #1
0
        public static IReadOnlyDictionary <SelectionSetNode, PSS> Compile(
            ISchema schema,
            FragmentCollection fragments,
            OperationDefinitionNode operation,
            IEnumerable <ISelectionSetOptimizer>?optimizers = null)
        {
            var selectionSets = new Dictionary <SelectionSetNode, PSS>();

            void Register(PreparedSelectionSet s) => selectionSets[s.SelectionSet] = s;

            SelectionSetNode selectionSet = operation.SelectionSet;
            ObjectType       typeContext  = schema.GetOperationType(operation.Operation);
            var root = new PSS(operation.SelectionSet);

            Register(root);

            var collector = new OperationCompiler(schema, fragments);

            collector.Visit(
                selectionSet,
                typeContext,
                new Stack <IObjectField>(),
                root,
                Register,
                new Dictionary <ISelectionNode, SelectionIncludeCondition>(),
                optimizers?.ToList() ?? new List <ISelectionSetOptimizer>());

            return(selectionSets);
        }
Example #2
0
 private OperationCompiler(
     ISchema schema,
     FragmentCollection fragments)
 {
     _schema    = schema;
     _fragments = fragments;
 }
Example #3
0
        public void Object_Field_Visibility_Is_Correctly_Inherited_2()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) => name.Equals("v"));

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean, $q: Boolean) {
                    hero(episode: EMPIRE) @include(if: $v) {
                        name @include(if: $q)
                    }
                    ... on Query {
                        hero(episode: EMPIRE) {
                            id
                        }
                    }
                    ... @include(if: $v) {
                        hero(episode: EMPIRE) {
                            height
                        }
                    }
                }");

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

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, PreparedSelectionSet> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            var op = new PreparedOperation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);
            IPreparedSelectionList rootSelections =
                op.RootSelectionSet.GetSelections(op.RootSelectionSet.GetPossibleTypes().First());
            IPreparedSelectionList droidSelections =
                op.GetSelections(rootSelections[0].SelectionSet !, droid);

            Assert.Collection(
                droidSelections.Where(t => t.IsIncluded(variables.Object)),
                t => Assert.Equal("id", t.ResponseName),
                t => Assert.Equal("height", t.ResponseName));

            op.Print().MatchSnapshot();
        }
Example #4
0
        public void Field_Is_Visible_When_One_Selection_Is_Visible_3()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) =>
            {
                return(name.Equals("q"));
            });

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean, $q: Boolean){
                    hero(episode: EMPIRE) {
                        name @include(if: $v)
                        ... abc @include(if: $q)
                    }
                }

                fragment abc on Droid {
                    name
                }");

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

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, PreparedSelectionSet> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            var op = new PreparedOperation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);
            IPreparedSelectionList rootSelections =
                op.RootSelectionSet.GetSelections(op.RootSelectionSet.GetPossibleTypes().First());
            IPreparedSelectionList droidSelections =
                op.GetSelections(rootSelections[0].SelectionSet !, droid);

            Assert.Equal("name", droidSelections[0].ResponseName);
            Assert.True(droidSelections[0].IsConditional);
            Assert.True(droidSelections[0].IsIncluded(variables.Object));
            Assert.True(droidSelections.IsConditional);
        }
Example #5
0
        public void Prepare_Inline_Fragment()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                hero(episode: EMPIRE) {
                    name
                    ... on Droid {
                        primaryFunction
                    }
                    ... on Human {
                        homePlanet
                    }
                }
             }");

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

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, PreparedSelectionSet> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            IPreparedSelection hero = selectionSets[operation.SelectionSet].GetSelections(schema.QueryType).Single();

            Assert.Equal("hero", hero.ResponseName);

            Assert.Collection(
                selectionSets[hero.SelectionSet].GetSelections(schema.GetType <ObjectType>("Droid")),
                selection => Assert.Equal("name", selection.ResponseName),
                selection => Assert.Equal("primaryFunction", selection.ResponseName));

            Assert.Collection(
                selectionSets[hero.SelectionSet].GetSelections(schema.GetType <ObjectType>("Human")),
                selection => Assert.Equal("name", selection.ResponseName),
                selection => Assert.Equal("homePlanet", selection.ResponseName));

            var op = new PreparedOperation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);

            op.Print().MatchSnapshot();
        }
Example #6
0
        public void Field_Based_Optimizers()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) => name.Equals("v"));

            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(d => d
                                           .Name("Query")
                                           .Field("root")
                                           .Resolve(new Foo())
                                           .UseOptimizer(new SimpleOptimizer()))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    root {
                        bar {
                            text
                        }
                    }
                }");

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

            var fragments = new FragmentCollection(schema, document);

            var optimizers = new List <NoopOptimizer> {
                new NoopOptimizer()
            };

            // act
            IReadOnlyDictionary <SelectionSetNode, PreparedSelectionSet> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation, optimizers);

            // assert
            var op = new PreparedOperation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);

            op.Print().MatchSnapshot();
        }
Example #7
0
        public void Prepare_Fragment_Definition()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                hero(episode: EMPIRE) {
                    name
                    ... abc
                    ... def
                }
              }

              fragment abc on Droid {
                  primaryFunction
              }

              fragment def on Human {
                  homePlanet
              }
             ");

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

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, PreparedSelectionSet> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            var op = new PreparedOperation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);

            op.Print().MatchSnapshot();
        }