【发布时间】:2019-11-01 16:22:06
【问题描述】:
我的理解是 null 是 graphql 响应中可接受的值。 所以我要么误解了某些东西,要么我做错了什么,因为现在我得到的是“java.lang.Object@7deb1b9”而不是 null。
schema {
query: Query
}
type Query {
greeting: String
}
Graphql 看起来像这样:
final Builder vContextBuilder = new Builder()
.of("locale", mLocale)
.of("securityContext", mSecurityContext);
final ExecutionInput vExecutionInput = ExecutionInput
.newExecutionInput()
.query(mQuery)
.context(vContextBuilder)
.build();
final InputStream vSchemaInputStream = Thread
.currentThread()
.getContextClassLoader()
.getResourceAsStream(SCHEMA_FILE_URI);
final TypeDefinitionRegistry vTypeDefinitionRegistry = new SchemaParser()
.parse(new InputStreamReader(vSchemaInputStream));
final RuntimeWiring vRuntimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", typeWiring -> typeWiring
.dataFetcher("greeting", new GreetingFetcher())
)
.build();
final GraphQLSchema vGraphQLSchema = new SchemaGenerator()
.makeExecutableSchema(vTypeDefinitionRegistry, vRuntimeWiring);
final GraphQL vGraphQL = GraphQL.newGraphQL(vGraphQLSchema)
.build();
final ExecutionResult vExecutionResult = vGraphQL.execute(vExecutionInput);
final Map<String, Object> toSpecificationResult = vExecutionResult.toSpecification();
还有 Greetingfetcher:
public class GreetingFetcher extends StaticDataFetcher {
public GreetingFetcher() {
super("Hello!");
//super(null);
}
}
查询:
query {
greeting
}
来自super("Hello!");的回复:
{
"data": {
"greeting": "Hello!"
}
}
来自super(null);的回复:
{
"data": {
"greeting": "java.lang.Object@7deb1b9"
}
}
响应不应该是:
{
"data": {
"greeting": null
}
}
有人有什么想法吗?
【问题讨论】:
-
null本身就是java中的一个类型。 -
你试过用调试器运行代码吗?
标签: graphql-java