当前位置: 首页 > 工具软件 > GraphQL Tools > 使用案例 >

graphql-java-tools集成springboot

松增
2023-12-01

GraphQL-java-tools集成到Springboot

本文描述搭建graphql服务器 。基于springboot+graphiql+graphql-java-tools。
graphql-java-tools能简化schema属性到java object的映射。
构建工具使用gradle5.6.4
IDE: IDEAL

1、引入依赖

下面展示一些 内联代码片

buildscript {
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    }
    dependencies {
        classpath('org.springframework.boot:spring-boot-gradle-plugin:2.1.3.RELEASE')
    }
}

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'idea'

group 'com.monday.taskdemo'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter:2.1.3.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-web:2.1.3.RELEASE'
    // 自动schema 映射到resolver、pojo
    implementation 'com.graphql-java-kickstart:graphql-java-tools:6.0.2'
    // web项目必需
    implementation 'com.graphql-java:graphql-java-spring-boot-starter-webmvc:1.0'
    // GraphQL 的浏览器内 IDE
    compile 'com.graphql-java-kickstart:graphiql-spring-boot-starter:5.4.1'
}

2、创建Schema.graphqls

type Query {
    books: [Book!]
}
type Book {
    id: Int!
    name: String!
    author: Author!
}
type Author {
    id: Int!
    name: String!
}

3、创建POJO

public class Author {
    private int id;
    private String name;
    // getter
    // setter
  }
public class Book{
    private int id;
    private String name;
    private int authorId;
    // getter
    // setter
  }

4、创建Resolver(解析器)

public class Query implements GraphQLQueryResolver {

    public List<Book> books() {
        List<Book> books = new ArrayList<>();
        Book book1 = new Book();
        books.add(book1);
        book1.setAuthorId(1001);
        book1.setId(1);
        book1.setName("了不起的盖茨比");

        Book book2 = new Book();
        books.add(book2);
        book2.setAuthorId(1002);
        book2.setId(2);
        book2.setName("瓦尔登湖");
        return books;
    }
}
public class BookResolver implements GraphQLResolver<Book> {
    public Author author(Book book) {
        Author author = new Author();
        author.setId(book.getAuthorId());
        author.setName("佚名");
        return author;
    }
}

5、配置GraphQL实例

@Component
public class GraphQLProvider {
    @Bean
    public GraphQL createGraphQL(){
        GraphQLSchema schema = SchemaParser.newParser()
                .file("schema.graphqls")
                .resolvers(new Query(), new BookResolver())
                .build()
                .makeExecutableSchema();
        return GraphQL.newGraphQL(schema).build();
    }
}

6、访问测试

localhost:8000/graphiql

## 参数
{
  books {
    id
    name
    author{
      id
      name
    }
  }
}
## 返回结果
{
  "data": {
    "books": [
      {
        "id": 1,
        "name": "了不起的盖茨比",
        "author": {
          "id": 1001,
          "name": "佚名"
        }
      },
      {
        "id": 2,
        "name": "凡尔登湖",
        "author": {
          "id": 1002,
          "name": "佚名"
        }
      }
    ]
  }
}

7、源码

https://gitee.com/MondayLiu/graphql-demo.git

module : graphql-java-tools-poc

若有帮助,欢迎star,转载请注明出处!!!

 类似资料: