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());
        }
Ejemplo n.º 2
0
        public void Then_String_Argument_Values_Are_Serialized()
        {
            var argumentValue =
                @"Lorem ipsum dolor sit amet, ""consectetur adipiscing"" elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in 'voluptate' velit esse cillum dolore eu fugiat nulla pariatur.

    Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

            var argument = ArgumentBuilder.Build("description", argumentValue);

            var selectionSet = SelectionSetBuilder.For <Customer>()
                               .AddScalarField(customer => customer.Id, new ArgumentCollection {
                argument
            })
                               .Build();

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

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Snapshot.Match(result, new SnapshotNameExtension("Windows"));
            }
            else
            {
                Snapshot.Match(result);
            }
        }
        public void If_Arguments_For_Object_Collection_Fields_Are_Null_Or_Empty_Then_Arguments_For_Field_Selection_Are_Empty(
            IArgumentCollection arguments)
        {
            var phoneNumberSelectionSet = SelectionSetBuilder.For <PhoneNumber>()
                                          .AddScalarField(phone => phone.Number)
                                          .AddScalarField(phone => phone.Extension)
                                          .Build();

            var selectionSet = SelectionSetBuilder.For <Contact>()
                               .AddObjectCollectionField(contact => contact.PhoneNumbers, arguments, phoneNumberSelectionSet)
                               .AddObjectCollectionField("foobar", contact => contact.PhoneNumbers, arguments, phoneNumberSelectionSet)
                               .Build();

            var expectedPhoneNumberSelectionSet = new SelectionSet <PhoneNumber>(
                new ISelectionSetItem[]
            {
                new ScalarFieldSelectionItem(null, nameof(PhoneNumber.Number)),
                new ScalarFieldSelectionItem(null, nameof(PhoneNumber.Extension))
            });

            var expectedSelections = new ISelectionSetItem[]
            {
                new ObjectFieldSelectionItem(null, nameof(Contact.PhoneNumbers), Enumerable.Empty <IArgument>(), expectedPhoneNumberSelectionSet),
                new ObjectFieldSelectionItem("foobar", nameof(Contact.PhoneNumbers), Enumerable.Empty <IArgument>(), expectedPhoneNumberSelectionSet),
            };

            selectionSet.Should().NotBeNull();
            selectionSet.Selections.Should()
            .BeEquivalentTo(expectedSelections, options => options.RespectingRuntimeTypes());
        }
        public void If_Added_Properties_Are_Classes_Then_They_Are_In_The_Selection_Set()
        {
            var contactSelectionSet = SelectionSetBuilder.For <Contact>()
                                      .AddScalarField(contact => contact.FirstName)
                                      .AddScalarField(contact => contact.LastName)
                                      .Build();

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

            var expectedContactSelectionSet = new SelectionSet <Contact>(
                new ISelectionSetItem[]
            {
                new ScalarFieldSelectionItem(null, nameof(Contact.FirstName)),
                new ScalarFieldSelectionItem(null, nameof(Contact.LastName)),
            });
            var expectedCustomerSelections = new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(Customer.Id)),
                new ObjectFieldSelectionItem(null, nameof(Customer.CustomerContact), expectedContactSelectionSet),
                new ObjectFieldSelectionItem("foobar", nameof(Customer.CustomerContact), expectedContactSelectionSet),
            };

            customerSelectionSet.Should().NotBeNull();
            customerSelectionSet.Selections.Should().BeEquivalentTo(expectedCustomerSelections, options => options.RespectingRuntimeTypes());
        }
        public void If_Arguments_For_Object_Fields_Are_Null_Or_Empty_Then_Arguments_For_Field_Selection_Are_Empty(
            IArgumentCollection arguments)
        {
            var contactSelection = SelectionSetBuilder.For <Contact>()
                                   .AddScalarField(contact => contact.FirstName)
                                   .Build();

            var selectionSet = SelectionSetBuilder.For <Customer>()
                               .AddObjectField(customer => customer.CustomerContact, arguments, contactSelection)
                               .AddObjectField("foobar", customer => customer.CustomerContact, arguments, contactSelection)
                               .Build();

            var expectedContactSelectionSet = new SelectionSet <Contact>(
                new ISelectionSetItem[]
            {
                new ScalarFieldSelectionItem(null, nameof(Contact.FirstName))
            });

            var expectedSelections = new ISelectionSetItem[]
            {
                new ObjectFieldSelectionItem(null, nameof(Customer.CustomerContact), Enumerable.Empty <IArgument>(), expectedContactSelectionSet),
                new ObjectFieldSelectionItem("foobar", nameof(Customer.CustomerContact), Enumerable.Empty <IArgument>(), expectedContactSelectionSet),
            };

            selectionSet.Should().NotBeNull();
            selectionSet.Selections.Should()
            .BeEquivalentTo(expectedSelections, options => options.RespectingRuntimeTypes());
        }
        public void If_Property_Alias_Is_Not_A_Valid_GraphQL_Name_Then_ArgumentException_Is_Thrown_Stating_Alias_Is_Not_Valid(
            string alias, string because)
        {
            var builder = SelectionSetBuilder.For <Contact>();

            Action method = () => builder.AddScalarCollectionField(alias, contact => contact.Nicknames);

            Invoking(method).Should().ThrowInvalidGraphQLNameException("alias", because);
        }
        public void If_No_Fields_Are_Selected_Before_Building_Then_An_InvalidOperationException_Is_Thrown()
        {
            var builder = SelectionSetBuilder.For <Customer>();

            Invoking(builder.Build).Should()
            .ThrowExactly <InvalidOperationException>("because a selection set must include 1 or more fields")
            .WithMessage("A selection set must include one or more fields.")
            .Where(e => e.HelpLink == "https://spec.graphql.org/June2018/#SelectionSet");
        }
