This is a convention based .NET standard library for building GraphQL endpoints. It is delivered with a middleware for ASP.NET Core and supports ASP.NET Core authorization.
This library is a work in progress and is not yet production ready. Key missing features are:
- Validation support
- Directive support
- Union support
- A few introspection fields are still missing (GraphiQL works though)
- Support for documentation attributes (description, deprecated, ...)
- Support for execution context data in field resolvers together with resolver injection.
With ASP.NET Core integration:
Install-Package Cooke.GraphQL.AspNetCore
Without ASP.NET Core integration:
Install-Package Cooke.GraphQL
See the integration tests for more in-depth examples on how to use the library with and without ASP.NET Core.
Define a query class:
publicclassQuery{// Dependency injection in query constructorpublicQuery(MyDbContextcontext){this.context=context;}// Properties and methods corresponds to fields in GraphQLpublicTask<IEnumerable<MyApiModel>>Models=>context.Models.Select(x =>newMyApiModel(x)).ToArrayAsync();// Support for the Authorization attribute[Authorize]publicMyApiModelModel(intid)=>{var model =awaitcontext.Models.FindAsync(id);if(model!=null){returnnewMyApiModel(model);}returnnull;}}Enable a GraphQL endpoint and add required services in StartUp:
publicclassStartup{publicvirtualvoidConfigureServices(IServiceCollectionservices){// Required for the Query class to be resolvedservices.AddTransient<Query>();// Adds services required by GraphQLservices.AddGraphQL();}publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv,ILoggerFactoryloggerFactory){// Adds a graphql endpoint at /graphqlapp.UseGraphQL<Query>();}}The creation of this library is motivated in the context of a comparison with the https://github.com/graphql-dotnet/graphql-dotnet library. The key driving bullets are as follows.
A great GraphQL Library should:
- Use a convention based approach as the primary technique to define api:s
- Should be ready to go with ASP.NET Core (delivered with middleware)
- Should be delivered with an available/suggested authorization technique
- The library interface should be intuitive/easy to understand
This library depends on the GraphQL query parser written by Marek Magdziak and released with a MIT license. Thank You!