GRAPHQL Getting Started Guide

Source: Internet
Author: User
Tags http post http request rfc

Under the MicroServices architecture, a complete application may be split into several functionally independent services, each running in a separate container under the support of a PAAs.

Synchronous calls between services are dominated by rest requests, which do not discuss the publication subscriptions for events in an asynchronous call.

For callers who access the service, the first to assemble and issue an HTTP request, and then get the response from the service, you also need to parse the data into an internal data object. This process is not related to business logic and is cumbersome and difficult to scale. Especially in the microservices architecture, involving calls to multiple services, aggregating multiple invocations of several services or a service, processing is very redundant, and there is gap between the business logic. How can you unify calls to services from the perspective of your business needs?

GRAPHQL is a language specification that describes the business-related data model defined by Facebook and provides implementations of a variety of programming languages. Looking at the name is expected to be comparable to the database domain of SQL, the service invocation domain standard.

GRAPHQL is located between the caller of the service and the provider of the service, providing the caller with a uniform, business logic-related service query language.

In fact, GRAPHQL provides a type definition system, similar to the schema definition of a database. With GRAPHQL, you can define data types that are related to business logic and provide data access functions for each type (in fact, setting up a data source). GRAPHQL makes the service provider's version, API, etc. transparent to service callers. The service caller only needs to know the desired type of GRAPHQL.

GRAPHQL can be deployed in conjunction with service providers, or in the form of standalone services.

GitHub has completely replaced the rest API with the GRAPHQL API.
1.GraphQL supported operation types (operation type)
Query (default), GET Mutation,post Subscription

Since it is a query language (like SQL), there are query-related statements.

2. GRAPHQL supported instruction (directives) @include (If:boolean) @skip (If:boolean)

When defining a data type, you can set certain logic through directives.

3. GRAPHQL Schema metadata __schema __type __typekind __field __inputvalue __enumvalue __directive

4. Example

1). Define the Type

Type Query {
  me:user
}

type User {
  id:id
  name:string
}

2). Data access functions that define properties in a type

Public User Query_me (request) {
  return request.auth.user;
}


Public String user_name (User) {
  return user.getname ();
}

3). HTTP Call Request

For GRAPHQL, the query operation can be an HTTP GET request or an HTTP POST request, which is recommended for HTTP POST.

An example of the requested parameter is as follows:

{
  me {
    name
  }
}

For HTTP GET requests, the URI example is as follows:

Http://myapi/graphql? Query={me{name}}

In practice, this path does not go through, when an HTTP GET request is issued, the server log displays an error message as follows:

Java.lang.IllegalArgumentException:Invalid character found in the request target. The valid characters is defined in RFC 7230 and RFC 3986

For HTTP POST requests, the body example is as follows:

{
  "query": "{Me{name}}"
}

4). HTTP response

The body format of the GRAPHQL response is always JSON, and the standard format is as follows:

{
  "data": {...},
  "errors": [...]
}

The body of the response in the example:

{"Data": {"Me": {"name": "Xiangbin HAN"}}}

Reference Links:

http://graphql.cn/

http://graphql.org/

Https://github.com/graphql

English Standard, http://facebook.github.io/graphql/

Chinese specification, http://spec.graphql.cn/

http://graphql.cn/code/

https://developer.github.com/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.