Ejemplo n.º 8
0
        public void If_Property_Alias_Is_Not_A_Valid_GraphQL_Name_Then_ArgumentException_Is_Thrown_Stating_Alias_Is_Not_Valid(
            string alias, string because)
        {
            var builder          = SelectionSetBuilder.For <Customer>();
            var fakeSelectionSet = Mock.Of <ISelectionSet <Contact> >();

            Action method = () => builder.AddObjectField(alias, customer => customer.CustomerContact, fakeSelectionSet);

            Invoking(method).Should().ThrowInvalidGraphQLNameException("alias", because);
        }
        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 phoneNumberSelection = SelectionSetBuilder.For <PhoneNumber>()
                                       .AddScalarField(phone => phone.Number)
                                       .Build();
            var phoneArgs = new ArgumentCollection
            {
                ArgumentBuilder.Build("areaCode", "231")
            };
            var expectedPhoneArgs = new IArgument[phoneArgs.Count];

            // copy arguments so we don't contaminate expectations
            phoneArgs.CopyTo(expectedPhoneArgs, 0);

            var contactSelection = SelectionSetBuilder.For <Contact>()
                                   .AddObjectCollectionField(contact => contact.PhoneNumbers, phoneArgs, phoneNumberSelection)
                                   .AddObjectCollectionField("foobar", contact => contact.PhoneNumbers, phoneArgs, phoneNumberSelection)
                                   .Build();

            var numbersArgs = new ArgumentCollection
            {
                ArgumentBuilder.Build("greaterThan", 10),
                ArgumentBuilder.Build("isEven", true)
            };
            var expectedNumbersArgs = new IArgument[numbersArgs.Count];

            // copy arguments so we don't contaminate expectations
            numbersArgs.CopyTo(expectedNumbersArgs, 0);

            var customerSelection = SelectionSetBuilder.For <Customer>()
                                    .AddScalarCollectionField(customer => customer.FavoriteNumbers, numbersArgs)
                                    .AddScalarCollectionField("myNumbers", customer => customer.FavoriteNumbers, numbersArgs)
                                    .AddObjectField(customer => customer.CustomerContact, contactSelection)
                                    .Build();

            var expectedPhoneSelection = new SelectionSet <PhoneNumber>(new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(PhoneNumber.Number))
            });
            var expectedContactSelection = new SelectionSet <Contact>(new List <ISelectionSetItem>
            {
                new ObjectFieldSelectionItem(null, nameof(Contact.PhoneNumbers), expectedPhoneArgs, expectedPhoneSelection),
                new ObjectFieldSelectionItem("foobar", nameof(Contact.PhoneNumbers), expectedPhoneArgs, expectedPhoneSelection),
            });
            var expectedCustomerSelections = new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(Customer.FavoriteNumbers), expectedNumbersArgs),
                new ScalarFieldSelectionItem("myNumbers", nameof(Customer.FavoriteNumbers), expectedNumbersArgs),
                new ObjectFieldSelectionItem(null, nameof(Customer.CustomerContact), expectedContactSelection)
            };

            customerSelection.Should().NotBeNull();
            customerSelection.Selections.Should()
            .BeEquivalentTo(expectedCustomerSelections, 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());
        }
