Java – API GraphQL operation
GraphQL can query data through Java API, and obtain specific query data through specific SDL query statements. It is equivalent to the back-end as a “database” to provide data sources. The front end queries the required data according to the defined SDL statement, and gives the control of the query data to the front end to improve the versatility and flexibility of the back-end interface
Introduction of depend on
-
Pom.xml introduces dependencies
<dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java</artifactId> <version>11.0</version> </dependency> Copy the code
You need to configure a third-party Maven repository to download this JAR package, otherwise it cannot be downloaded from a central repository.
The official website, which has warehouses to be configured in quick Start www.graphql-java.com
Java uses the GraphQL API
Query data through Java API according to the simple query syntax defined, query specification statements and refer to this article GraphQL query specification,
-
Simple query without parameters
Through the defined query format, the query is realized through the GraphQL object, and the response data object and the response data need to be constructed first
/** * simply shows GraphQL queries and responds to data via JavaAPI */ public class GraphQLSimpleDemo { public static void main(String[] args) { // Define the data response object GraphQLObjectType userType = createGraphQLObjectType(); // Build the data for the response based on the defined data response object GraphQLFieldDefinition userDefinition = createGraphQLFieldDefinition(userType); // Create a query response GraphQLSchema graphQLSchema = createGraphQLSchema(userDefinition); GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); // Query statement String graph1 = "{User{id, name}}"; // Query multiple fields String graph2 = "{User{id, name, age}}"; // Execute the query ExecutionResult execute = graphQL.execute(graph1); // Get the result System.out.println(execute.toSpecification()); // Execute the query ExecutionResult execute2 = graphQL.execute(graph2); // Get the result System.out.println(execute2.toSpecification()); } / / create GraphQLSchema public static GraphQLSchema createGraphQLSchema(GraphQLFieldDefinition userDefinition) { GraphQLObjectType userQuery = GraphQLObjectType.newObject() .name("userQuery") .field(userDefinition) .build(); return GraphQLSchema.newSchema().query(userQuery).build(); } /** * Create GraphQLFieldDefinition object ** Do the actual query based on the defined query object, return the query data ** Use static objects to build the data, if it is query data, can do the query here ** / public static GraphQLFieldDefinition createGraphQLFieldDefinition(GraphQLObjectType userType) { return GraphQLFieldDefinition.newFieldDefinition() .name("User") .type(userType) // Static data .dataFetcher(new StaticDataFetcher(new User(1L."Test".10))) .build(); } /** * defines the GraphQLObjectType object * which is used to make the name of the query response object and the definition of the query field */ public static GraphQLObjectType createGraphQLObjectType(a) { return GraphQLObjectType.newObject() .name("User") .field(GraphQLFieldDefinition.newFieldDefinition().name("id").type(Scalars.GraphQLLong)) .field(GraphQLFieldDefinition.newFieldDefinition().name("name").type(Scalars.GraphQLString)) .field(GraphQLFieldDefinition.newFieldDefinition().name("age").type(Scalars.GraphQLInt)) .build(); }}Copy the code
-
Simple query with parameters
In a customized query specification, you can define parameters to implement the query. You can obtain parameters from the API to implement the user-defined query. Parameters must be defined according to the specifications
/** * simply shows GraphQL queries and queries via JavaAPI response data ** passing parameters */ public class GraphQLSimpleDemoWithArgs { public static void main(String[] args) { GraphQLObjectType userType = createGraphQLObjectType(); GraphQLFieldDefinition userDefinition = createGraphQLFieldDefinition(userType); GraphQLSchema graphQLSchema = createGraphQLSchema(userDefinition); GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); String graph3 = "{User(id:1){id, name, age}}"; ExecutionResult execute3 = graphQL.execute(graph3); // Get the result System.out.println(execute3.toSpecification()); } / / create GraphQLSchema public static GraphQLSchema createGraphQLSchema(GraphQLFieldDefinition userDefinition) { GraphQLObjectType userQuery = GraphQLObjectType.newObject() .name("userQuery") .field(userDefinition) .build(); return GraphQLSchema.newSchema().query(userQuery).build(); } /** * Create GraphQLFieldDefinition object ** Do the actual query based on the defined query object, return the query data ** Use static objects to build the data, if it is query data, can do the query here ** / public static GraphQLFieldDefinition createGraphQLFieldDefinition(GraphQLObjectType userType) { return GraphQLFieldDefinition.newFieldDefinition() .name("User") .type(userType) // Set parameters to query data .argument(GraphQLArgument.newArgument().name("id").type(Scalars.GraphQLLong).build()) .dataFetcher(environment -> { Long id = environment.getArgument("id"); return new User(id, "name" + id, id.intValue()); }) .build(); } /** * defines the GraphQLObjectType object * which is used to make the name of the query response object and the definition of the query field */ public static GraphQLObjectType createGraphQLObjectType(a) { return GraphQLObjectType.newObject() .name("User") .field(GraphQLFieldDefinition.newFieldDefinition().name("id").type(Scalars.GraphQLLong)) .field(GraphQLFieldDefinition.newFieldDefinition().name("name").type(Scalars.GraphQLString)) .field(GraphQLFieldDefinition.newFieldDefinition().name("age").type(Scalars.GraphQLInt)) .build(); }}Copy the code
In the above two simple examples of GraphQL, one is a query with no parameters, and the other is a query with parameters passed. As you can see, the control of the query data of GraphQL is given to the defined query statement. The data constructed by GraphQL serves as the basic data source. If the interface defined by GraphQL has flexibility and generality, it can be seen that it is also complicated to use. In addition, in the case of multiple and complex interfaces, it is more complicated than Restful. The two methods have their own advantages and disadvantages
In the next article, we’ll take a simple example of defining the interface ~~ in Springboot using GraphQL