我正在处理一个 graphql 问题,在该问题中我收到请求的以下错误
{
customer(id: "5ed6092b-6924-4d31-92d0-b77d4d777b47") {
id
firstName
lastName
carsInterested
}
}
"message": "Validation error of type SubSelectionRequired: Sub selection required for type null of field carsInterested @ 'customer/carsInterested'",
Run Code Online (Sandbox Code Playgroud)
下面是我的架构
type Customer {
id: ID!
firstName: String!
lastName: String!
# list of cars that the customer is interested in
carsInterested: [Car!]
}
type Query {
# return 'Customer'
customer(id: ID!): Customer
}
Run Code Online (Sandbox Code Playgroud)
我确实有一个 CustomerResolver,里面有函数 carsInterested。它看起来如下
@Component
public class CustomerResolver implements GraphQLResolver<Customer> {
private final CarRepository carRepo;
public CustomerResolver(CarRepository carRepo) …Run Code Online (Sandbox Code Playgroud) 我想编写一段代码,它将处理如下的GraphQL查询:
query {
group(id: "com.graphql-java")
name(name: "graphql-java")
version(id: "2.3.0")
}
Run Code Online (Sandbox Code Playgroud)
我已经创建了一个数据获取器并在get方法中放置了一个断点:
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
public class TestDataFetcher implements DataFetcher {
public Object get(final DataFetchingEnvironment dataFetchingEnvironment) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我写了下面的代码:
public class Example02 {
public static void main(final String[] args) throws IOException {
final Example02 app = new Example02();
app.run();
}
void run() throws IOException {
final TestDataFetcher testDataFetcher = new TestDataFetcher();
final List<GraphQLFieldDefinition> fields = Lists.newArrayList(
createGroupField(testDataFetcher),
createNameField(),
createVersionField());
final GraphQLObjectType queryType = newObject()
.name("query") …Run Code Online (Sandbox Code Playgroud) 我是GraphQL的新手.我知道这是非常基本的问题.但是尝试花费大量时间而我无法做到.
我的要求是我需要使用java类中的graphql-java api方法发送GraphQL查询.
这是查询:
{
contentItem(itemId: 74152479) {
slug
updatedAt
contributors {
id
isFreeForm
name
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在探索GraphQL,并想知道是否有任何方式重命名响应字段,例如我有一个POJO与这些字段
class POJO {
Long id;
String name;
}
Run Code Online (Sandbox Code Playgroud)
GraphQL查询:
type POJO {
id: Long
name: String
}
Run Code Online (Sandbox Code Playgroud)
我的回答是这样的
{
"POJO" {
"id": 123,
"name": "abc"
}
}
Run Code Online (Sandbox Code Playgroud)
我可以将名称字段重命名为userName,以便我的响应在下面
{
"POJO" {
"id": 123,
"userName": "abc"
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在将我的REST服务器迁移到GraphQL(至少部分).大部分工作已经完成,但我偶然发现了这个问题,我似乎无法解决这个问题:在Graphql查询中使用FetchType.LAZY的OneToMany关系.
我正在使用:https: //github.com/graphql-java/graphql-spring-boot 和 https://github.com/graphql-java/graphql-java-tools进行集成.
这是一个例子:
实体:
@Entity
class Show {
private Long id;
private String name;
@OneToMany
private List<Competition> competition;
}
@Entity
class Competition {
private Long id;
private String name;
}
Run Code Online (Sandbox Code Playgroud)
架构:
type Show {
id: ID!
name: String!
competitions: [Competition]
}
type Competition {
id: ID!
name: String
}
extend type Query {
shows : [Show]
}
Run Code Online (Sandbox Code Playgroud)
解析器:
@Component
public class ShowResolver implements GraphQLQueryResolver {
@Autowired
private ShowRepository showRepository;
public List<Show> getShows() {
return …Run Code Online (Sandbox Code Playgroud) 我正在使用graphql开发一个spring boot项目.我正在使用graphql-java-tools和graphql-spring-boot-starter.我设法使用spring安全性配置安全性和会话管理,如下面的java配置文件中所示.
现在"/ graphql"路径是安全的(只能在请求的http头中发送"基本http身份验证"或会话令牌(x-auth-token)).在任何graphql操作上使用"基本http身份验证"进行身份验证将启动新会话并在标头中发回新会话令牌,并且可以进一步使用该令牌继续该会话.
如何让匿名用户访问某些graphql查询/突变保持上述行为?
如果我将antMatchers("/ graphql").authenticated()更改为antMatchers("/ graphql").permitAll()以允许匿名访问,那么即使我尝试使用"basic"进行身份验证,也不再调用自定义AuthenticationProvider http身份验证".
谢谢!
这是我的配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationProvider authenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
authenticationManagerBuilder.authenticationProvider(authenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/graphql").authenticated()
.and()
.requestCache()
.requestCache(new NullRequestCache())
.and()
.httpBasic()
.and()
.headers()
.frameOptions().sameOrigin() // needed for H2 web console
.and()
.sessionManagement()
.maximumSessions(1)
.maxSessionsPreventsLogin(true)
.sessionRegistry(sessionRegistry());
}
@Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() …Run Code Online (Sandbox Code Playgroud) authentication spring-security spring-boot graphql graphql-java
我必须在响应和请求中使用 GraphQL 中的 Java 对象。
我应该在 GraphQL 模式文件中将每个对象写两次作为“输入”和“类型”吗?用于在请求和响应中获取该对象。
我应该使用 input 和 type 两次定义同一个对象吗?
文件: test.graphqls
input Employee {
id: Integer
name: String
dept: String
active: String
}
type Employee {
id: Integer
name: String
dept: String
active: String
}
Run Code Online (Sandbox Code Playgroud) 来自 graphql 模式的书籍模型
type Book {
id: ID
name: String
pageCount: Int
author: Author
}
Run Code Online (Sandbox Code Playgroud)
所以我有一个 Book 解析器
type Book {
id: ID
name: String
pageCount: Int
author: Author
}
Run Code Online (Sandbox Code Playgroud)
效果很好。
现在假设我正在使用这个 graphql 查询,它只请求 Book 的一个字段
{
bookById(id: "someId") {
name
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,如何获取有关请求字段的 bookById 方法级别的信息(在本例中仅是名称字段)?这对于 GraphQLQueryResolver 概念来说是可能的吗?
dataFetcher 的示例
public class BookResolver implements BookByIdQueryResolver, GraphQLQueryResolver {
private final MockRepository mockRepository;
public BookResolver(MockRepository mockRepository) {
this.mockRepository = mockRepository;
}
@Override
public BookTO bookById(String id) {
return mockRepository.getBookById(id);
}
}
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但我对解析器方式感兴趣。是否可以?
在一个项目中,我将graphql-java和spring boot与postgreSQL数据库一起使用。现在,我想使用3.0.0版中发布的订阅功能。不幸的是,有关使用订阅功能的信息不是很成熟。
如何通过订阅实现实时功能graphql-java?
我正在使用 graphql-spring-boot 库,并使用解析器对象来解析输入查询的值。
下面是一个例子:
@Component
public class BookResolver implements GraphQLQueryResolver {
@Autowired
private BookImpl bookImpl;
@Autowired
private GraphqlValidator validator;
public GetBookOutput getBooks(GetBookQuery getBookQuery) {
validator.validateBookInputQuery(getBookQuery);
GetBookOutput output = bookImpl.getBook(getBookQuery)
return output;
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我想验证 getBookQuery,并在发送到客户端的响应中引发自定义错误。输入 getBookQuery 类型包含一串数字。
下面是我如何实现 GraphqlValidator 类:
@Component
public class GraphqlValidator {
private static final String BOOK_NUMBER_REGEX = " *[0-9]+( *, *[0-9]+)* *";
public void validateBookInputQuery(GetBookInputQuery getBookInputQuery) {
String bookNumber = getBookInputQuery.getBookNumber();
if (!isValidValueForRefVal(bookNumber, BOOK_NUMBER_REGEX)) {
throw new GraphqlInvalidFieldException("Input type getBookInputQuery Invalid", "bookNumber", bookNumber);
}
}
} …Run Code Online (Sandbox Code Playgroud)