【问题标题】:graphql-java: How to add custom scalar type while generating graphql schema programmatically?graphql-java:如何在以编程方式生成 graphql 模式时添加自定义标量类型?
【发布时间】:2020-10-01 12:34:50
【问题描述】:

我正在使用 graphql-java 构建一个 graphql 模式。目前我以编程方式构建所有类型并使用以下语句来构建架构:

/* Construct the schema */
GraphQLSchema schema = GraphQLSchema.newSchema()
        .query(query)
        .mutation(mutations)
        .additionalTypes(types)
        .codeRegistry(codeRegistryBuilder.build())
        .build();

我想添加一个自定义标量类型来处理 JSON 对象。我看到的所有添加自定义标量类型的示例都使用 RuntimeWiring。他们建议我应该做以下模型:

RuntimeWiring buildRuntimeWiring() {
    return RuntimeWiring.newRuntimeWiring()
            .scalar(CustomScalar)

GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(registry, runtimeWiring);

由于我没有使用 RuntimeWiring,我想知道如何添加自定义标量类型。非常感谢您的帮助。

谢谢, 饶

【问题讨论】:

    标签: graphql-java


    【解决方案1】:

    您可以直接实例化 GraphQLScalarType 实例并将其添加到 GraphQL 构建器中的其他类型:

    GraphQLScalarType fooScaler = new GraphQLFooScaler();
    types.add(fooScaler);
    
    GraphQLSchema schema = GraphQLSchema.newSchema()
            .query(query)
            .mutation(mutations)
            .additionalTypes(types)
            .codeRegistry(codeRegistryBuilder.build())
            .build();
    

    SchemaGenerator 仅在您从 GraphQL SDL 构建架构时使用。如果在代码中手动构建Schema,则不需要。

    【讨论】:

    • 很高兴听到。所以请考虑投票支持我的答案或接受它?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 2022-08-21
    • 2019-09-08
    • 2021-06-14
    • 2020-12-05
    • 2023-03-25
    • 2022-10-20
    相关资源
    最近更新 更多