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 | 144 | 1/28/2025 |
1.18.1 | 142 | 11/14/2024 |
1.18.0 | 173 | 2/14/2024 |
1.17.0 | 269 | 11/15/2023 |
1.16.1 | 188 | 10/9/2023 |
1.16.0 | 202 | 9/4/2023 |
1.15.2 | 204 | 8/23/2023 |
1.15.1 | 202 | 8/22/2023 |
1.15.0 | 198 | 8/22/2023 |
1.14.22 | 208 | 7/14/2023 |
1.14.21 | 195 | 7/14/2023 |
1.14.20 | 206 | 7/13/2023 |
1.14.19 | 203 | 7/13/2023 |
1.14.18 | 202 | 7/12/2023 |
1.14.17 | 208 | 7/12/2023 |
1.14.16 | 194 | 7/11/2023 |
1.14.15 | 196 | 7/10/2023 |
1.4.14 | 198 | 7/4/2023 |
1.4.13 | 184 | 6/28/2023 |
1.4.10 | 193 | 6/28/2023 |
1.4.9 | 178 | 6/19/2023 |
1.4.8 | 179 | 6/16/2023 |
1.4.7 | 198 | 6/16/2023 |
1.4.6 | 181 | 6/15/2023 |
1.4.5 | 197 | 6/15/2023 |
1.4.4 | 192 | 6/14/2023 |
1.4.3 | 184 | 6/14/2023 |
1.4.2 | 182 | 6/13/2023 |
1.4.1 | 205 | 6/11/2023 |
1.4.0 | 196 | 6/10/2023 |
1.3.2 | 192 | 6/8/2023 |
1.3.1 | 199 | 6/8/2023 |
1.3.0 | 190 | 6/7/2023 |
1.2.4 | 194 | 5/23/2023 |
1.2.3 | 187 | 5/23/2023 |
1.2.2 | 189 | 5/12/2023 |
1.2.1 | 187 | 5/12/2023 |
1.2.0 | 190 | 5/12/2023 |
1.1.0 | 235 | 4/18/2023 |
1.0.0 | 267 | 3/29/2023 |
1.0.0-beta3 | 181 | 2/28/2023 |
1.0.0-beta2 | 179 | 2/17/2023 |
1.0.0-beta1 | 183 | 2/16/2023 |