Example #1
0
        public PersonEntityMapper()
        {
            companyEntityMapper = new CompanyEntityMapper();

            // FIXME: DeduplicatingEntityMapper is broken here, why?
            personEntityMapper = new DeduplicatingEntityMapper <Person>
            {
                Mapper     = this,
                PrimaryKey = p => p.Id,
                ReturnsNullWithDuplicates = false,
            };
        }
Example #2
0
        public PersonQuery(
            IQueryBuilder <Person> personQueryBuilder,
            IServiceProvider serviceProvider)
        {
            Field <ListGraphType <PersonType> >(
                "people",
                description: "A list of people.",
                resolve: context =>
            {
                var alias = "person";
                var query = SqlBuilder.From($"Person {alias}");
                query     = personQueryBuilder.Build(query, context.FieldAst, alias);

                // Create a mapper that understands how to uniquely identify the 'Person' class,
                // and will deduplicate people as they pass through it
                var personMapper = new DeduplicatingEntityMapper <Person>
                {
                    Mapper     = new PersonEntityMapper(),
                    PrimaryKey = person => person.Id,
                };

                using (var connection = serviceProvider.GetRequiredService <IDbConnection>())
                {
                    var results = query.Execute(connection, context.FieldAst, personMapper);
                    return(results);
                }
            }
                );

            FieldAsync <ListGraphType <PersonType> >(
                "peopleAsync",
                description: "A list of people fetched asynchronously.",
                resolve: async context =>
            {
                var alias = "person";
                var query = SqlBuilder.From($"Person {alias}");
                query     = personQueryBuilder.Build(query, context.FieldAst, alias);

                // Create a mapper that understands how to uniquely identify the 'Person' class,
                // and will deduplicate people as they pass through it
                var personMapper = new DeduplicatingEntityMapper <Person>
                {
                    Mapper     = new PersonEntityMapper(),
                    PrimaryKey = p => p.Id,
                };

                using (var connection = serviceProvider.GetRequiredService <IDbConnection>())
                {
                    connection.Open();

                    var results = await query.ExecuteAsync(connection, context.FieldAst, personMapper);
                    return(results);
                }
            }
                );

            Field <PersonType>(
                "person",
                description: "Gets a person by ID.",
                arguments: new QueryArguments(
                    new QueryArgument <IntGraphType> {
                Name = "id", Description = "The ID of the person."
            }
                    ),
                resolve: context =>
            {
                var id    = context.Arguments["id"];
                var alias = "person";
                var query = SqlBuilder
                            .From($"Person {alias}")
                            .Where($"{alias}.Id = @id", new { id });

                query = personQueryBuilder.Build(query, context.FieldAst, alias);

                // Create a mapper that understands how to uniquely identify the 'Person' class,
                // and will deduplicate people as they pass through it
                var personMapper = new DeduplicatingEntityMapper <Person>
                {
                    Mapper     = new PersonEntityMapper(),
                    PrimaryKey = p => p.Id,
                };

                using (var connection = serviceProvider.GetRequiredService <IDbConnection>())
                {
                    var results = query.Execute(connection, context.FieldAst, personMapper);
                    return(results.FirstOrDefault());
                }
            }
                );
        }