Ejemplo n.º 12
0
        public void Then_Arguments_Are_Rendered_Properly()
        {
            var addressSelectionSet = SelectionSetBuilder.For <Address>()
                                      .AddScalarField("postalCode", address => address.ZipCode)
                                      .Build();
            var phoneSelectionSet = SelectionSetBuilder.For <PhoneNumber>()
                                    .AddScalarField(phone => phone.Number)
                                    .Build();

            var nameArguments = new ArgumentCollection
            {
                ArgumentBuilder.Build("initials", true),
                ArgumentBuilder.Build("length", 3),
                ArgumentBuilder.Build("filter", (string)null)
            };
            var addressArguments = new ArgumentCollection
            {
                ArgumentBuilder.Build("state", "CA"),
                ArgumentBuilder.Build("withApartments", false),
                ArgumentBuilder.Build("distanceFromDc", 300.25)
            };
            var phoneNumberArguments = new ArgumentCollection
            {
                ArgumentBuilder.Build("areaCode", "231"),
                ArgumentBuilder.Build("foobar")
            };

            var contactSelectionSet = SelectionSetBuilder.For <Contact>()
                                      .AddScalarField(contact => contact.FirstName, nameArguments)
                                      .AddObjectField(contact => contact.Address, addressArguments, addressSelectionSet)
                                      .AddObjectCollectionField("phones", contact => contact.PhoneNumbers, phoneNumberArguments, phoneSelectionSet)
                                      .Build();

            var contactArguments = new ArgumentCollection
            {
                ArgumentBuilder.Build("isActive", true)
            };
            var favoriteNumberArguments = new ArgumentCollection
            {
                ArgumentBuilder.Build("isEven", false),
                ArgumentBuilder.Build("greaterThan", 10)
            };

            var customerSelectionSet = SelectionSetBuilder.For <Customer>()
                                       .AddObjectField(customer => customer.CustomerContact, contactArguments, contactSelectionSet)
                                       .AddScalarCollectionField("favoriteOddNumbersGreaterThan10", customer => customer.FavoriteNumbers, favoriteNumberArguments)
                                       .Build();

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

            Snapshot.Match(result);
        }
        public void If_Added_Properties_Are_Collection_Then_They_Are_In_The_Selection_Set()
        {
            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)
                                      .AddObjectCollectionField(contact => contact.PhoneNumbers, phoneNumberSelectionSet)
                                      .AddObjectCollectionField("foobar", contact => contact.PhoneNumbers, phoneNumberSelectionSet)
                                      .AddScalarCollectionField("names", contact => contact.Nicknames)
                                      .Build();

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

            var expectedPhoneNumberSelectionSet = new SelectionSet <PhoneNumber>(
                new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(PhoneNumber.Number)),
                new ScalarFieldSelectionItem("ext", nameof(PhoneNumber.Extension))
            });

            var expectedContactSelection = new SelectionSet <Contact>(
                new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(Contact.FirstName)),
                new ScalarFieldSelectionItem("surname", nameof(Contact.LastName)),
                new ObjectFieldSelectionItem(null, nameof(Contact.PhoneNumbers), expectedPhoneNumberSelectionSet),
                new ObjectFieldSelectionItem("foobar", nameof(Contact.PhoneNumbers), expectedPhoneNumberSelectionSet),
                new ScalarFieldSelectionItem("names", nameof(Contact.Nicknames))
            });

            var expectedCustomerSelections = new List <ISelectionSetItem>
            {
                new ScalarFieldSelectionItem(null, nameof(Customer.Id)),
                new ScalarFieldSelectionItem(null, nameof(Customer.FavoriteNumbers)),
                new ScalarFieldSelectionItem("baz", nameof(Customer.FavoriteNumbers)),
                new ObjectFieldSelectionItem(null, nameof(Customer.CustomerContact), expectedContactSelection)
            };

            customerSelectionSet.Should().NotBeNull();
            customerSelectionSet.Selections.Should().BeEquivalentTo(expectedCustomerSelections, 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);
        }
