Graphql-java中的自定义标量 [英] Custom Scalar in Graphql-java

查看:993
本文介绍了Graphql-java中的自定义标量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们计划在我们的应用程序中使用Graphql作为后端服务器。我们选择Graphql-Java来开发我们的POC。我们遇到了一个创建自己的scalartype来处理java.util.Map对象类型的情况。

We are planning use Graphql as backend server in our application. We choose Graphql-Java to develop our POC. We came across a stituation to create our own scalartype to handle java.util.Map object type.

我们还没有找到有关创建自定义标量类型的任何文档。
在下面的示例代码中

we havent found any documentation regarding creating a custom scalar type. In example code as below

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

如何为CustomScalar对象完成实现。
需要帮助。

how to was the implementation done for CustomScalar object. need help.

推荐答案

为了大致了解如何制作标量,只需看看现有的和做类似的事情。

To get a general idea how to make a scalar, just take a look into the existing ones and do something similar.

对于动态对象标量,请看一下 graphql-spqr的对象标量实现并做类似的事情:

For a dynamic object scalar, take a look at graphql-spqr's object scalar implementation and do something similar:

public static GraphQLScalarType graphQLObjectScalar(String name) {
        return new GraphQLScalarType(name, "Built-in object scalar", new Coercing() {

            @Override
            public Object serialize(Object input) {
                return input;
            }

            @Override
            public Object parseValue(Object input) {
                return input;
            }

            @Override
            public Object parseLiteral(Object input) {
                return parseFieldValue((Value) input);
            }

            //recursively parse the input into a Map
            private Object parseFieldValue(Value value) {
                if (value instanceof StringValue) {
                    return ((StringValue) value).getValue();
                }
                if (value instanceof IntValue) {
                    return ((IntValue) value).getValue();
                }
                if (value instanceof FloatValue) {
                    return ((FloatValue) value).getValue();
                }
                if (value instanceof BooleanValue) {
                    return ((BooleanValue) value).isValue();
                }
                if (value instanceof EnumValue) {
                    return ((EnumValue) value).getName();
                }
                if (value instanceof NullValue) {
                    return null;
                }
                if (value instanceof ArrayValue) {
                    return ((ArrayValue) value).getValues().stream()
                            .map(this::parseFieldValue)
                            .collect(Collectors.toList());
                }
                if (value instanceof ObjectValue) {
                    return ((ObjectValue) value).getObjectFields().stream()
                            .collect(Collectors.toMap(ObjectField::getName, field -> parseFieldValue(field.getValue())));
                }
                //Should never happen, as it would mean the variable was not replaced by the parser
                throw new IllegalArgumentException("Unsupported scalar value type: " + value.getClass().getName());
            }
        });
    }

这篇关于Graphql-java中的自定义标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