Java Spring -如何在Rest URI下执行GraphQL查询?

8gsdolmq  于 2023-04-19  发布在  Spring
关注(0)|答案(1)|浏览(110)

我有一个像/books这样的Rest URI,在这个URI中,它将只执行与书籍相关的任何类型的GraphQL查询,查询取决于请求参数,如?id?name等。
我试图使用Spring框架实现这个想法,但得到了以下错误:
Servlet.service()for servlet [dispatcherServlet] in context with path [] throughed exception〉[Circular view path [books]:将再次调度回当前处理程序URL [/books]〉。检查ViewResolver设置!(提示:这可能是由于默认视图名称生成而导致的未指定〉视图的结果。)]
jakarta.servlet.ServletException:循环视图路径[书籍]:将再次调度回〉当前处理程序URL [/books]。检查您的ViewResolver设置!(提示:这可能是一个未指定视图的〉结果,因为默认的视图名称生成。)
这样的执行力可以实现吗?如果可以,我做错了什么?
下面是Controller的代码:

package com.example.graphqlserver;

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
// import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BookController {
    @GetMapping("/books")
    public Book getBook(@RequestParam(name = "id", defaultValue = "") String id) {
        return this.bookById(id);
    }

    @QueryMapping
    public Book bookById(@Argument String id) {
        return Book.getById(id);
    }

    @SchemaMapping
    public Author author(Book book) {
        return Author.getById(book.authorId());
    }
}

类的代码:
书籍:

package com.example.graphqlserver;

import java.util.Arrays;
import java.util.List;

public record Book(String id, String name, int pageCount, String authorId) {

    private static List<Book> books = Arrays.asList(
            new Book("book-1", "Effective Java", 416, "author-1"),
            new Book("book-2", "Hitchhiker's Guide to the Galaxy", 208, "author-2"),
            new Book("book-3", "Down Under", 436, "author-3"));

    public static Book getById(String id) {
        return books.stream()
                .filter(book -> book.id().equals(id))
                .findFirst()
                .orElse(null);
    }
}

作者:

package com.example.graphqlserver;

import java.util.Arrays;
import java.util.List;

public record Author(String id, String firstName, String lastName) {

    private static List<Author> authors = Arrays.asList(
            new Author("author-1", "Joshua", "Bloch"),
            new Author("author-2", "Douglas", "Adams"),
            new Author("author-3", "Bill", "Bryson"));

    public static Author getById(String id) {
        return authors.stream()
                .filter(author -> author.id().equals(id))
                .findFirst()
                .orElse(null);
    }
}

GraphQL架构:

type Query {
    bookById(id: ID): Book
}

type Book {
    id: ID
    name: String
    pageCount: Int
    author: Author
}

type Author {
    id: ID
    firstName: String
    lastName: String
}
jtw3ybtb

jtw3ybtb1#

我尝试将@RestController@Controller添加到Controller类中,它返回了“某种”预期结果:
localhost:8080/books?id=book-1

{
  "id": "book-1",
  "name": "Effective Java",
  "pageCount": 416,
  "authorId": "author-1"
}

更新代码:

package com.example.graphqlserver;

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Controller
public class BookController {
    @GetMapping("/books")
    public Book getBook(@RequestParam(name = "id", defaultValue = "") String id) {
        return this.bookById(id);
    }

    @QueryMapping
    public Book bookById(@Argument String id) {
        return Book.getById(id);
    }

    @SchemaMapping
    public Author author(Book book) {
        return Author.getById(book.authorId());
    }
}

相关问题