public void If_Arguments_For_Object_Collection_Fields_Are_Null_Or_Empty_Then_Arguments_For_Field_Selection_Are_Empty(
            IArgumentCollection arguments)
        {
            var customerSelectionSet = SelectionSetBuilder.For <Customer>()
                                       .AddScalarField(customer => customer.Id)
                                       .Build();

            var query = QueryOperationBuilder.ForSchema <SimpleSchema>(OperationTypeForFixture)
                        .AddObjectCollectionField(schema => schema.Customers, arguments, customerSelectionSet)
                        .AddObjectCollectionField("foobar", schema => schema.Customers, arguments, customerSelectionSet)
                        .Build();

            var expectedCustomerSelectionSet = new SelectionSet <Customer>(new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(Customer.Id))
            });
            var expectedSelections = new List <ISelectionSetItem>
            {
                new ObjectFieldSelectionItem(null, nameof(SimpleSchema.Customers), Enumerable.Empty <IArgument>(), expectedCustomerSelectionSet),
                new ObjectFieldSelectionItem("foobar", nameof(SimpleSchema.Customers), Enumerable.Empty <IArgument>(), expectedCustomerSelectionSet),
            };

            query.Should().NotBeNull();
            query.SelectionSet.Selections.Should()
            .BeEquivalentTo(expectedSelections, options => options.RespectingRuntimeTypes());
        }
        public void Then_Added_Fields_Are_Included_In_Selections()
        {
            var contactSelectionSet = SelectionSetBuilder.For <Contact>()
                                      .AddScalarField(contact => contact.FirstName)
                                      .AddScalarField("surname", contact => contact.LastName)
                                      .AddScalarCollectionField(contact => contact.Nicknames)
                                      .Build();

            var customerSelectionSet = SelectionSetBuilder.For <Customer>()
                                       .AddObjectField(customer => customer.CustomerContact, contactSelectionSet)
                                       .Build();

            var queryOperation = QueryOperationBuilder.ForSchema <SimpleSchema>(OperationTypeForFixture)
                                 .AddScalarField(schema => schema.Version)
                                 .AddScalarField("foobar", schema => schema.Version)
                                 .AddScalarCollectionField(schema => schema.PastVersions)
                                 .AddScalarCollectionField("versions", schema => schema.PastVersions)
                                 .AddObjectField(schema => schema.Administrator, contactSelectionSet)
                                 .AddObjectField("admin", schema => schema.Administrator, contactSelectionSet)
                                 .AddObjectCollectionField(schema => schema.Customers, customerSelectionSet)
                                 .AddObjectCollectionField("customers", schema => schema.Customers, customerSelectionSet)
                                 .Build();

            var expectedContactsSelectionSet = new SelectionSet <Contact>(
                new List <ISelectionSetItem>
            {
                new ObjectFieldSelectionItem(null, nameof(Contact.FirstName), null),
                new ObjectFieldSelectionItem("surname", nameof(Contact.LastName), null),
                new ObjectFieldSelectionItem(null, nameof(Contact.Nicknames), null),
            });

            var expectedCustomerSelectionSet = new SelectionSet <Customer>(
                new List <ISelectionSetItem>
            {
                new ObjectFieldSelectionItem(null, nameof(Customer.CustomerContact), expectedContactsSelectionSet)
            });

            var expectedQuerySelectionSet = new SelectionSet <SimpleSchema>(
                new List <ISelectionSetItem>
            {
                new ObjectFieldSelectionItem(null, nameof(SimpleSchema.Version), null),
                new ObjectFieldSelectionItem("foobar", nameof(SimpleSchema.Version), null),
                new ObjectFieldSelectionItem(null, nameof(SimpleSchema.PastVersions), null),
                new ObjectFieldSelectionItem("versions", nameof(SimpleSchema.PastVersions), null),
                new ObjectFieldSelectionItem(null, nameof(SimpleSchema.Administrator), expectedContactsSelectionSet),
                new ObjectFieldSelectionItem("admin", nameof(SimpleSchema.Administrator), expectedContactsSelectionSet),
                new ObjectFieldSelectionItem(null, nameof(SimpleSchema.Customers), expectedCustomerSelectionSet),
                new ObjectFieldSelectionItem("customers", nameof(SimpleSchema.Customers), expectedCustomerSelectionSet),
            });

            var expectedQueryOperation =
                new GraphQLQueryOperation <SimpleSchema>(OperationTypeForFixture, null, expectedQuerySelectionSet);

            queryOperation.Should().BeEquivalentTo(expectedQueryOperation, options => options.RespectingRuntimeTypes());
        }
        public void Then_Arguments_Are_Included_In_Field_Selections()
        {
            var customerSelectionSet = SelectionSetBuilder.For <Customer>()
                                       .AddScalarField(customer => customer.Id)
                                       .Build();

            var versionArgs = new ArgumentCollection
            {
                ArgumentBuilder.Build("foo", "hello world"),
                ArgumentBuilder.Build("bar", 10),
                ArgumentBuilder.Build("baz")
            };
            var expectedVersionArgs = new IArgument[versionArgs.Count];

            // copy the original arguments so our expectations aren't contaminated by the method under test
            versionArgs.CopyTo(expectedVersionArgs, 0);

            var customersArgs = new ArgumentCollection
            {
                ArgumentBuilder.Build("isActive", true),
                ArgumentBuilder.Build("age", 50.2)
            };
            var expectedCustomersArgs = new IArgument[customersArgs.Count];

            // copy the original arguments so our expectations aren't contaminated by the method under test
            customersArgs.CopyTo(expectedCustomersArgs, 0);

            var queryOperation = QueryOperationBuilder.ForSchema <SimpleSchema>(OperationTypeForFixture)
                                 .AddScalarCollectionField(schema => schema.PastVersions, versionArgs)
                                 .AddScalarCollectionField("versions", schema => schema.PastVersions, versionArgs)
                                 .AddObjectCollectionField(schema => schema.Customers, customersArgs, customerSelectionSet)
                                 .AddObjectCollectionField("foobar", schema => schema.Customers, customersArgs, customerSelectionSet)
                                 .Build();

            var expectedCustomerSelectionSet = new SelectionSet <Customer>(new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(Customer.Id))
            });

            var expectedSchemaSelections = new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(SimpleSchema.PastVersions), expectedVersionArgs),
                new ScalarFieldSelectionItem("versions", nameof(SimpleSchema.PastVersions), expectedVersionArgs),
                new ObjectFieldSelectionItem(null, nameof(SimpleSchema.Customers), expectedCustomersArgs,
                                             expectedCustomerSelectionSet),
                new ObjectFieldSelectionItem("foobar", nameof(SimpleSchema.Customers), expectedCustomersArgs,
                                             expectedCustomerSelectionSet),
            };

            queryOperation.Should().NotBeNull();
            queryOperation.SelectionSet.Selections.Should().BeEquivalentTo(expectedSchemaSelections,
                                                                           options => options.RespectingRuntimeTypes());
        }
        public void Then_Field_Arguments_Are_Rendered(
            [Values] GraphQLOperationType operationType,
            [Values(null, "", "   ", "MyQuery")] string operationName)
        {
            var phoneNumberSelectionSet = SelectionSetBuilder.For <PhoneNumber>()
                                          .AddScalarField(phone => phone.Number)
                                          .Build();

            var phoneArguments = new ArgumentCollection
            {
                ArgumentBuilder.Build("areaCode", "231"),
                ArgumentBuilder.Build("foobar")
            };


            var contactSelectionSet = SelectionSetBuilder.For <Contact>()
                                      .AddObjectCollectionField("michiganNumbers", contact => contact.PhoneNumbers, phoneArguments,
                                                                phoneNumberSelectionSet)
                                      .Build();

            var numberArguments = new ArgumentCollection
            {
                ArgumentBuilder.Build("isEven", true),
                ArgumentBuilder.Build("greaterThan", 22)
            };

            var customerSelectionSet = SelectionSetBuilder.For <Customer>()
                                       .AddScalarCollectionField("favEvenNumbersGreaterThan22", customer => customer.FavoriteNumbers,
                                                                 numberArguments)
                                       .Build();

            var customersArguments = new ArgumentCollection
            {
                ArgumentBuilder.Build("isActive", true),
                ArgumentBuilder.Build("lastName", "Smith"),
                ArgumentBuilder.Build("minRating", 3.2)
            };

            var query = QueryOperationBuilder.ForSchema <SimpleSchema>(operationType, operationName)
                        .AddObjectCollectionField(schema => schema.Customers, customersArguments, customerSelectionSet)
                        .AddObjectCollectionField("foobar", schema => schema.Customers, customersArguments,
                                                  customerSelectionSet)
                        .AddObjectField(schema => schema.Administrator, contactSelectionSet)
                        .Build();

            var result = new QueryRenderer().Render(query);

            Snapshot.Match(result);
        }
        public void If_Arguments_For_Scalar_Collection_Fields_Are_Null_Or_Empty_Then_Arguments_For_Field_Selection_Are_Empty(
            IArgumentCollection arguments)
        {
            var query = QueryOperationBuilder.ForSchema <SimpleSchema>(OperationTypeForFixture)
                        .AddScalarCollectionField(schema => schema.PastVersions, arguments)
                        .AddScalarCollectionField("foobar", schema => schema.PastVersions, arguments)
                        .Build();

            var expectedSelections = new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(SimpleSchema.PastVersions), Enumerable.Empty <IArgument>()),
                new ScalarFieldSelectionItem("foobar", nameof(SimpleSchema.PastVersions), Enumerable.Empty <IArgument>()),
            };

            query.Should().NotBeNull();
            query.SelectionSet.Selections.Should()
            .BeEquivalentTo(expectedSelections, options => options.RespectingRuntimeTypes());
        }
        public void Then_Added_Fields_Are_Rendered(
            [Values] GraphQLOperationType operationType,
            [Values(null, "", "   ", "MyQuery")] string operationName)
        {
            var addressSelectionSet = SelectionSetBuilder.For <Address>()
                                      .AddScalarField("line1", address => address.Street1)
                                      .AddScalarField("line2", address => address.Street2)
                                      .AddScalarField(address => address.City)
                                      .AddScalarField(address => address.State)
                                      .AddScalarField(address => address.ZipCode)
                                      .Build();

            var phoneNumberSelectionSet = SelectionSetBuilder.For <PhoneNumber>()
                                          .AddScalarField(phone => phone.Number)
                                          .AddScalarField("ext", phone => phone.Extension)
                                          .Build();

            var contactSelectionSet = SelectionSetBuilder.For <Contact>()
                                      .AddScalarField(contact => contact.FirstName)
                                      .AddScalarField("surname", contact => contact.LastName)
                                      .AddScalarCollectionField("names", contact => contact.Nicknames)
                                      .AddObjectField(contact => contact.Address, addressSelectionSet)
                                      .AddObjectCollectionField(contact => contact.PhoneNumbers, phoneNumberSelectionSet)
                                      .AddObjectCollectionField("foobar", contact => contact.PhoneNumbers, phoneNumberSelectionSet)
                                      .Build();

            var customerSelectionSet = SelectionSetBuilder.For <Customer>()
                                       .AddScalarField(customer => customer.Id)
                                       .AddScalarField("acctNum", customer => customer.AccountNumber)
                                       .AddObjectField("contactInfo", customer => customer.CustomerContact, contactSelectionSet)
                                       .AddScalarCollectionField(customer => customer.FavoriteNumbers)
                                       .Build();

            var queryOperation = QueryOperationBuilder.ForSchema <SimpleSchema>(operationType, operationName)
                                 .AddScalarField(schema => schema.Version)
                                 .AddScalarCollectionField("versions", schema => schema.PastVersions)
                                 .AddObjectCollectionField("users", schema => schema.Customers, customerSelectionSet)
                                 .AddObjectField("admin", schema => schema.Administrator, contactSelectionSet)
                                 .Build();

            var result = new QueryRenderer().Render(queryOperation);

            Snapshot.Match(result);
        }
Exemple #7
0
 protected IQueryOperationBuilder <T> CreateBuilderFor <T>(string operationName = null) where T : class
 {
     return(QueryOperationBuilder.ForSchema <T>(OperationTypeForFixture, operationName));
 }