Fireflies.GraphQL.AspNet
1.18.2
dotnet add package Fireflies.GraphQL.AspNet --version 1.18.2
NuGet\Install-Package Fireflies.GraphQL.AspNet -Version 1.18.2
<PackageReference Include="Fireflies.GraphQL.AspNet" Version="1.18.2" />
<PackageVersion Include="Fireflies.GraphQL.AspNet" Version="1.18.2" />
<PackageReference Include="Fireflies.GraphQL.AspNet" />
paket add Fireflies.GraphQL.AspNet --version 1.18.2
#r "nuget: Fireflies.GraphQL.AspNet, 1.18.2"
#:package Fireflies.GraphQL.AspNet@1.18.2
#addin nuget:?package=Fireflies.GraphQL.AspNet&version=1.18.2
#tool nuget:?package=Fireflies.GraphQL.AspNet&version=1.18.2
Fireflies GraphQL ASP.NET middleware
Example
Add the following code to your WebApplication pipeline.
app.UseWebSockets();
var graphQLOptions = new GraphQLOptionsBuilder();
graphQLOptions.Add<BookOperations>();
app.UseGraphQL(await graphQLOptions.Build());
Operation definition
public class BookOperations {
[GraphQLPagination]
[GraphQLQuery]
public async Task<IEnumerable<InventoryBook>> Books(BookFilterInput? filter) {
return new InventoryBook[] {
new() { BookId = 1, Title = "My first book", ISBN = "1234", ExactInventory = 20 },
new() { BookId = 2, Title = "My second book", ISBN = "5678", ExactInventory = 29 }
}.Where(x => filter == null || string.IsNullOrWhiteSpace(filter.Title) || x.Title == filter.Title);
}
[GraphQLMutation]
public async Task<InventoryBook> AddBook(AddBookInput data) {
return new InventoryBook {
BookId = DateTime.UtcNow.Second,
Title = data.Title
};
}
[GraphQLSubscription]
public async IAsyncEnumerable<InventoryBook> BookUpdated(int bookId, CancellationToken cancellationToken) {
while(!cancellationToken.IsCancellationRequested) {
await Task.Delay(2000);
yield return new() { BookId = 1, Title = "My first book was updated", ISBN = "1234", ExactInventory = 20 };
}
}
}
public class BookFilterInput {
public string? Title { get; set; }
public StringFilterOperatorInput? ISBN { get; set; }
}
public class AddBookInput {
public string Title { get; set; }
}
Return types
[GraphQLUnion]
public interface IBook {
public int BookId { get; set; }
string Title { get; set; }
string ISBN { get; set; }
}
public class InventoryBook : IBook {
[GraphQLId]
public int BookId { get; set; }
public string Title { get; set; }
public string ISBN { get; set; }
public async Task<decimal> CalculatePrice() {
return (decimal)23.3;
}
[MustBeSalesman]
[GraphQLDescription("Returns the exact inventory")]
[GraphQLDeprecated("Will be 0 from 2024-01-01")]
public int ExactInventory { get; set; }
}
Input definitions
public class BookFilterInput : GraphQLInput {
public string? Title { get; set; }
public StringFilterOperatorInput? ISBN { get; set; }
}
public class AddBookInput : GraphQLInput {
public string Title { get; set; }
}
public class StringFilterOperatorInput : GraphQLInput {
public string? Eq { get; set; }
}
Authorization
Please note that the default constructor is marked as internal. This will make the IoC system to select the version which has a User as an argument.
The User object can be registered using a implementation of the IRequestDependencyResolverBuilder interface.
public class MustBeSalesmanAttribute : GraphQLAuthorizationAttribute {
internal MustBeSalesmanAttribute() {
}
public MustBeSalesmanAttribute(User user) {
}
public override Task<bool> Authorize() {
return Task.FromResult(false);
}
public override string Help => "Must be authenticated as a salesman";
}
Request container
To setup the container per request you need to define a class that implements the IRequestDependencyResolverBuilder interface.
public class RequestDependencyResolverBuilder : IRequestDependencyResolverBuilder {
public void Build(ILifetimeScopeBuilder builder, HttpContext context) {
builder.RegisterInstance(new User());
}
}
This resolver needs to be registered within your root container, example below is using Fireflies.IoC.Autofac.
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<MustBeSalesmanAttribute>();
containerBuilder.RegisterType<RequestDependencyResolverBuilder>().As<IRequestDependencyResolverBuilder>();
var container = containerBuilder.Build();
...
graphQLOptions.SetDependencyResolver(new AutofacDependencyResolver(container));
Federation
To add a federated backend, you only need to register it during startup.
graphQLOptions.AddFederation("Author", "https://localhost:7274/graphql");
Logo by freepik
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Fireflies.GraphQL.Core (>= 1.18.2)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.18.2 | 220 | 1/28/2025 |
| 1.18.1 | 195 | 11/14/2024 |
| 1.18.0 | 222 | 2/14/2024 |
| 1.17.0 | 306 | 11/15/2023 |
| 1.16.1 | 217 | 10/9/2023 |
| 1.16.0 | 238 | 9/4/2023 |
| 1.15.2 | 243 | 8/23/2023 |
| 1.15.1 | 242 | 8/22/2023 |
| 1.15.0 | 236 | 8/22/2023 |
| 1.14.22 | 242 | 7/14/2023 |
| 1.14.21 | 239 | 7/14/2023 |
| 1.14.20 | 248 | 7/13/2023 |
| 1.14.19 | 236 | 7/13/2023 |
| 1.14.18 | 236 | 7/12/2023 |
| 1.14.17 | 246 | 7/12/2023 |
| 1.14.16 | 249 | 7/11/2023 |
| 1.14.15 | 241 | 7/10/2023 |
| 1.4.14 | 230 | 7/4/2023 |
| 1.4.13 | 220 | 6/28/2023 |
| 1.4.10 | 240 | 6/28/2023 |