Ejemplo n.º 15
0
        public void If_Property_Expression_Is_Null_Then_ArgumentNullException_Is_Thrown()
        {
            var builder = SelectionSetBuilder.For <Customer>();

            Action addingFieldWithoutAlias = () => builder.AddScalarField <Customer, Guid>(null);
            Action addingFieldWithAlias    = () => builder.AddScalarField <Customer, Guid>("foo", null);

            using (new AssertionScope())
            {
                foreach (var addingField in new[] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <ArgumentNullException>();
                }
            }
        }
Ejemplo n.º 16
0
        public void If_Expression_Is_Not_A_Property_Of_The_Class_Then_An_InvalidOperationException_Is_Thrown()
        {
            var builder = SelectionSetBuilder.For <Customer>();

            Action addingFieldWithoutAlias = () => builder.AddScalarField(customer => customer.CustomerContact.FirstName);
            Action addingFieldWithAlias    = () => builder.AddScalarField("foo", customer => customer.CustomerContact.FirstName);

            using (new AssertionScope())
            {
                foreach (var addingField in new [] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <InvalidOperationException>()
                    .WithMessage($"Property '*' is not a member of type '{typeof(Customer).FullName}'.");
                }
            }
        }
        public void Then_Added_Properties_Are_In_The_Selection_Set()
        {
            var selectionSet = SelectionSetBuilder.For <Customer>()
                               .AddScalarField(customer => customer.Id)
                               .AddScalarField("acctNum", customer => customer.AccountNumber)
                               .Build();

            var expectedSelections = new List <IFieldSelectionItem>
            {
                new ScalarFieldSelectionItem(null, nameof(Customer.Id)),
                new ScalarFieldSelectionItem("acctNum", nameof(Customer.AccountNumber))
            };

            selectionSet.Should().NotBeNull();
            selectionSet.Selections.Should().BeEquivalentTo(expectedSelections, options => options.RespectingRuntimeTypes());
        }
