Run Microsoft Sqlserver Container On Mac With Azure Data Studio.

Run Microsoft Sqlserver Container On Mac With Azure Data Studio.

Azure Data Studio, a cross-platform database development tool originally branded as SQL Operations Studio and provide seamless database management experience, regardless of whether users are connecting to on-premise or Azure-based data platforms.

Azure Data Studio is a cross-platform database tool for data professionals using the Microsoft family of on-premises and cloud data platforms on Windows, MacOS, and Linux.

–Microsoft

For many standards functions, Azure Data Studio offers a much richer experience that Microsoft’s flagship database management tool, SQL Server Management Studio (SSMS).

Azure data studio has better intelligence as compare to microsoft sql server management studio and more extensive with support for keywords and useful TSQL snippets. For example, with a few key presses you can locate and open the snippet for a CREATE PROCEDURE statement.

once you press tab then complete stored procedure creation base script automatically come and make changes as per requirement.

Download sql server docker image & Run Container

First you have to pull docker image for sql server from docker container registry, here i have selected sql server linux image.

Command: docker pull microsoft/mssql-server-linux

check docker image status by issue docker image command which list all downloaded images from docker registry.

Command: docker images

In Docker we can “install” an image by running it. And that creates the actual Docker Container, which is finally the executable that you are executing. Let’s run our Docker Image with the docker run command:

Command: docker run -d –name homer -e ‘ACCEPT_EULA=Y’ -e ‘SA_PASSWORD=<YourPassword>’ -p 1433:1433 <imageName>

  • -e ‘ACCEPT_EULA=Y’ With the -e option you set an environment variable, on which SQL Server is dependent on. In our case we have to accept the EULA to be able to use SQL Server.
  • -e ‘SA_PASSWORD=Password@123‘ With the SA_PASSWORD environment variable we set the password for the SA login.
  • -p 1433:1433 With the -p option we bind a port on our host machine (in my case on the Mac) to a port in the Container. The port on the left side of the colon is the port on the host machine, and the port on the right side of the colon is the port in the Container. In my case I bind the default SQL Server port of 1433 within the Container to the port 1433 on my Mac.
  • –name : With the –name option we assign a custom name to our Docker Container.
  • -d :And with the -d option we specify the Docker Image that we have pulled previously, and that you want to run the Docker Container detached from the Terminal. This just means that you can close your Terminal, and your Docker Container is still running in the background.

After you have executed that Docker command, your Docker Container is up and running and ready to use.

Connect Azure Data Studio With Container.

we can connect azure data studio with running container here directly to localhost, because in the last step we have exposed the port 1433 of the Docker Container to our host machine.

Once connection established successfully,you can see server details on home pane where you can verify that container id is coming as computer name on azure data studio which means you have successfully connected to running sql server container and now you can execute sql server queries.

Execute Sql Quries

after successfully connection with sqlserver container,right click on database and choose new query option. A query window will option and now you can execute sql queries and run it .

Summary

Run sqlserver on mac is always very tedious task and tools which is available for mac is not easy to use and not much responsive.But docker made developer life easy to run sqlserver on mac os with better user experience.

it’s now real. You can now run natively SQL Server on the Mac, and with the help of Azure Data Studio you can even access SQL Server with a native MacOS.

Moving REST To GraphQL  With Asp.net Core & Entity Framework Core.

Moving REST To GraphQL With Asp.net Core & Entity Framework Core.

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

source: GraphQl

GraphQL queries are used to query the GraphQL server for the data that the client needs. What is interesting about GraphQL is that clients can write custom made queries based on the individual client’s needs. This means that GraphQL enables the client to ask for exactly what they want using a query and also returns a response with only what was asked. This approach gives the client more power.

Benefits of GraphQL:

  • Good fit for complex systems and microservices: By integrating multiple systems behind its API, GraphQL unifies them and hides their complexity. The GraphQL server is then responsible for fetching the data from the existing systems and packaging it up in the GraphQL response format.
  • Fetch data in single call and avoid multiple round trips:GraphQl is less chatty than Rest and rest api’s required multiple round trips between client and resources to fetch the data and return back to client to render on calling apps.

