The original address: https://xeblog.cn/articles/6
Introduction to the
Thousands of words summed up in one sentence: MY future is up to me.
GraphQL is a query language for apis, a server-side runtime that executes queries using a type based system (defined by your data). GraphQL isn’t tied to any particular database or storage engine, but relies on your existing code and data. (Perfect Copy)
Why use it?
- The front-end defines the returned data and structure to reduce the communication cost between the front and back ends
- No interface documentation required (GraphQL automatically generates API documentation based on the schema)
- Schema splicing can combine and connect multiple GraphQL apis into one, reducing the number of requests
using
Based on the use of SpringBoot
Rely on
<dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-spring-boot-starter</artifactId> < version > 4.0.0 < / version > < / dependency > < the dependency > < groupId > com. Graphql - Java < / groupId > < artifactId > graphql - Java - tools < / artifactId > < version > 4.3.0 < / version > < / dependency > <! --> <dependency> <groupId>com.graphql- Java </groupId> <artifactId>graphiql-spring-boot-starter</artifactId> The < version > 4.0.0 < / version > < / dependency >Copy the code
define
Service Entry Definition
Create a file named root.graphqls in the Resources directory
# query related interface
type Query {
Get user information by idgetUserById(id: Int!) : User# User info list
listUser: [User]
}
# Change the related interface
type Mutation {
Add user informationsaveUser(user: addUserInput!) : BooleanDelete user informationdeleteUser(id: Int!) : Boolean# Update user informationupdateUser(user: updateUserInput!) : Boolean }Copy the code
Query defines the entry associated with a Query
Mutation defines entries associated with additions, deletions, and changes
Analyze the
GeUserById: the same as the method name in the Java class id: method parameter, Int(I uppercase) indicates an Int type,"!"Non-null User: the return value type, which returns a Java object [type]: indicates a listCopy the code
The scheme is defined
{beanName}.graphqls = {beanName}.graphqls = {beanName}.graphqls = {beanName}.graphqls = {beanName}.graphqls = {beanName}.graphqls = {beanName}
Example: the user graphqls
# user information object
type User {
# number
id: Int!
# username
username: String!
# your password
password: String!
# age
age: Int
}
Add user information input parameter
input addUserInput {
id: Int!
username: String!
password: String!
age: Int
}
# update user information input parameters
input updateUserInput {
id: Int!
username: String!
password: String!
age: Int
}
Copy the code
Realize the entrance
Entity class
Query related Java class implementation
- Implement the GraphQLQueryResolver interface
- Add the @Component annotation
- The method name, parameter type, and return value are as defined in root.graphqls
Mutation related Java class implementation
- Implement the GraphQLMutationResolver interface
- Add the @Component annotation
- The method name, parameter type, and return value are as defined in root.graphqls
Interface debugging
The default request path for graphQL is/graphQL
Visual debugging interface: / graphiQL
Debugging using GraphiQL
Access the address: http://localhost:8080/graphiql
The interface looks like this
Query
Example Query a single piece of data
Query all data
Combination query
Interfaces with the same name are combined into one interface call, and an alias is required to distinguish them
Define aliases in a format like this
Aliases: interfacesCopy the code
Mutation
add
Modify the
delete
Console
Use Postman for debugging
Query
Example Query a single piece of data
Query all data
Combination query
Mutation
add
Modify the
delete
reference
graphql.cn/learn/