首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在GraphQL -graphql-tools中禁用java自检

在GraphQL -graphql-tools中禁用java自检
EN

Stack Overflow用户
提问于 2020-11-18 03:39:35
回答 1查看 1.2K关注 0票数 0

我试图在我的项目中禁用GraphQL自省,但对我正在使用的特定框架并不是很幸运。一些文章说它可以在CcodeRegistry模块中完成,但那是一个只读的反编译源代码。有没有人用GraphQL-java-kickstart框架做到了这一点?

下面是我的pom文件中的依赖项:

代码语言:javascript
复制
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java</artifactId>
            <version>${graphql.java.version}</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java-kickstart</groupId>
            <artifactId>graphql-java-tools</artifactId>
            <version>${graphql.java.tools.version}</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java-extended-validation</artifactId>
            <version>0.0.3</version>
        </dependency>
EN

回答 1

Stack Overflow用户

发布于 2020-11-18 07:21:27

Graphql-java

使用graphql-java,您可以使用GraphQLSchema.Builder构建一个GraphQLSchema。您需要在构建之前为内省字段设置构建器可见性,以禁用内省查询。

代码语言:javascript
复制
GraphQLSchema.Builder builder = GraphQLSchema.newSchema()
                                     .query(query)
                                     .mutation(mutation)
                                     .subscription(subscription)
                                     .additionalTypes(dictionary);

builder.fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY);

GraphQLSchema = builder.build();

您可以使用graphql-java-tools implementation作为参考。

Graphql-java-tools

通过graphql-java-tools,您可以使用SchemaParserBuilder构建一个SchemaParser。SchemaParserBuilder需要一个SchemaParserOptions对象。在构建SchemaParserOptions时,您可以启用或禁用自检查询。这是一个非常简单的实现。

代码语言:javascript
复制
SchemaParserBuilder builder = new SchemaParserBuilder();
final SchemaParserOptions.Builder optionsBuilder = newOptions();
optionsBuilder.introspectionEnabled(introspectionEnabled);
return builder.options(optionsBuilder.build()).build();

您可以使用graphql-spring-boot implementation作为参考。

Graphql-spring-boot

如果您正在使用graphql-spring-boot,根据graphql-java-tools README,您可以通过在application.properties或application.yml文件中将graphql.tools.introspection-enabled属性设置为false来禁用自检查询。

代码语言:javascript
复制
graphql:
    tools:
        schema-location-pattern: "**/*.graphqls"
        # Enable or disable the introspection query. Disabling it puts your server in contravention of the GraphQL
        # specification and expectations of most clients, so use this option with caution
        introspection-enabled: false  

Graphql-spqr

使用Graphql-spqr的想法与graphql-java中的相同:设置构建器字段的可见性。有关如何实现它,请参阅我对this question的回答。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64882196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档