GraphQL Part I: hello, world.

GraphQL with ASP.NET Core (Part- I : Hello World)

Tired of REST? Let's talk about GraphQL, GraphQL provide declarative way to get data from the server. You can see all the benefits from GraphQL official site. At present, this blog series, I will show how to use ASP.NET Core integrated GraphQL, will implement it as a query language API.

This means that you only need to declare the properties you need, as opposed to REST, return to a fixed set of data for a specific endpoint, then you need to find the desired attributes.

For C #, the community has provided an open source graphql-DOTNET , we will use it to complete the project. let's start.

First, start by creating a ASP.NET Core Applications

dotnet new web

We will build a GraphQL middleware (next article), however, the first step, let's start with start with the basics, if you already know a bit about GraphQL. So, consider a simple Hello World application: We will query the server hello attribute, then the server will return "world" string, so that the property must be of type string hello.

In our empty project, add GraphQL Package Penalty for for DOTNET , then, get this package.

dotnet add package GraphQL --version 2.0.0-alpha-912
dotnet restore

This series of articles will be using the latest update package. If I forgot to update the article, making the sample does not work, please leave a message told me I needed updated.

Let's create a query and name HelloWorldQuery. It is a simple class field is used to define the query. Any query class should be derived from ObjectGraphType. Therefore, HelloWorldQuery class should contain a string of fields, as follows:

using GraphQL.Types;
public class HelloWorldQuery : ObjectGraphType
{
    public HelloWorldQuery()
    {
        Field<StringGraphType>(
            name: "hello",
            resolve: context => "world"
        );
    }
}

Field defines the constructor

Note that we use the generic version of Field and passed a GraphQL string type (StringGraphType) to make a query field that contains the string hello to know the type of returns the result.

Now we have a query, then we need to build a schema for it.

In the Configure method Startup.cs file, delete the existing code and paste the following code.

var schema = new Schema { Query = new HelloWorldQuery() };

app.Run(async (context) =>
{
    var result = await new DocumentExecuter().ExecuteAsync(options =>
    {
        options.Schema = schema;
        options.Query = @"
            query {
                hello
            }
        ";
    }).ConfigureAwait(false);

    var json = new DocumentWriter(indent: true).Write(result)
    await context.Response.WriteAsync(json);
});

Note that we create a new instance of HelloWorldQuery by GraphQL schema object.

DocumentExecuter of the ExecuteAsync () method to get a type of ExecutionOptions Action. Here is initialized based on the schema and query execution it provides.

Carefully review the string to Query (doc.Query) of. Here we only request hello field. You can override this string {hello} (query not required portion), it also work.

Finally, the result is performed using the Write DocumentWriter class () function converts to JSON. The final but not the last we will JSON output to the response.

Now run the application

dotnet run

You will get the following output from the browser.

You will find it is not difficult. We can add other fields, for example, or howdy, then let it return the string universe. Add another field follows HelloWorldQuery class.

Field<StringGraphType>(
    name: "howdy",
    resolve: context => "universe"
);

 Now, back to Startup.cs, this time we only howdy query in the query {howdy}. You will receive the following response.

 Also, you can check the following two fields by the query, as follows:

{
    hello
    howdy
}

You will receive the following response:

So, in this step, you can query the section you are interested in from the perspective of declaration. GraphQL intelligent enough to understand your needs and return the appropriate response.

We only grassroots the surface of things. hello world application is useful, because in the next chapter, we will begin to build middleware; we do not have any line of code do not understand.

References:

Guess you like

Origin www.cnblogs.com/haogj/p/9155569.html