GraphQL-java: Error: Introspection result missing interfaces

GraphQL-java: Error: Introspection result missing interfaces

我刚开始使用 GraphQL,但在使用它时遇到了一些问题。 无论我有什么样的架构,我都会得到 Error: Introspection result missing interfaces

架构:

schema {
    query: Query
}

type Query {
    dd: String
}

GraphiQL 的结果:

Error: Introspection result missing interfaces: {"kind":"OBJECT","name":"Query","fields":[{"name":"dd","type":{"kind":"SCALAR","name":"String"},"isDeprecated":false}]}
    at E (file:///C:/Program%20Files/GraphiQL/resources/app.asar/dist/bundle.js:32:22809)
    at _ (file:///C:/Program%20Files/GraphiQL/resources/app.asar/dist/bundle.js:32:22340)
    at r (file:///C:/Program%20Files/GraphiQL/resources/app.asar/dist/bundle.js:32:21822)
    at file:///C:/Program%20Files/GraphiQL/resources/app.asar/dist/bundle.js:32:25249
    at Array.map (native)
    at i (file:///C:/Program%20Files/GraphiQL/resources/app.asar/dist/bundle.js:32:25226)
    at file:///C:/Program%20Files/GraphiQL/resources/app.asar/dist/bundle.js:26:30839
    at process._tickCallback (internal/process/next_tick.js:103:7)

无论我怎么改,我还是报这个错,只是改变量名时引用了不同的地方。 是不是我理解错了什么?

我正在使用 graphql-java 和 Spring 启动并且设置相当基本:

@RestController
public class GraphQLEndpoint {

    @Autowired
    private GraphQL graphQL;

    @PostMapping(path = "/graphql", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Map<String, Object> graphql(@RequestBody GraphQLRequest request, WebRequest webRequest) throws ExecutionException, InterruptedException, JsonProcessingException {
        val query = nonNull(request.getQuery()) ? request.getQuery() : "";
        val variables = (Map<String, Object>) (nonNull(request.getVariables()) ? request.getVariables() : emptyMap());

        val executionInput = ExecutionInput.newExecutionInput()
                .query(query)
                .operationName(request.getOperationName())
                .variables(variables)
                .build();

        return graphQL.execute(executionInput).toSpecification();
    }
}

@Configuration
public class GraphQLConfig {

    @Bean
    GraphQL graphQL(@Value("${classpath:schemas/pdl.graphqls}") ClassPathResource schemaFile) throws IOException {
        val typeRegistry = new SchemaParser().parse(schemaFile.getFile());
        val runtimeWiring = RuntimeWiring.newRuntimeWiring()
                .type(typeRuntimeWiring())
                .build();
        val schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, runtimeWiring);
        return GraphQL.newGraphQL(schema).build();
    }

    private TypeRuntimeWiring typeRuntimeWiring() {
        return TypeRuntimeWiring.newTypeWiring("Query")
                .dataFetcher("dd", environment -> "test")
                .build();
    }
}

我让 jackson 中的 属性 忽略 non_empty 列表,这使得 GraphiQL 和 Altair 抛出异常。

当响应中缺少 GraphiQL 预期的 接口 属性 时,会出现此问题。

使用 graphql-java-servlet 时 - 使用适当的配置定义 GraphQLObjectMapper 有助于即不设置序列化 属性 到 JsonInclude.Include.NON_NULL 以便 interfaces 属性 存在。

   @Bean
   public GraphQLObjectMapper graphQLObjectMapper() {
       return GraphQLObjectMapper.newBuilder()
               .withObjectMapperProvider(ObjectMapper::new)
               .build();
   }