【问题标题】:GraphQL-java: Error: Introspection result missing interfacesGraphQL-java:错误:自省结果缺少接口
【发布时间】:2019-09-19 09:35:18
【问题描述】:

我刚开始使用 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)

无论我更改什么,我仍然会遇到这个错误,除了当我更改变量名时它引用了不同的地方。 是不是我误解了什么?

我在 Spring Boot 中使用 graphql-java,而且设置相当基本:

@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();
    }
}

【问题讨论】:

    标签: graphql-java


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2020-11-12
        • 2019-04-02
        • 2019-12-19
        • 2019-11-05
        • 2019-02-14
        • 1970-01-01
        • 1970-01-01
        • 2019-02-05
        • 1970-01-01
        相关资源
        最近更新 更多