diff --git a/src/GraphQL/GraphTypes/UserType.cs b/src/GraphQL/GraphTypes/UserType.cs new file mode 100644 index 0000000..89e81e6 --- /dev/null +++ b/src/GraphQL/GraphTypes/UserType.cs @@ -0,0 +1,15 @@ +using GraphQL.Types; +using NetCoreBootstrap.Models.Database; + +namespace NetCoreBootstrap.GraphQL.GraphQLTypes +{ + public class UserType : ObjectGraphType + { + public UserType() + { + Field(user => user.Id); + Field(user => user.Email); + Field(user => user.UserName); + } + } +} diff --git a/src/GraphQL/Queries/UserQuery.cs b/src/GraphQL/Queries/UserQuery.cs new file mode 100644 index 0000000..3de1579 --- /dev/null +++ b/src/GraphQL/Queries/UserQuery.cs @@ -0,0 +1,22 @@ +using GraphQL.Types; +using NetCoreBootstrap.Repositories.Interfaces; +using NetCoreBootstrap.GraphQL.GraphQLTypes; + +namespace NetCoreBootstrap.GraphQL.Queries +{ + public class UserQuery : ObjectGraphType + { + private readonly IUnitOfWork _unitOfWork; + public UserQuery(IUnitOfWork unitOfWork) + { + this._unitOfWork = unitOfWork; + Field>("users", resolve: context => _unitOfWork.UserRepository.GetAllUsers()); + Field("user", + arguments: new QueryArguments(new QueryArgument { Name = "id" }), + resolve: + context => _unitOfWork.UserRepository + .GetUserById(context.GetArgument("id")) + .Result); + } + } +} diff --git a/src/GraphQL/Schemas/UserSchema.cs b/src/GraphQL/Schemas/UserSchema.cs new file mode 100644 index 0000000..56e9173 --- /dev/null +++ b/src/GraphQL/Schemas/UserSchema.cs @@ -0,0 +1,12 @@ +using GraphQL; +using GraphQL.Types; +using NetCoreBootstrap.GraphQL.Queries; + +namespace NetCoreBootstrap.GraphQL.Schemas +{ + public class UserSchema : Schema + { + public UserSchema(IDependencyResolver resolver) : base(resolver) + => this.Query = resolver.Resolve(); + } +} diff --git a/src/NetCoreBootstrap.csproj b/src/NetCoreBootstrap.csproj index 0635a2e..2b51413 100755 --- a/src/NetCoreBootstrap.csproj +++ b/src/NetCoreBootstrap.csproj @@ -4,17 +4,20 @@ ./ca.ruleset - - - - - - - - - - - - + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Scripts/appsettings.Development.json b/src/Scripts/appsettings.Development.json deleted file mode 100644 index 5b52094..0000000 --- a/src/Scripts/appsettings.Development.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - }, - "ConnectionString": "User ID=your_postgres_user;Password=your_postgres_password;Host=localhost;Port=5432;Database=your_database_name;Pooling=true;" -} diff --git a/src/Startup.cs b/src/Startup.cs index ae3e186..13040ac 100755 --- a/src/Startup.cs +++ b/src/Startup.cs @@ -1,8 +1,8 @@ -using System; -using System.Globalization; -using System.IdentityModel.Tokens.Jwt; -using System.Text; -using Microsoft.AspNetCore.Authentication.JwtBearer; +using System.Globalization; +using GraphQL; +using GraphQL.Server; +using GraphQL.Server.Ui.Playground; +using GraphQL.Types; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -13,10 +13,10 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Microsoft.IdentityModel.Tokens; -using NetCoreBootstrap.Mail; +using NetCoreBootstrap.GraphQL.GraphQLTypes; +using NetCoreBootstrap.GraphQL.Queries; +using NetCoreBootstrap.GraphQL.Schemas; using NetCoreBootstrap.Models.Database; -using NetCoreBootstrap.Repositories; using NetCoreBootstrap.Repositories.Database; using NetCoreBootstrap.Repositories.Interfaces; // using Rollbar; @@ -39,6 +39,8 @@ public Startup(IConfiguration configuration, IHostingEnvironment currentEnvironm // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddScoped(s => new FuncDependencyResolver(s.GetRequiredService)); + // Add framework services. // Rollbar service start // ConfigureRollbarSingleton(); @@ -119,6 +121,13 @@ public void ConfigureServices(IServiceCollection services) // Uncomment this if you want use Hangfire // services.AddHangfire(options => GlobalConfiguration.Configuration.UsePostgreSqlStorage(connectionString)); // services.AddSingleton(); + + // ----------------------------------------- GraphQL services ----------------------------------------------- + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + services.AddGraphQL(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -169,6 +178,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF // Uncomment this if you want use Hangfire // app.UseHangfireDashboard(); // app.UseHangfireServer(new BackgroundJobServerOptions(), null, new PostgreSqlStorage(Configuration["ConnectionString"])); + app.UseGraphQL("/graphql"); + // use graphql-playground at default url /ui/playground + app.UseGraphQLPlayground(new GraphQLPlaygroundOptions { Path = "/ui/playground" }); } // Rollbar methods start