作为更新它的一部分,我很难在java项目中实现GraphQL.我正在尝试将实体(使用Hibernate ORM映射到不同的数据库)连接到GraphQLObjectType.有什么建议我怎么能做到这一点?如果是这样,我可以省略GraphQL数据库配置吗?
我们在项目中使用 graphql,并使用 graphql-java 2.3.0(我们计划更新,但目前不可能)。
我正在尝试执行这样的突变:
mutation {
UPDATE_User(
id:3,
type: null
) {id}
}
Run Code Online (Sandbox Code Playgroud)
回复:
{
"errors": [
{
"validationErrorType": "WrongType",
"message": "Validation error of type WrongType: argument value EnumValue{name='null'} has wrong type",
"locations": [
{
"line": 5,
"column": 7
}
],
"errorType": "ValidationError"
}
],
"data": null
}
Run Code Online (Sandbox Code Playgroud) 我们计划在我们的应用程序中使用 Graphql 作为后端服务器。我们选择 Graphql-Java 来开发我们的 POC。我们遇到了创建我们自己的标量类型来处理 java.util.Map 对象类型的情况。
我们还没有找到任何关于创建自定义标量类型的文档。在示例代码如下
RuntimeWiring buildRuntimeWiring() {
return RuntimeWiring.newRuntimeWiring()
.scalar(CustomScalar)
Run Code Online (Sandbox Code Playgroud)
如何为 CustomScalar 对象完成实现。需要帮忙。
我得到以下内容,似乎无法找到答案.
Error [ValidationError{validationErrorType=FieldUndefined, queryPath=[find_by_id], message=Validation error of type FieldUndefined: Field 'find_by_id' in type 'Query' is undefined @ 'find_by_id', locations=[SourceLocation{line=1, column=2}], description='Field 'find_by_id' in type 'Query' is undefined'}]
Run Code Online (Sandbox Code Playgroud)
我的守则.
询问
@GraphQLQuery(name = "find_by_id")
public Event findById(@GraphQLArgument(name = "id") Long id) {
Run Code Online (Sandbox Code Playgroud)
架构
@EJB
private EventFacade eventFacade; // Normal stateless bean
GraphQLSchema guestSchema = new GraphQLSchemaGenerator()
.withOperationsFromSingleton(eventFacade)
.withValueMapperFactory(new JacksonValueMapperFactory())
.withDefaults()
.generate();
GraphQL graphQL = GraphQL.newGraphQL(guestSchema).build();
Run Code Online (Sandbox Code Playgroud)
要执行的代码
String query = "{find_by_id (id: 1){eventName}}";
ExecutionResult result = graphQL.execute(query);
Run Code Online (Sandbox Code Playgroud)
使用SPQR lib
事件POJO是基本的,eventName为String,抽象(Parent)类为id.实体类位于不同的jar(Entity Jar)中.执行查询和构建模式的代码位于EJB Jar中. …
我想为将来的项目测试GraphQL。该项目将使用Spring Boot,Spring Security和GraphQL。因此,我使用Spring Initializr中的构建在IntelliJ中创建了一个新的Spring Boot App。当然,Spring Boot版本是最新的(2.0.3.RELEASE)现在,我添加了GraphQL和GraphiQL的依赖项。
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
为了对此进行测试,我运行了该应用程序,但没有/graphql端点。在这种情况下,我application.properties毫无问题地对其进行了配置:
# GraphQL
graphql.servlet.mapping=/graphql
graphql.servlet.enabled=true
graphql.servlet.corsEnabled=true
# GraphiQL
graphiql.mapping=/graphiql
graphiql.endpoint=/graphql
graphiql.enabled=true
graphiql.cdn.enabled=true
graphiql.cdn.version=0.11.11
Run Code Online (Sandbox Code Playgroud)
再次测试之后,端点就在那里了,所以现在我可以编写模式,解析器等了。这是我实现的:
架构:
schema {
query: Query
mutation: Mutation
}
type Greeting {
id: ID!
message: String!
}
type Query {
greetingsAll: [Greeting]
}
type Mutation {
greeting(message: String!): Greeting
}
Run Code Online (Sandbox Code Playgroud)
模型:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString
@Entity
public class Greeting …Run Code Online (Sandbox Code Playgroud) 我正在研究 graphQL 和 spring boot 项目。该 API 使用 graphiQL 运行良好,但当尝试使用 Apollo vueJS 使用它时,会导致 CORS 起源错误。
我在 ProductQuery 类中使用 @CrossOrigin 注释,该类实现 GraphQLQueryResolver ,如下所示:
@CrossOrigin(origins = "https://localhost:8081")
public List<Product> getProducts(){return this.productService.findAll(); }
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)
我不知道从哪里开始。你们能帮忙吗
寻找将 Flux 转换为List<Object>. 如果我使用 block() 会出现错误。因此,需要在不阻塞呼叫的情况下进行通话。
Flux.from(Collection.find())
Run Code Online (Sandbox Code Playgroud)
使用反应式编程,但 graphql 期望List<objects>返回 Flux 并出错。
使用 Block() 编写代码
public List<Test> findAll() {
return Flux.from(testCollection.find()).collectList().block();
}
Run Code Online (Sandbox Code Playgroud)
错误 :-
block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-kqueue-7
Run Code Online (Sandbox Code Playgroud)
在这里,我需要返回,因为由于某种原因List<Test>我无法发送。Flux<Test>
我尝试使用以下解析器将我们的模型转换为 GraphQL 模式:
@Component
public class QueryResolver implements GraphQLQueryResolver {
public List<Result> results;
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
类型 java.util.List 不能映射到 GraphQL 类型!由于 GraphQL-Java 在运行时处理擦除的类型,因此只有非参数化的类才能表示 GraphQL 类型。这允许在接口和联合类型中通过 java 类进行反向查找。
我如何解决这个问题,因为我需要使用泛型类型?
我正在使用:graphql-spring-boot 和 graphql-java-tools 来实现。
电影.graphqls
type Movie {
id: Short
name: String
poster: String
releaseDate: String
runtime: String
storyline: String
rated: String
rating: String
inserted: String
}
type Query {
movies: [Movie]
movie(id: ID!): Movie
}
Run Code Online (Sandbox Code Playgroud)
电影模型
@Entity
public class Movie {
private Short id;
private String name;
private String poster;
private Date releaseDate;
private Time runtime;
private String storyline;
private String rated;
private double rating;
private Timestamp inserted;
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我与其他模型没有任何关系。最后是实现GraphQLQueryResolver 的
类
@Component
public class Query implements GraphQLQueryResolver …Run Code Online (Sandbox Code Playgroud) graphql-java ×10
graphql ×8
java ×8
spring-boot ×3
graphiql ×1
graphql-spqr ×1
hibernate ×1
vue-apollo ×1
vue.js ×1