/graph-ql-poco

Experimental Dapper based SQL resolver for GraphQL.NET to allow building a GraphQL schema from a POCO model

Primary LanguageC#MIT LicenseMIT

graph-ql-poco

An experiment with GraphQL.Net and a custom Dapper based SQL resolver which defines the GraphQL schema and queries a DB based on introspection of the model.

Given the model:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Order> Orders { get; set; } = new List<Order>();
}

public class Order
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Customer Customer { get; set; }
}

And the setup:

services.SetupGraphQLSchema(
    schema => schema
        .DefaultResolver(
            new SQLResolver(
                () => new SqlConnection(
                    @"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=GraphQLTest;Integrated Security=SSPI"
                )
            )
        )
        .Add<Customer>(x => x
            .EntityConfig(e => e.Table("Customers").Key(f => f.Id))
        )
        .Add<Order>(x => x
            .EntityConfig(e => e.Table("Orders").Key(f => f.Id))
        )
);

Allows the following GraphQL query:

customers {
  id
  name
  orders {
    id
    name
  }
}

or alternatively:

orders {
  id
  name
  customer {
    id
    name
  }
}