Ejemplo n.º 18
0
        public void If_Selection_Set_Is_Null_Then_An_ArgumentNullException_Is_Thrown()
        {
            var builder = SelectionSetBuilder.For <Customer>();

            Action addingFieldWithoutAlias = () => builder.AddObjectField(customer => customer.CustomerContact, null);
            Action addingFieldWithAlias    = () => builder.AddObjectField("foo", customer => customer.CustomerContact, null);

            using (new AssertionScope())
            {
                foreach (var addingField in new [] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <ArgumentNullException>()
                    .Where(e => e.ParamName == "selectionSet");
                }
            }
        }
        public void If_Property_Is_A_GraphQL_Object_Type_Then_InvalidOperationException_Is_Thrown()
        {
            var builder = SelectionSetBuilder.For <Contact>();

            Action addingFieldWithoutAlias = () => builder.AddScalarCollectionField(contact => contact.PhoneNumbers);
            Action addingFieldWithAlias    = () => builder.AddScalarCollectionField("foo", contact => contact.PhoneNumbers);

            using (new AssertionScope())
            {
                foreach (var addingField in new [] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <InvalidOperationException>("because there is an overload for class properties")
                    .WithMessage($"The type '{typeof(PhoneNumber).FullName}' is not a GraphQL scalar type.")
                    .Where(e => e.HelpLink == "http://spec.graphql.org/June2018/#sec-Scalars");
                }
            }
        }
        public void If_Expression_Is_Not_A_Member_Expression_Then_An_ArgumentException_Is_Thrown()
        {
            var builder = SelectionSetBuilder.For <Contact>();

            Action addingFieldWithoutAlias = () => builder.AddScalarCollectionField(_ => new [] { "hello" });
            Action addingFieldWithAlias    = () => builder.AddScalarCollectionField("foo", _ => new [] { "hello" });

            using (new AssertionScope())
            {
                foreach (var addingField in new [] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <ArgumentException>()
                    .WithMessage($"A {nameof(MemberExpression)} was not provided.*")
                    .Where(e => e.ParamName == "expression");
                }
            }
        }
Ejemplo n.º 21
0
        public void If_Property_Expression_Is_Null_Then_ArgumentNullException_Is_Thrown()
        {
            var builder          = SelectionSetBuilder.For <Customer>();
            var fakeSelectionSet = Mock.Of <ISelectionSet <Contact> >();

            Action addingFieldWithoutAlias = () => builder.AddObjectField(null, fakeSelectionSet);
            Action addingFieldWithAlias    = () => builder.AddObjectField("foo", null, fakeSelectionSet);

            using (new AssertionScope())
            {
                foreach (var addingField in new [] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <ArgumentNullException>()
                    .Where(e => e.ParamName == "expression");
                }
            }
        }
        public void If_Arguments_For_Scalar_Collection_Fields_Are_Null_Or_Empty_Then_Arguments_For_Field_Selection_Are_Empty(
            IArgumentCollection arguments)
        {
            var selectionSet = SelectionSetBuilder.For <Customer>()
                               .AddScalarCollectionField(customer => customer.FavoriteNumbers, arguments)
                               .AddScalarCollectionField("numbers", customer => customer.FavoriteNumbers, arguments)
                               .Build();

            var expectedSelections = new ISelectionSetItem[]
            {
                new ScalarFieldSelectionItem(null, nameof(Customer.FavoriteNumbers)),
                new ScalarFieldSelectionItem("numbers", nameof(Customer.FavoriteNumbers)),
            };

            selectionSet.Should().NotBeNull();
            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);
        }
Ejemplo n.º 24
0
        public void If_A_Collection_Of_Strings_Is_Added_With_A_Selection_Set_Then_An_InvalidOperationException_Is_Thrown_Stating_A_String_Does_Not_Need_A_Selection_Set()
        {
            var builder          = SelectionSetBuilder.For <Contact>();
            var fakeSelectionSet = Mock.Of <ISelectionSet <string> >();

            Action addingFieldWithoutAlias = () => builder.AddObjectCollectionField(contact => contact.Nicknames, fakeSelectionSet);
            Action addingFieldWithAlias    = () => builder.AddObjectCollectionField("foobar", contact => contact.Nicknames, fakeSelectionSet);

            using (new AssertionScope())
            {
                foreach (var addingField in new[] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <InvalidOperationException>()
                    .WithMessage(
                        $"The type '{typeof(string).FullName}' is not a GraphQL object type. Use the {nameof(builder.AddScalarCollectionField)} method.")
                    .Where(e => e.HelpLink == "http://spec.graphql.org/June2018/#sec-Objects");
                }
            }
        }
