我最近开始使用graphql它并发现它非常有趣。由于大多数我的rest应用程序都在java,我决定做一个快速安装使用所提供的春天开机启动的项目graphql-java团队。它带有graph-iqlautoconf spring 设置,可以更轻松地查询/graphql端点。
在 IDEA 中的项目设置上花了几个小时后,我能够运行graphql-sample-app。但我认为我的 servlet 仍未启用,只有graphiql端点正在运行,因为默认查询返回404.
这是application.yml:
spring:
application:
name: graphql-todo-app
server:
port: 9000
graphql:
spring-graphql-common:
clientMutationIdName: clientMutationId
injectClientMutationId: true
allowEmptyClientMutationId: false
mutationInputArgumentName: input
outputObjectNamePrefix: Payload
inputObjectNamePrefix: Input
schemaMutationObjectName: Mutation
servlet:
mapping: /graphql
enabled: true
corsEnabled: true
graphiql:
mapping: /graphiql
enabled: true
Run Code Online (Sandbox Code Playgroud)
这是我的build.gradle文件的样子:
buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
maven { …Run Code Online (Sandbox Code Playgroud) 我是 GrapQL 的新手。我正在尝试将它与弹簧靴一起使用。我可以成功进行查询,它正在返回我需要的数据,但我现在想使用变异。我需要在他注册时向数据库添加一个用途。
这是我的 schema.graphqls 文件:
type Token {
token: String
}
type Register {
message: String
}
type User {
username: String!
firstName: String!
lastName: String!
password: String!
role: String!
}
type Query {
login(username: String, password: String): Token
}
type Mutation {
register(input: RegisterUserInput!): Register
}
input RegisterUserInput {
username: String!
firstName: String!
lastName: String!
password: String!
role: String!
}
schema {
query: Query
mutation: Mutation
}
Run Code Online (Sandbox Code Playgroud)
所以你可以看到 register 是 Mutation 类型,它和 Query 一样添加到模式中。但出于某种原因,它看起来没有进入 Mutation,它只是试图在 Query 中查找类型。
这是我的控制器: …
我正在使用 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) 我计划在我的 Spring Boot 应用程序中实现 Graphql。我在 Google 上搜索了许多用 Java 设置 Graphql 服务器的站点,并找到了两种方法。
一个是像下面这样实现 GraphQlResolver
public class MyResolver implements GraphQLResolver<ModelX>
Run Code Online (Sandbox Code Playgroud)
另一个是通过实现 Datafetcher 参考:https ://www.graphql-java.com
@Component
public class MyDataFetcher implements DataFetcher<ModelX> {
@Override
public ModelX get(DataFetchingEnvironment environment) {
// TODO Auto-generated method stub
}
}
Run Code Online (Sandbox Code Playgroud)
请提供一些关于两种方法差异和其中最佳方法的信息
我graphql-java-kickstart在 Spring Boot 中使用该库并有一些如下代码:
class MyResolver implements GraphQLQueryResolver {
public String helloWorld(DataFetchingEnvironment env) {
// this works!!
CustomGraphQLContext context = env.getContext();
return "Hello world";
}
}
Run Code Online (Sandbox Code Playgroud)
我在其他地方使用GraphQLServletContextBuilder.
问题是它getContext已被弃用。它说可以选择使用getGraphQLContext,但目前对我来说为空。我如何填充它?如果我需要覆盖ExecutionInput,我该在哪里做?
作为更新它的一部分,我很难在java项目中实现GraphQL.我正在尝试将实体(使用Hibernate ORM映射到不同的数据库)连接到GraphQLObjectType.有什么建议我怎么能做到这一点?如果是这样,我可以省略GraphQL数据库配置吗?
我们计划在我们的应用程序中使用 Graphql 作为后端服务器。我们选择 Graphql-Java 来开发我们的 POC。我们遇到了创建我们自己的标量类型来处理 java.util.Map 对象类型的情况。
我们还没有找到任何关于创建自定义标量类型的文档。在示例代码如下
RuntimeWiring buildRuntimeWiring() {
return RuntimeWiring.newRuntimeWiring()
.scalar(CustomScalar)
Run Code Online (Sandbox Code Playgroud)
如何为 CustomScalar 对象完成实现。需要帮忙。
我尝试了几种变体,没有运气在GraphQL中返回地图。所以我有以下两个对象:
public class Customer {
private String name, age;
// getters & setters
}
public class Person {
private String type;
private Map<String, Customer> customers;
// getters & setters
}
Run Code Online (Sandbox Code Playgroud)
我的架构如下所示:
type Customer {
name: String!
age: String!
}
type Person {
type: String!
customers: [Customer!] // Here I tried all combination but had no luck, is there a Map type support for GQL?
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何实现这一点,以便GraphQL神奇地处理此问题或替代方法。
非常感谢!
在我们的GraphQL SpringBoot应用程序中,我们通过graphql-java-datetime成功地为LocalDateTime使用自定义标量实现
<dependency>
<groupId>com.zhokhov.graphql</groupId>
<artifactId>graphql-datetime-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>4.3.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
只要我们在架构中仅使用它作为String,它就可以正常工作
scalar LocalDateTime
input AttendanceFilter {
# date format yyyy-MM-ddThh:mm:ss
fromDateTime: String
toDateTime: String
}
type Query {
attendanceList(filter: AttendanceFilter): [Attendance]
}
type Attendance {
date: LocalDateTime
}
Run Code Online (Sandbox Code Playgroud)
我们也可以在Java解析器实现中使用Java类型的LocalDateTime
public List<AttendancePOJO> attendanceList(AttendanceFilter filter) {
LocalDateTime fromDateTime = filter.getFromDateTime;
LocalDateTime toDateTime = filter.getToDateTime;
return jpaAttendanceRepository.findByFromTimeAfterAndToTimeBefore(fromDateTime, toDateTime) }
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
public …Run Code Online (Sandbox Code Playgroud) 我必须发送一个带有一些标头和 graphQL 变量的查询作为对 java 中的 GraphQL API 的 POST 调用。我还必须在查询中发送一些标头和身份验证参数。现在,我正在 POSTMAN 中进行手动调用,但我想以编程方式执行此操作。你们能帮我从哪里开始吗?我的查询和变量如下
query sampleQuery($param: SampleQueryParams!, $pagingParam: PagingParams) {
sampleLookup(params: $param, pagingParams: $pagingParam) {
ID
name1
name2
}
}
Run Code Online (Sandbox Code Playgroud)
我的 GraphQL 变量如下:
{"param": {"id": 58763897}, "pagingParam": {"pageNumber": 0, "pageSize": 10 } }
Run Code Online (Sandbox Code Playgroud)
我不知道从哪里开始。你们能帮忙吗