• This repository has been archived on 18/Jul/2023
  • Stars
    star
    202
  • Rank 193,691 (Top 4 %)
  • Language
    C#
  • License
    Other
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

LINQ to GraphQL - Strongly typed GraphQL queries with LINQ query syntax. No more magic strings and runtime errors.

GraphQLinq

LINQ to GraphQL - Strongly typed GraphQL queries with LINQ query syntax.

You must regenerate your client code with GraphQLinq.Scaffolding after updating GraphQLinq.Client to a newer version

License AppVeyor Coverage Status Ko-Fi

Project Icon

This library is not maintained any more. For a similar LINQ style GraphQL syntax library check out ZeroQL

About The Project

GraphQLinq 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:

  • Write strongly typed queries with LINQ.
  • Have your queries checked by the compiler.
  • Run queries and deserialize JSON response into strongly typed classes in a single method call.
  • View queries generated by LINQ to GraphQL.

Getting Started

Install Scaffolding Tool

Before you starting writing queries, you need to generate classes from GraphQL types. This is done by GraphQLinq.Scaffolding, a .NET tool that is part of this project.

To get the tool, open your favourite command shell and run

dotnet tool install --global --version 1.1.0-beta GraphQLinq.Scaffolding

Running this command will install the GraphQLinq.Scaffolding tool and make it available globally for all projects.

Scaffolding Client Code

Next, 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 o option specifies the output directory for generated classes, and n specifies the namespace of the classes.

Scaffolding

Install GraphQLinq NuGet Package

Before 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.1.0-beta

Running GraphQL Queries with LINQ

The scaffolding tool generates classes for types available in the GraphQL type system and a QueryContext class that serves as an entry point for running the queries. GraphQLinq supports running different kinds of queries.

Query all Primitive Properties of a Type

To query all properties of a type, simply run a query like this:

var spaceXContext = new QueryContext();

var company = await spaceXContext.Company().ToItem();

RenderCompanyDetails(company);

This will query all primitive and string properties of Company, but it won't query nested properties or collection type properties. Here is the output of the code snippet:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 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 Properties

If you want to query specific properties, including a navigation property, you can specify it with the Select method. You either map the projection to an existing type or an anonymous object (Headquarters is a nested property):

var companySummaryAnonymous = await spaceXContext.Company().Select(c => new { c.Ceo, c.Name, c.Headquarters }).ToItem();

//Use data class to select specific properties
var companySummary = await 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 Properties

You can also query navigation properties using the Include method. You can include several properties if you need, and you can also Include nested navigation properties:

var companyWithHeadquartersAndLinks = await spaceXContext.Company()
                                            .Include(info => info.Headquarters)
                                            .Include(info => info.Links).ToItem();

RenderCompanyDetailsAndLinks(companyWithHeadquartersAndLinks);

Pass Parameters to Queries and Compose Queries

If the query has parameters, the generated method will have a parameter for each query parameter.

This code will query for all Missions that included Orbital ATK as a manufacturer. It also builds a new query over the existing one that includes Payloads in the result.

var missionsQuery = spaceXContext.Missions(new MissionsFind { Manufacturer = "Orbital ATK" }, null, null)
                                 .Include(mission => mission.Manufacturers);
var missions = await missionsQuery.ToEnumerable();

RenderMissions(missions);

var missionsWithPayloads = await missionsQuery.Include(mission => mission.Payloads).ToEnumerable();

RenderMissions(missionsWithPayloads, true);

Include Multiple Levels of Navigation Properties

The Include method allows quering for multi-level nested properties too. For example, here is how to query for Launches and include Rocket's second stage payload manufacturer:

//Launch_date_unix and Static_fire_date_unix need custom converter
spaceXContext.JsonSerializerOptions.Converters.Add(new UnixEpochDateTimeConverter());

var launches = await 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))
                            .ToEnumerable();

RenderLaunches(launches);

View Generated Query

You can view the GraphQL query and variables by using the Query and Variables property of the GraphQuery class. The ToString() method of the GraphQuery class returns the query and the variables combined:

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 will be equal to

query ($find: MissionsFind) { result: missions (find: $find) { 
  description
  id
  name
  twitter
  website
  wikipedia
  manufacturers
 }}

and the content of fullQuery will be:

{"query":"query ($find: MissionsFind) { result: missions (find: $find) { 
  description
  id
  name
  twitter
  website
  wikipedia
  manufacturers
 }}","variables":{"find":{"manufacturer":"Orbital ATK"}}}

Roadmap

See the open issues for a list of proposed features and known issues.

Contributing

If 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.

License

Copyright Β© Giorgi Dalakishvili

Distributed under the Apache License. See License for more information.

More Repositories

1

EntityFramework.Exceptions

Strongly typed exceptions for Entity Framework Core. Supports SQLServer, PostgreSQL, SQLite, Oracle and MySql.
C#
1,446
star
2

EFCore.Visualizer

Entity Framework Core queries debugger visualizer.
C#
419
star
3

LINQPad.QueryPlanVisualizer

SQL Server and PostgreSQL query execution plan visualizer for LINQPad
C#
399
star
4

DuckDB.NET

Bindings and ADO.NET Provider for DuckDB
C#
392
star
5

Math-Expression-Evaluator

A C# library for parsing mathemitical expressions with support for parentheses and variables.
C#
114
star
6

BetterOpenWith

A better way to open files
Java
42
star
7

Dynamic-PInvoke

C#
15
star
8

Entity-Framework-Analyzers

Code Analyzers and Fixers for Common Entity Framework Issues.
C#
9
star
9

Maui-DotNetConf-Sample

Maui Spatial Data Sample App
C#
7
star
10

Dynamic-ViewState-in-ASP.Net-WebForms

A sample project showing how to access ViewState data dynamically by using DynamicObject class
ASP.NET
6
star
11

VpnProxyChanger

Automatically launches rdp to work PC and sets proxy
C#
4
star
12

EF-Core-Demos

EF Core Examples
C#
4
star
13

U2G-Shipping-Calculator

Google Chrome extension for calculating USA2Georgia shipping cost on amazon.com
JavaScript
4
star
14

Silverlight-Remote-Control

Silverlight 4 application with remote control support.
C#
3
star
15

Giorgi

3
star
16

TbilisiFloodDonations

Visualization of Tbilisi flood donations
HTML
3
star
17

Movie-Explorer

C#
2
star
18

userscripts

Collection of userscripts for different websites
2
star
19

SemanticKernel.Connectors.Oracle

Semantic Kernel memory built on top of Oracle 23ai
C#
2
star
20

Okta.CaloriesTracker

JavaScript
1
star
21

SE-Hot-Network-Questions-Filter

Greasemonkey & Tampermonkey script for filterting hot network questions on stackexchange sites
JavaScript
1
star
22

PostgresRangeTypes

Demo showing how to use PostgreSQL ranges from Entity Framework Core
C#
1
star
23

github-stats

Python
1
star
24

DuckDB.NET-Docs

DuckDB.NET project documentation.
C#
1
star
25

Semantic-Search-Demo

Demo app for pgvector semantic search
C#
1
star