在尝试构建彼此依赖的类型时,我碰到了一堵砖墙,这里是代码:
import graphql.schema.GraphQLObjectType;
import static graphql.schema.GraphQLObjectType.newObject;
import static graphql.Scalars.*;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
public class GraphQLTypes {
private GraphQLObjectType studentType;
private GraphQLObjectType classType;
public GraphQLTypes() {
createStudentType();
createClassType();
}
void createStudentType() {
studentType = newObject().name("Student")
.field(newFieldDefinition().name("name").type(GraphQLString).build())
.field(newFieldDefinition().name("currentClass").type(classType).build())
.build();
}
void createClassType() {
classType = newObject().name("Class")
.field(newFieldDefinition().name("name").type(GraphQLString).build())
.field(newFieldDefinition().name("students").type(new GraphQLList(studentType)).build())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
它不可能实例化这个类,因为我得到了这个例外
Caused by: graphql.AssertException: type can't be null
at graphql.Assert.assertNotNull(Assert.java:10)
at graphql.schema.GraphQLFieldDefinition.<init>(GraphQLFieldDefinition.java:23)
at graphql.schema.GraphQLFieldDefinition$Builder.build(GraphQLFieldDefinition.java:152)
at graphql_types.GraphQLTypes.createStudentType(GraphQLTypes.java:26)
at graphql_types.GraphQLTypes.<init>(GraphQLTypes.java:19)
Run Code Online (Sandbox Code Playgroud)
很明显,classType尚未在createStudentType()引用它时被实例化.如何解决这个问题?
由于以下错误 ExecutionStrategy.resolveField() - Exception while fetching data java.lang.ClassCastException: java.util.LinkedHashMap incompatible with com.fathome.graphql.OffersDto
OffersDto inputObject = environment.getArgument("offersInput");
请让我知道下面的代码有什么问题,提前致谢。
package com.fathome.graphql;
import graphql.schema.*;
import static graphql.Scalars.*;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
import static graphql.schema.GraphQLInputObjectType.newInputObject;
import static graphql.schema.GraphQLList.list;
import static graphql.schema.GraphQLObjectType.newObject;
public class ManualGraphQLQuerySchema {
public static GraphQLObjectType offersResponse = newObject()
.name("OffersResponse")
.field(newFieldDefinition()
.name("offerName")
.type(GraphQLString))
.field(newFieldDefinition()
.name("offerId")
.type(GraphQLString))
.build();
public static GraphQLInputObjectType offersRequestType = GraphQLInputObjectType.newInputObject()
.name("OffersDto")
.field(newInputObjectField()
.name("offerName")
.type(GraphQLString))
.field(newInputObjectField()
.name("offerId")
.type(GraphQLString))
.build();
public static GraphQLObjectType queryType = newObject()
.name("QueryType")
.field(newFieldDefinition()
.name("offers") …Run Code Online (Sandbox Code Playgroud) 我正在将 GraphQL 与 java 一起使用。我有几个要解析的模式文件。任何人都可以让我知道是否有办法做到这一点。我使用了下面的方法,它只允许解析一个模式文件。
@Value("classpath:dashboard.graphql")
private Resource schemaResource;
File schemaFile = schemaResource.getFile();
TypeDefinitionRegistry definitionRegistry = new SchemaParser().parse(schemaFile);
Run Code Online (Sandbox Code Playgroud)
假设我有两个模式文件,例如dashboard.graphql和student.graphql。那么我们如何解析这两个文件呢?
任何人都可以请解释我。
谢谢
我们必须重建基于 REST 服务的后端应用程序,并且由于我们在服务中有很多嵌套级别,因此我们决定创新并尝试 GraphQL。
我们开始做一些简单的事情,这个项目看起来很有前途,但是我们开始面临分页等现实世界的问题。在 REST 中,分页方法很简单,我们使用 GET 方法和一些参数,如pageSizeand pageNumber(or offset) 并构建 sql 查询来执行此分页。
在 GraphQL 中,我们按照相同的方法解决问题,例如使用以下查询:
users(size:5 offset:2) {
id
name
}
Run Code Online (Sandbox Code Playgroud)
这种方法看起来很容易实现,但是在深入挖掘之后,我们发现实现这一点的“最佳”模式是连接模式,查询将如下所示:
users(first:2) {
totalCount
edges {
node {
name
}
cursor
}
pageInfo {
endCursor
hasNextPage
}
}
Run Code Online (Sandbox Code Playgroud)
我们的数据保存在关系数据库中,因此我看不到游标如何提供帮助(除非我使用自动增量 ID?)。
为什么这种复杂的方法比简单的方法更受推荐?还有什么 cursor 和 endCursor 将存储?我是否误解了我的学习路径中的某些内容?
我正在开发一个使用 GraphQL 作为后端的 android 应用程序。我让查询和变异部分完美地工作。但是我找不到任何用于身份验证的文件。
那么如何将用户名和密码传递给服务器并对其进行身份验证?
LocalApolloClient.java :
public class LocalApolloClient {
private static final String URL = "http://192.168.1.100/graphql/";
private static ApolloClient apolloClient;
private static String authHeader = "";
public static ApolloClient getApolloClient(){
//HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder().method(original.method(), original.body());
builder.header("Authorization", authHeader);
return chain.proceed(builder.build());
})
.build();
apolloClient = ApolloClient.builder()
.serverUrl(URL)
.okHttpClient(okHttpClient)
.build();
return apolloClient;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意 :
如果你投反对票,那么请为自己辩解。
在我的数据库中,该Event字段是用字符串数组动态填充的,而有些是用文本手动填充的(无数组)。当我执行我的查询时,只填充一个字符串(不在数组中)的结果显示为空。
架构中的字段是: Event: [String]
GraphQL 有没有办法同时分配 [String] 和 String 类型的字段?我尝试了以下操作,但出现语法错误:
union test = [String] | String
Event: test
Run Code Online (Sandbox Code Playgroud)
注意:手动分配的不在数组中。
我正在构建使用 GraphQL 和Leangen graphql-spqr 的Web 应用程序。
我有异常处理问题。例如,在服务类中,我使用 spring bean 验证来检查某些有效性,如果不正确,则抛出 ConstraintViolationException。
有没有办法添加一些异常处理程序来向客户端发送正确的消息?类似于ExceptionHandler用于 rest api 中的控制器?或者也许应该以其他方式完成?
我发现从一个模式文件中分离查询真的很困难。我想要这样的东西:
car.graphqls
type Query {
car(id: ID!): Car
}
type Car {
id: ID!,
name: String!
}
Run Code Online (Sandbox Code Playgroud)
房子.graphqls
type Query {
house(id: ID!): House
}
type House {
id: ID!,
owner: String,
street: String
}
Run Code Online (Sandbox Code Playgroud)
我搜索了很多,但找不到一种方法来编写两个 java 类并getHouse()在其中一个和getCar()另一个中实现。
@Component
public class CarQuery implements GraphQLQueryResolver {
@Autowired
private CarService carService;
public List<Car> getCar(final int id) {
return this.carService.getCar(id);
}
}
public class HouseQuery implements GraphQLQueryResolver {
@Autowired
private HouseService houseService;
public List<House> getHouse(final int id) {
return …Run Code Online (Sandbox Code Playgroud) 是否可以动态创建GraphQL架构?
我们将数据存储在mongoDB中,并且可能会添加新字段.我们不希望在mongoDB文档中对此新添加的字段进行任何代码更改.
我们有什么方法可以动态生成架构吗?
模式在代码中定义,但对于java(模式为pojo),添加新属性时,必须更新并重新编译代码,然后再次存档和部署jar.有什么方法可以通过数据生成模式而不是预先定义它?
目前我们正在使用java相关项目(graphql-java,graphql-java-annotations)进行GraphQL开发.
我是GraphQL和graphql-java的新手。我正在尝试以编程方式创建此架构
type Query{name:String!}
Run Code Online (Sandbox Code Playgroud)
我不知道如何使name字段不为空String!
默认情况下这是可选的。
GraphQLObjectType queryType = GraphQLObjectType.newObject()
.name("Query")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("name")
.type(Scalars.GraphQLString)
.dataFetcher(new StaticDataFetcher("Joe")))
.build();
Run Code Online (Sandbox Code Playgroud)