GraphQL solves the roundtrip problem by allowing the client to create a single query which calls several related functions (or resolvers) on the server to construct a response with multiple resources – in a single request. This is a much more efficient method of data delivery requiring fewer resources than multiple roundtrips.

  • Avoid Over/Under data fetching problems:REST api responses are known for either containing too much data or not enough of it, it’s very hard to design an API flexible enough to fulfill every client’s precise data needs. GraphQL solves this efficiency problem by fetching the exact data in a single request.

Building a GraphQL Service in ASP.NET Core

  1. Installing GraphQL in .net core: Since GraphQL support is not provided within ASP.NET Core, you need a Nuget package. Below are the most commonly used nuget packages used in .net core.
1
2
3

2. Setting up graph types: GraphTypes is a Class which derives from the ObjectGraphType base class that implements IObjectGraphType. Now in the constructor you can declare fields for this graph type. 

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Types;
using WebApiWithGraphQl.Data.Entities;
using WebApiWithGraphQl.Repositories;

namespace WebApiWithGraphQl.GraphQ.Types
{
public class EmployeType : ObjectGraphType<Employee>
{
public EmployeType(EmployeeRepository employeeRepository)
{
Field(x => x.EmployeId);
Field(x => x.Name);
Field<EmployeeTypeEnumType>("EmploymentType", resolve: context => context.Source.EmployeType.ToString());

Field<ListGraphType<AddressType>>(

"Address",
resolve: context => employeeRepository.GetAdddressById(context.Source.EmployeId)
);
}
}
}

3. Define Resolver:Now that we have a Employe graph type we need another class that knows how to get Employees. I call it EmployeQuery which also derives from ObjectGraphType.
In the constructor I declare one field, this time I explicitly say that this field must return a list of EmployeType objects. As you can see even list is a special graph type.
Then I give the field a name and in a lambda I can now specify where the data should come from, in other words how to data should be resolved.

Resolvers are the functions responsible for supplying the data requested by the query and is the integration point between our application’s data source and the GraphQL infrastructure.

namespace WebApiWithGraphQl.GraphQ.Query
{
public class EmployeeQuery:ObjectGraphType
{
public EmployeeQuery(EmployeeRepository employeeRepository)
{
Field(
"Employees",
resolve: context => employeeRepository.GetAllEmployees()
);
}
}
}

2. Set up schema: A GraphQL schema is at the center of any GraphQL server implementation and describes the functionality available to the clients which connect to it.

GraphQL implements a human-readable schema syntax known as its Schema Definition Language, or “SDL”. The SDL is used to express the types available within a schema and how those types relate to each other. 

using GraphQL;
using GraphQL.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiWithGraphQl.GraphQ.Query;

namespace WebApiWithGraphQl.GraphQl
{
public class EmployeSchema :Schema
{
public EmployeSchema(IDependencyResolver resolver ):base(resolver)
{
Query = resolver.Resolve();
}
}
}

Configuring Asp.net Core With GraphQL Middleware

To setup GraphQL in your project and start using the scheme we created go to the startup class of your application.

  • Add the dependency resolver to get the query instance.
  • AddGraphQL extension method to register all the types GraphQL .NET uses
  • AddGraphTypes which will scan the assembly for all ObjectGraphTypes and register them automatically in the container using the specified lifetime.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("EmpoyeeDBConnectionString")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "My API", Version = "v1" });
});
services.AddScoped(s => new FuncDependencyResolver(s.GetRequiredService));
services.AddScoped();
services.AddScoped();

services.AddGraphQL(o => { o.ExposeExceptions = false; })
.AddGraphTypes(ServiceLifetime.Scoped);
}

Now we have to inject graphQL middleware by calling UseGraphQL extension method in configure method of startup.cs class.

public void Configure(IApplicationBuilder app, IHostingEnvironment env,EmployeeContext context)
{
app.UseGraphQL();
app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());
context.Seed();

app.UseHttpsRedirection();
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API with graphQl V1");
});

}

Till now we have setup all the mandatory steps for graphQL integration with asp.net core api.If you want to see the playground UI as soon as you start the API go to the properties of the project and select the debug tab. Activate launch browser there and type in ui/playground as the url.

5

When the browser opens take a look at the schema tab on the right side. The metadata of the schema has been read by the playground.
Using this metadata information the query editor can have intellisense.
Type the query in the pic. It gets all products but only the names and descriptions. When I execute it you can see that the result is JSON and the data is contained in the data root node which has a products array with the data I asked for.

6
PlaygroundQueryExecution