【问题标题】:Disable GraphQL Introspection in graphql-java-tools在 graphql-java-tools 中禁用 GraphQL Introspection
【发布时间】:2021-03-01 01:53:27
【问题描述】:

我正在尝试在我的项目中禁用GraphQL Introspection,并且对我正在使用的特定框架没有太多运气。一些文章说它可以在CcodeRegistry 模块中完成,但这是一个只读的反编译源。有人用 GraphQL-java-kickstart 框架实现了这一点吗?

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

        <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>

【问题讨论】:

    标签: graphql graphql-java graphql-schema graphql-java-tools


    【解决方案1】:

    Graphql-java

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

    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 时,您可以启用或禁用自省查询。这是一个非常简化的实现。

    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 来禁用自省查询.

    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 的回答,了解如何实现它。

    【讨论】:

    • 问题是我没有使用springboot,项目基于Google Dagger 2,所以必须通过代码来完成。
    • 抱歉,我误读了您的依赖项。我更新了我的答案以涵盖 graphql-java 和 graphql-java-tools。
    • @AllirionX 哪个版本的 spring boot 使它工作,因为我从 4.0.0 升级到 5.4.1,但它仍然不能与提到的给定属性一起工作。关于我在代码中的答案的唯一变化是我使用属性文件而不是 yml
    • @JalajChawla 抱歉,我没有运行 graphql 项目。根据我的 pom.xml 文件,我使用的是 graphql-spring-boot-starter 5.10.0
    • 感谢@AllirionX 我刚刚通过一个新项目解决了这个问题,它适用于 5.4.1 的 spring boot graphql 项目。我测试的一个例子github.com/abhijaykumar/graphql-spring-boot-api
    猜你喜欢
    • 2022-11-11
    • 2019-03-22
    • 2020-09-16
    • 2017-11-16
    • 1970-01-01
    • 2018-09-24
    • 2021-01-02
    • 2018-01-11
    • 2018-08-24
    相关资源
    最近更新 更多