在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):landmarkhw/Dapper.GraphQL开源软件地址(OpenSource Url):https://github.com/landmarkhw/Dapper.GraphQL开源编程语言(OpenSource Language):C# 100.0%开源软件介绍(OpenSource Introduction):Dapper.GraphQLA library designed to integrate the Dapper and graphql-dotnet projects with ease-of-use in mind and performance as the primary concern. DesignDapper.GraphQL combines the ideas that come out-of-the-box with graphql-dotnet, and adds the following concepts:
Query BuildersQuery builders are used to build dynamic queries based on what the client asked for in their GraphQL query. For example, given this GraphQL query: query {
people {
id
firstName
lastName
}
} A proper query builder will generate a SQL query that looks something like this: SELECT id, firstName, lastName
FROM Person Using the same query builder, and given the following GraphQL: query {
people {
id
firstName
lastName
emails {
id
address
}
phones {
id
number
type
}
}
} A more complex query should be generated, something like: SELECT
person.Id, person.firstName, person.lastName,
email.id, email.address,
phone.id, phone.number, phone.type
FROM Person person
LEFT OUTER JOIN Email email ON person.Id = email.PersonId
LEFT OUTER JOIN Phone phone ON person.Id = phone.PersonId Entity MappersEntity mappers are used to map entities to Dapper from query results. Since a single entity can be composed of multiple rows of a query result, an entity mapper is designed to quickly merge multiple rows of output SQL into a single hierarchy of entities. See the UsageSetupDapper.GraphQL uses Microsoft's standard DI container, IServiceCollection, to manage all of the Dapper and GraphQL interactions. If you're developing in ASP.NET Core, you can add this to the ConfigureServices() method: serviceCollection.AddDapperGraphQL(options =>
{
// Add GraphQL types
options.AddType<CompanyType>();
options.AddType<EmailType>();
options.AddType<PersonType>();
options.AddType<GraphQL.PhoneType>();
options.AddType<PersonQuery>();
// Add the GraphQL schema
options.AddSchema<PersonSchema>();
// Add query builders for dapper
options.AddQueryBuilder<Company, CompanyQueryBuilder>();
options.AddQueryBuilder<Email, EmailQueryBuilder>();
options.AddQueryBuilder<Person, PersonQueryBuilder>();
options.AddQueryBuilder<Phone, PhoneQueryBuilder>();
}); QueriesWhen creating a SQL query based on a GraphQL query, you need 2 things to build the query properly: A query builder and entity mapper. Query builderEach entity in a system should have its own query builder, so any GraphQL queries that interact with those entities can be automatically handled, even when nested within other entities. In the above setup, the public class EmailQueryBuilder :
IQueryBuilder<Email>
{
public SqlQueryContext Build(SqlQueryContext query, IHaveSelectionSet context, string alias)
{
// Always get the ID of the email
query.Select($"{alias}.Id");
// Tell Dapper where the Email class begins (at the Id we just selected)
query.SplitOn<Email>("Id");
var fields = context.GetSelectedFields();
if (fields.ContainsKey("address"))
{
query.Select($"{alias}.Address");
}
return query;
}
} Arguments
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论