Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gql #25

Merged
merged 4 commits into from
Jun 13, 2021
Merged

Gql #25

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions Micro.Starter.Api/Controllers/WeatherForecast.cs

This file was deleted.

40 changes: 0 additions & 40 deletions Micro.Starter.Api/Controllers/WeatherForecastController.cs

This file was deleted.

50 changes: 50 additions & 0 deletions Micro.Starter.Api/GraphQL/Directives/AuthorizeDirective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using GraphQL;
using GraphQL.Resolvers;
using GraphQL.Types;
using GraphQL.Utilities;
using Micro.Starter.Api.GraphQL.Directives.Exceptions;
using Microsoft.AspNetCore.Http;

namespace Micro.Starter.Api.GraphQL.Directives
{
public class AuthorizeDirective : DirectiveGraphType
{
public const string DirectiveName = "authorize";
public override bool? Introspectable => true;

public AuthorizeDirective() : base(
DirectiveName,
DirectiveLocation.Field,
DirectiveLocation.Mutation,
DirectiveLocation.Query,
DirectiveLocation.FieldDefinition)
{
}
}
public class AuthorizeDirectiveVisitor : BaseSchemaNodeVisitor
{
private readonly IHttpContextAccessor _contextAccessor;

public AuthorizeDirectiveVisitor(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}

public override void VisitObjectFieldDefinition(FieldType field, IObjectGraphType type, ISchema schema)
{
var applied = field.FindAppliedDirective(AuthorizeDirective.DirectiveName);
if (applied == null)
{
return;
}

var isAuthenticated = _contextAccessor.HttpContext?.User.Identity?.IsAuthenticated;
if (isAuthenticated == true)
{
return;
}

field.Resolver = new AsyncFieldResolver<object>(async context => { throw new NotAuthorizedException(); });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Micro.Starter.Api.GraphQL.Directives.Exceptions
{
public class NotAuthorizedException : Exception
{
public NotAuthorizedException() : base("This operation requires logging in")
{
}
}
}
21 changes: 21 additions & 0 deletions Micro.Starter.Api/GraphQL/Extensions/Directives.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GraphQL;
using GraphQL.Builders;
using GraphQL.Types;
using Micro.Starter.Api.GraphQL.Directives;

namespace Micro.Starter.Api.GraphQL.Extensions
{
public static class Directives
{
public static FieldType Authorize(this FieldType type)
{
return type.ApplyDirective(AuthorizeDirective.DirectiveName);
}

public static FieldBuilder<TSourceType, TReturnType> Authorize<TSourceType, TReturnType>(
this FieldBuilder<TSourceType, TReturnType> type)
{
return type.Directive(AuthorizeDirective.DirectiveName);
}
}
}
56 changes: 56 additions & 0 deletions Micro.Starter.Api/GraphQL/Extensions/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using GraphQL;
using GraphQL.DataLoader;
using GraphQL.Execution;
using GraphQL.Server;
using GraphQL.Server.Ui.GraphiQL;
using GraphQL.Server.Ui.Playground;
using GraphQL.SystemTextJson;
using GraphQL.Types;
using Micro.GraphQL.Federation;
using Micro.Starter.Api.GraphQL.Directives;
using Micro.Starter.Api.GraphQL.Types;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Micro.Starter.Api.GraphQL.Extensions
{
public static class Startup
{
public static void ConfigureGraphql(this IServiceCollection services)
{
services.EnableFederation<EntityType>();
services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddSingleton<IDocumentWriter, DocumentWriter>();
services.AddSingleton<IDataLoaderContextAccessor, DataLoaderContextAccessor>();
services.AddSingleton<IDocumentExecutionListener, DataLoaderDocumentListener>();
services.AddTransient<ISchema, StarterSchema>();
services.AddTransient<Query>();
services.AddTransient<WeatherType>();
services.AddScoped<AuthorizeDirectiveVisitor>();
services
.AddGraphQL(options =>
{
options.UnhandledExceptionDelegate = ctx =>
{
ctx.ErrorMessage = ctx.OriginalException.Message;
};
})
.AddDataLoader()
.AddSystemTextJson()
.AddErrorInfoProvider(opts => opts.ExposeExceptionStackTrace = true);
}
public static void SetupGraphQl(this IApplicationBuilder app)
{
app.UseGraphQL<ISchema>();
app.UseGraphQLGraphiQL(new GraphiQLOptions
{
SubscriptionsEndPoint = null,
GraphQLEndPoint = "/graphql"
}, "/ui/graphql");
app.UseGraphQLPlayground(new PlaygroundOptions
{
GraphQLEndPoint = "/graphql",
}, "/ui/playground");
}
}
}
21 changes: 21 additions & 0 deletions Micro.Starter.Api/GraphQL/Query.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using Micro.GraphQL.Federation;
using Micro.Starter.Api.GraphQL.Types;
using Micro.Starter.Storage;

namespace Micro.Starter.Api.GraphQL
{
public sealed class Query : Query<EntityType>
{
public Query()
{
Field<WeatherType, Weather>()
.Name("weather")
.ResolveAsync(x => Task.FromResult(new Weather
{
Id = "id",
Temperature = 23.3
}));
}
}
}
17 changes: 17 additions & 0 deletions Micro.Starter.Api/GraphQL/StarterSchema.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Micro.GraphQL.Federation;
using Micro.Starter.Api.GraphQL.Directives;
using Micro.Starter.Api.GraphQL.Types;

namespace Micro.Starter.Api.GraphQL
{
public class StarterSchema : Schema<EntityType>
{
public StarterSchema(IServiceProvider services, Query query) : base(services)
{
Query = query;
Directives.Register(new AuthorizeDirective());
RegisterVisitor(typeof(AuthorizeDirectiveVisitor));
}
}
}
10 changes: 10 additions & 0 deletions Micro.Starter.Api/GraphQL/Types/EntityType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Micro.Starter.Api.GraphQL.Types
{
public class EntityType : Micro.GraphQL.Federation.Types.EntityType
{
public EntityType()
{
Type<WeatherType>();
}
}
}
21 changes: 21 additions & 0 deletions Micro.Starter.Api/GraphQL/Types/WeatherType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using Micro.GraphQL.Federation;
using Micro.Starter.Storage;

namespace Micro.Starter.Api.GraphQL.Types
{
public sealed class WeatherType : ObjectGraphType<Weather>
{
public WeatherType()
{
Name = "Weather";
Field("id", x => x.Id);
Field("temperature", x => x.Temperature);
ResolveReferenceAsync(ctx => Task.FromResult(new Weather
{
Id = "id",
Temperature = 32.2
}));
}
}
}
12 changes: 12 additions & 0 deletions Micro.Starter.Api/Internal/Configs/Services.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Micro.Starter.Api.Internal.Configs
{
public class Services
{
public KeyStoreConfig KeyStore { set; get; }
}

public class KeyStoreConfig
{
public string Url { set; get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging;

namespace Micro.Starter.Api.Configs
namespace Micro.Starter.Api.Internal.Configs
{
public class SlackLoggingConfig
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Threading;
using System.Threading.Tasks;
using Micro.Starter.Api.Models;
using Micro.Starter.Storage;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Micro.Starter.Api.HealthCheck
namespace Micro.Starter.Api.Internal.HealthCheck
{
public class ConnectionToDbCheck : IHealthCheck
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Micro.Starter.Api.HealthCheck
namespace Micro.Starter.Api.Internal.HealthCheck
{
public class MemoryCheck : IHealthCheck
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Micro.Starter.Api.Configs;
using Micro.Starter.Api.Internal.Configs;
using Micro.Starter.Storage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Micro.Starter.Api.StartupExtensions
namespace Micro.Starter.Api.Internal.StartupExtensions
{
public static class Configuration
{
public static void AddConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<DatabaseConfig>(configuration.GetSection("DatabaseConfig"));
services.Configure<Services>(configuration.GetSection("Services"));
services.Configure<SlackLoggingConfig>(configuration.GetSection("Logging").GetSection("Slack"));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Threading.Tasks;
using Micro.Starter.Api.Models;
using Micro.Starter.Storage;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;

namespace Micro.Starter.Api.StartupExtensions
namespace Micro.Starter.Api.Internal.StartupExtensions
{
public static class Database
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Micro.Starter.Api.Models;
using Micro.Starter.Api.Repository;
using Micro.Starter.Api.Uuid;
using Micro.Starter.Common.Uuid;
using Micro.Starter.Storage;
using Microsoft.Extensions.DependencyInjection;

namespace Micro.Starter.Api.StartupExtensions
namespace Micro.Starter.Api.Internal.StartupExtensions
{
public static class DependencyInjection
{
public static void ConfigureRequiredDependencies(this IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddDbContext<ApplicationContext>();
services.AddScoped<IWeatherRepository, WeatherRepository>();
services.AddSingleton<IUuidService, UuidService>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using Micro.Starter.Api.HealthCheck;
using Micro.Starter.Api.Internal.HealthCheck;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
Expand All @@ -10,7 +10,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Micro.Starter.Api.StartupExtensions
namespace Micro.Starter.Api.Internal.StartupExtensions
{
public static class HealthChecks
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Micro.Starter.Api.Configs;
using Micro.Starter.Api.Internal.Configs;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Slack;

namespace Micro.Starter.Api.StartupExtensions
namespace Micro.Starter.Api.Internal.StartupExtensions
{
public static class Logger
{
Expand Down
Loading