如何在 GraphQL-Java 中禁用自省查询?

How to disbale introspection query in GraphQL-Java?

我正在使用 GraphQL-Java version:11.0

来自 Official GraphQL-Java documenetation 我发现我可以禁用内省查询作为 belwo :

GraphQLSchema schema = GraphQLSchema.newSchema()
        .query(StarWarsSchema.queryType)
        .fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY)
        .build();

this SO question's answer

中给出了同样的内容

但问题出在上述解决方案中 fieldVisibility 可用于 GraphQLSchema.newSchema() 返回的对象。

我正在构建 GraphQL 架构,如下所示:

public GraphQLProvider init(GraphQLResolvers graphQLResolvers) {
        GraphQLSchema graphQLSchema = buildSchema (typeRegistry, runtimeWiring);
        this.graphQL = createInstance (graphQLSchema);
        return this;
    }



private GraphQLSchema buildSchema (TypeDefinitionRegistry typeRegistry, RuntimeWiring runtimeWiring) {
        SchemaGenerator schemaGenerator = new SchemaGenerator();
        return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
    }

private GraphQL createInstance (GraphQLSchema graphQLSchema) {
        return GraphQL.newGraphQL(graphQLSchema).build();
    }

正如你在上面的代码中看到的,我没有在任何地方使用 GraphQLSchema.newSchema(),所以我无法为 introspectionQuery 设置 fieldVisibility,任何人都可以建议我如何修改上面的代码容纳 fieldVisibility 个选项?

非常感谢任何帮助。谢谢!

fieldVisibility 选项在 RuntimeWiring 上也可用,所以我就这么做了

RuntimeWiring.Builder builder = graphQLResolvers.newRuntimeWiringBuilder();
 builder.fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY);

希望对大家有所帮助:)