Detailed explanation of building query service using graphql in Java
background
With the open source of react, Facebook has opened many related projects. These projects have been used internally for many years. What attracts my attention is that the discussion this time is about graphql. At present, the official version is only nodejs. Because the background technology stack of many companies is Java, there is a java version of graphql, which can be found on GitHub. There is no more nonsense, Look at the code directly. You'd better go to the official Internet cafe for specific introduction, otherwise you'll get off the subject.
GraphQLSchema
Schema is equivalent to a database. It is composed of many graphqlfielddefinitions. Field is equivalent to database tables / views. Each table / view is composed of name, query parameters, data structure and data
1) First define a data structure (graphqloutputtype) field, and then define an initialization method
2) Then define two tables / views, including name, query parameters, data structure, and data retriever
3) Then define a schema and initialize it, which contains a name and one or more tables / views (fields)
4) After completing the above steps, you need to define a model with unlimited class names, but the structure must meet the data structure defined above and must be public
5) Then write a main method to test it
Output:
6) Finally, put the code in the main method into the web layer. You only need to define a query parameter to easily build the query service. The original query interface is still called in the datafetcher
7) Introducing Maven dependency
About the definition of graphql query, it may be helpful for you to see this
json
query
The latter part is actually a JSON string. The result of removing = and value is still readable
epilogue
Graphql brings a new way of thinking, which can simplify the development of Web API, specify what data is needed by the client and what data is returned by the server, reduce unnecessary traffic transmission, be friendly to the mobile terminal, and also provide a variety of data aggregation queries. Multiple queries only use one request, which not only meets the minimum granularity of API, but also meets the needs of the front end and reduces requests, Improve performance.
I feel that I will develop in this direction in the future, driven by the general trend.