在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Giorgi/GraphQLinq开源软件地址(OpenSource Url):https://github.com/Giorgi/GraphQLinq开源编程语言(OpenSource Language):C# 96.7%开源软件介绍(OpenSource Introduction):GraphQLinqLINQ to GraphQL - Strongly typed GraphQL queries with LINQ query syntax. About The ProjectGraphQLinq is a .NET tool for generating C# classes from a GraphQL endpoint and a .Net Standard library for writing strongly typed GraphQL queries with LINQ. With GraphQLinq you will:
Getting StartedInstall Scaffolding ToolBefore you starting writing queries, you need to generate classes from GraphQL types. This is done by To get the tool, open your favourite command shell and run dotnet tool install --global --version 1.0.0-beta GraphQLinq.Scaffolding Running this command will install the Scaffolding Client CodeNext, navigate to the project where you want to add the classes and scaffold the client code. In this example, I will use the SpaceX GraphQL Api so run the following command: graphqlinq-scaffold https://api.spacex.land/graphql -o SpaceX -n SpaceX The Install GraphQLinq NuGet PackageBefore writing the queries, you need to install the LINQ to GraphQL client library from NuGet. Run the following command to install it in the current project: dotnet add package GraphQLinq.Client --version 1.0.0-beta Running GraphQL Queries with LINQThe scaffolding tool generates classes for types available in the GraphQL type system and a Query all Primitive Properties of a TypeTo query all properties of a type, simply run a query like this: var spaceXContext = new QueryContext();
var company = spaceXContext.Company().ToItem();
RenderCompanyDetails(company); This will query all primitive and string properties of ┌───────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Property │ Value │
├───────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Name │ SpaceX │
│ Ceo │ Elon Musk │
│ Summary │ SpaceX designs, manufactures and launches advanced rockets and spacecraft. The company was founded in │
│ │ 2002 to revolutionize space technology, with the ultimate goal of enabling people to live on other │
│ │ planets. │
│ Founded │ 2002 │
│ Founder │ Elon Musk │
│ Employees │ 7000 │
└───────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────┘ Query Specific PropertiesIf you want to query specific properties, including a navigation property, you can specify it with the var companySummaryAnonymous = spaceXContext.Company().Select(c => new { c.Ceo, c.Name, c.Headquarters }).ToItem();
//Use data class to select specific properties
var companySummary = spaceXContext.Company().Select(c => new CompanySummary
{
Ceo = c.Ceo,
Name = c.Name,
Headquarters = c.Headquarters
}).ToItem();
RenderCompanySummary(companySummary); This will result in the following output: ┌──────────────┬─────────────────┐
│ Property │ Value │
├──────────────┼─────────────────┤
│ Name │ SpaceX │
│ Ceo │ Elon Musk │
│ Headquarters │ ┌─────────────┐ │
│ │ │ California │ │
│ │ │ Hawthorne │ │
│ │ │ Rocket Road │ │
│ │ └─────────────┘ │
└──────────────┴─────────────────┘ Include Navigation PropertiesYou can also query navigation properties using the var companyWithHeadquartersAndLinks = spaceXContext.Company()
.Include(info => info.Headquarters)
.Include(info => info.Links).ToItem();
RenderCompanyDetailsAndLinks(companyWithHeadquartersAndLinks); Pass Parameters to Queries and Compose QueriesIf the query has parameters, the generated method will have a parameter for each query parameter. This code will query for all var missionsQuery = spaceXContext.Missions(new MissionsFind { Manufacturer = "Orbital ATK" }, null, null)
.Include(mission => mission.Manufacturers);
var missions = missionsQuery.ToList();
RenderMissions(missions);
var missionsWithPayloads = missionsQuery.Include(mission => mission.Payloads).ToList();
RenderMissions(missionsWithPayloads, true); Include Multiple Levels of Navigation PropertiesThe //Launch_date_unix and Static_fire_date_unix need custom converter
spaceXContext.ContractResolver = new SpaceXContractResolver();
var launches = spaceXContext.Launches(null, 10, 0, null, null)
.Include(launch => launch.Links)
.Include(launch => launch.Rocket)
.Include(launch => launch.Rocket.Second_stage.Payloads.Select(payload => payload.Manufacturer))
.ToList();
RenderLaunches(launches); View Generated QueryYou can view the GraphQL query and variables by using the var missionsQuery = spaceXContext.Missions(new MissionsFind { Manufacturer = "Orbital ATK" }, null, null)
.Include(mission => mission.Manufacturers);
var query = missionsQuery.Query;
var fullQuery = missionsQuery.ToString(); If you run the above code query ($find: MissionsFind) { result: missions (find: $find) {
description
id
name
twitter
website
wikipedia
manufacturers
}} and the content of {"query":"query ($find: MissionsFind) { result: missions (find: $find) {
description
id
name
twitter
website
wikipedia
manufacturers
}}","variables":{"find":{"manufacturer":"Orbital ATK"}}} RoadmapSee the open issues for a list of proposed features and known issues. ContributingIf you encounter a bug or have a feature request, please use the Issue Tracker. The project is also open to contributions, so feel free to fork the project and open pull requests. LicenseCopyright © Giorgi Dalakishvili Distributed under the Apache License. See License for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论