【问题标题】:Why is a String that is null returned as java.lang.Object@.?为什么返回 null 的字符串为 java.lang.Object@.?
【发布时间】: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


【解决方案1】:

问题在于进口。 当我将其更改为 import graphql.GraphQL; 时,Eclipse 添加了 import graphql.nextgen.GraphQL;,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 2012-10-09
    • 2015-02-12
    • 2019-05-04
    • 1970-01-01
    • 2011-11-04
    相关资源
    最近更新 更多