Custom Scalar in Graphql-java

后端 未结 2 958
感动是毒
感动是毒 2020-12-11 06:42

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 han

相关标签:
2条回答
  • 2020-12-11 07:29

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

    graphql-java also has a separate project for extra scalars: graphql-java-extended-scalars. And there you can find the object scalar (a.k.a. JSON scalar), that can be used for dynamic structures, like Maps.

    Register it via:

    RuntimeWiring.newRuntimeWiring().scalar(ExtendedScalars.Object)
    
    0 讨论(0)
  • 2020-12-11 07:33

    In code first approach (SPQR v0.9.6) adding @GraphQLScalar is enough. Or, as alternative, add scalar definition to GraphQLSchemaGenerator:

    new GraphQLSchemaGenerator()
    .withScalarMappingStrategy(new MyScalarStrategy())
    

    And define MyScalarStrategy:

    class MyScalarStrategy extends DefaultScalarStrategy {
    
    @Override
    public boolean supports(AnnotatedType type) {
      return super.supports(type) || GenericTypeReflector.isSuperType(MyScalarStrategy.class, type.getType());
    }
    }
    
    0 讨论(0)
提交回复
热议问题