DEV Community

Cover image for Updating My Previous GraphQL + .NET to GraphQL.NET version 7
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on

Updating My Previous GraphQL + .NET to GraphQL.NET version 7

GraphQL.NET version 7 was released around August 23, 2022. Now, I want to upgrade it. It has some breaking changes. You can see the breaking changes here too.

Here is my changes for GraphQLNETExample.csproj :

+<PackageReference Include="GraphQL" Version="7.0.2" />
+<PackageReference Include="GraphQL.MicrosoftDI" Version="7.0.2" />
+<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="7.1.0" />
+<PackageReference Include="GraphQL.Server.Ui.Altair" Version="7.1.0" />
+<PackageReference Include="GraphQL.SystemTextJson" Version="7.0.2" />
Enter fullscreen mode Exit fullscreen mode

Final Result:

    <PackageReference Include="GraphQL" Version="7.0.2" />
    <PackageReference Include="GraphQL.MicrosoftDI" Version="7.0.2" />
    <PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="7.1.0" />
    <PackageReference Include="GraphQL.Server.Ui.Altair" Version="7.1.0" />
    <PackageReference Include="GraphQL.SystemTextJson" Version="7.0.2" />
Enter fullscreen mode Exit fullscreen mode

Pull Request Diff

I have partial changes to the codes because I've updated the dependencies without updating my code, which became a broken build.

Code Changes

I only need to update the Program.cs. Here the code:

using GraphQL;
using GraphQL.MicrosoftDI;
using GraphQL.Types;
using GraphQLNetExample.EntityFramework;
using GraphQLNetExample.Notes;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<NotesContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("Default"));
});
builder.Services.AddSingleton<ISchema, NotesSchema>(services => new NotesSchema(new SelfActivatingServiceProvider(services)));
builder.Services.AddGraphQL(options =>
                    options.ConfigureExecution((opt, next) =>
                    {
                        opt.EnableMetrics = true;
                        return next(opt);
                    }).AddSystemTextJson()
                );

builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(
        builder =>
        {
            builder.WithOrigins("*")
                   .AllowAnyHeader();
        });
});

builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "GraphQLNetExample", Version = "v1" });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "GraphQLNetExample v1"));
    app.UseGraphQLAltair();
}

app.UseHttpsRedirection();

app.UseCors();

app.UseAuthorization();

app.MapControllers();

app.UseGraphQL();

app.Run();
Enter fullscreen mode Exit fullscreen mode

Good Luck!

If you are still confused, feel free to comment here. Thanks for reading!

wink gif

Top comments (1)

Collapse
 
xvonrad profile image
makio-one

Thanks for the articles... Needed to update to v. 7 and you made it simple