Example #3
0
        public void InsertPerson()
        {
            var person = new Person
            {
                FirstName = "Steven",
                LastName  = "Rollman",
            };

            // Ensure inserting a person works and we get the person's Id back
            int personId;

            using (var db = fixture.GetDbConnection())
            {
                personId = SqlBuilder
                           .Insert(person)
                           .ExecuteWithSqlIdentity <int>(db); // supply the identifier's type yourself

                Assert.True(personId > 0);

                int anotherPersonId = SqlBuilder
                                      .Insert(person)
                                      .ExecuteWithSqlIdentity(db, p => p.Id); // use type and func selector to identifier

                Assert.True(anotherPersonId > 1);

                var email = new Email
                {
                    Address = "*****@*****.**",
                };

                var phone = new Phone
                {
                    Number = "8011115555",
                    Type   = PhoneType.Mobile,
                };

                // Add email and phone number to the person
                int insertedCount;
                insertedCount = SqlBuilder
                                .Insert(email)
                                .Insert(phone)
                                .Execute(db);

                // Ensure both were inserted properly
                Assert.Equal(2, insertedCount);

                // Build an entity mapper for person
                var personMapper = new DeduplicatingEntityMapper <Person>
                {
                    Mapper     = new PersonEntityMapper(),
                    PrimaryKey = p => p.Id,
                };

                // Query the person from the database
                var query = SqlBuilder
                            .From <Person>("person")
                            .LeftJoin("Email email on person.Id = email.PersonId")
                            .LeftJoin("Phone phone on person.Id = phone.PersonId")
                            .Select("person.*, email.*, phone.*")
                            .SplitOn <Person>("Id")
                            .SplitOn <Email>("Id")
                            .SplitOn <Phone>("Id")
                            .Where("person.Id = @id", new { id = personId });

                var graphql = @"
{
    person {
        firstName
        lastName
        emails {
            id
            address
        }
        phones {
            id
            number
        }
    }
}";

                var selection = fixture.BuildGraphQLSelection(graphql);

                person = query
                         .Execute(db, selection, personMapper)
                         .FirstOrDefault();
            }

            // Ensure all inserted data is present
            Assert.NotNull(person);
            Assert.Equal(personId, person.Id);
            Assert.Equal("Steven", person.FirstName);
            Assert.Equal("Rollman", person.LastName);
            Assert.Equal(1, person.Emails.Count);
            Assert.Equal("*****@*****.**", person.Emails[0].Address);
            Assert.Equal(1, person.Phones.Count);
            Assert.Equal("8011115555", person.Phones[0].Number);
        }
Example #4
0
        public void EntityMapSucceeds()
        {
            var person1 = new Person
            {
                FirstName = "Doug",
                Id        = 2,
                LastName  = "Day",
            };
            var person2 = new Person
            {
                FirstName = "Douglas",
                Id        = 2,
                LastName  = "Day",
            };

            var email1 = new Email
            {
                Address = "*****@*****.**",
                Id      = 2,
            };

            var email2 = new Email
            {
                Address = "*****@*****.**",
                Id      = 3,
            };

            var phone = new Phone
            {
                Id     = 1,
                Number = "8011234567",
                Type   = PhoneType.Mobile,
            };

            var splitOn = new[]
            {
                typeof(Person),
                typeof(Email),
                typeof(Phone),
            };

            var deduplicatingPersonMapper = new DeduplicatingEntityMapper <Person>
            {
                Mapper     = new PersonEntityMapper(),
                PrimaryKey = p => p.Id,
            };

            var graphql = @"
{
    query {
        firstName
        lastName
        id
        emails {
            id
            address
        }
        phones {
            id
            number
            type
        }
    }
}";

            var selectionSet = fixture.BuildGraphQLSelection(graphql);
            var context1     = new EntityMapContext
            {
                Items = new object[]
                {
                    person1,
                    email1,
                    phone,
                },
                SelectionSet = selectionSet,
                SplitOn      = splitOn,
            };

            person1 = deduplicatingPersonMapper.Map(context1);
            Assert.Equal(3, context1.MappedCount);

            Assert.Equal(2, person1.Id);
            Assert.Equal("Doug", person1.FirstName);
            Assert.Equal(1, person1.Emails.Count);
            Assert.Equal(1, person1.Phones.Count);

            var context2 = new EntityMapContext
            {
                Items = new object[]
                {
                    person2,
                    email2,
                    null,
                },
                SelectionSet = selectionSet,
                SplitOn      = splitOn,
            };

            person2 = deduplicatingPersonMapper.Map(context2);
            Assert.Equal(3, context2.MappedCount);

            // Duplicate is detected
            Assert.Null(person2);

            // 2nd email added to person
            Assert.Equal(2, person1.Id);
            Assert.Equal("Doug", person1.FirstName);
            Assert.Equal(2, person1.Emails.Count);
        }