Ejemplo n.º 25
0
        public void If_Expression_Is_Not_A_Property_Of_The_Class_Then_An_InvalidOperationException_Is_Thrown()
        {
            var builder          = SelectionSetBuilder.For <Customer>();
            var fakeSelectionSet = Mock.Of <ISelectionSet <PhoneNumber> >();

            Action addingFieldWithoutAlias = () =>
                                             builder.AddObjectCollectionField(schema => schema.CustomerContact.PhoneNumbers, fakeSelectionSet);
            Action addingFieldWithAlias = () =>
                                          builder.AddObjectCollectionField("foo", schema => schema.CustomerContact.PhoneNumbers, fakeSelectionSet);

            using (new AssertionScope())
            {
                foreach (var addingField in new [] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <InvalidOperationException>()
                    .WithMessage($"Property '*' is not a member of type '{typeof(Customer).FullName}'.");
                }
            }
        }
Ejemplo n.º 26
0
        public void If_Expression_Is_A_String_Property_Then_An_InvalidOperationException_Is_Thrown_Stating_To_Use_The_Other_Method_Overload()
        {
            var builder          = SelectionSetBuilder.For <Customer>();
            var fakeSelectionSet = Mock.Of <ISelectionSet <string> >();

            Action addingFieldWithoutAlias = () =>
                                             builder.AddObjectField(customer => customer.AccountNumber, fakeSelectionSet);
            Action addingFieldWithAlias = () =>
                                          builder.AddObjectField("foo", customer => customer.AccountNumber, fakeSelectionSet);

            using (new AssertionScope())
            {
                foreach (var addingField in new [] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <InvalidOperationException>()
                    .WithMessage(
                        $"The type '{typeof(string).FullName}' is not a GraphQL object type. Use the {nameof(builder.AddScalarField)} method.")
                    .Where(e => e.HelpLink == "http://spec.graphql.org/June2018/#sec-Objects");
                }
            }
        }
Ejemplo n.º 27
0
        public void If_Expression_Is_An_Enumerable_Property_Then_An_InvalidOperationException_Is_Thrown_Stating_To_Use_AddCollectionField()
        {
            var builder          = SelectionSetBuilder.For <Contact>();
            var fakeSelectionSet = Mock.Of <ISelectionSet <IEnumerable <PhoneNumber> > >();

            Action addingFieldWithoutAlias = () =>
                                             builder.AddObjectField(contact => contact.PhoneNumbers, fakeSelectionSet);
            Action addingFieldWithAlias = () =>
                                          builder.AddObjectField("foo", contact => contact.PhoneNumbers, fakeSelectionSet);

            using (new AssertionScope())
            {
                foreach (var addingField in new [] { addingFieldWithAlias, addingFieldWithoutAlias })
                {
                    Invoking(addingField).Should().ThrowExactly <InvalidOperationException>()
                    .WithMessage(
                        $"The type '{typeof(IEnumerable<PhoneNumber>).FullName}' is a GraphQL list type. Use the {nameof(builder.AddScalarCollectionField)} or {nameof(builder.AddObjectCollectionField)} methods.")
                    .Where(e => e.HelpLink == "http://spec.graphql.org/June2018/#sec-Type-System.List");
                }
            }
        }
Ejemplo n.º 28
0
        public void Then_Specified_Properties_Are_Rendered()
        {
            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 selectionSet = SelectionSetBuilder.For <Customer>()
                               .AddScalarField(customer => customer.Id)
                               .AddScalarField("acctNum", customer => customer.AccountNumber)
                               .AddObjectField("contactInfo", customer => customer.CustomerContact, contactSelectionSet)
                               .AddScalarCollectionField(customer => customer.FavoriteNumbers)
                               .Build();

            IQueryRenderer renderer = new QueryRenderer();

            var result = renderer.Render(selectionSet);

            Snapshot.Match(result);
        }