11

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)

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

3 Answers 3

9

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)
Sign up to request clarification or add additional context in comments.

9 Comments

how do I utilize above scaler into my code? I've a Spring-boot application and it'll help if you could provide an example.
@Simple-Solution How do you use any built-in scalar, like String or Integer? There's no difference.
Not sure I understand what you mean. But those; String and Integer are built in and it all happens automatically. So above scalar is for object and I would need to create one for Map, right? Do you've an example. Also what is the significant of the name argument? Is that the name of the type we defined in the schema?
@Simple-Solution Happens automatically? Does that mean you're using GraphQL-SPQR? If so, annotate the type at the place of use with @GraphQLScalar (e.g. annotated a method argument) or use a different ScalarMappingStrategy (e.g. gen.withScalarMappingStrategy(new MapScalarStrategy())). Without SPQR, there's no automatic mapping. You set a type of each field, e.g. newFieldDefinition().type(Scalars.GraphQLString), so you can use newFieldDefinition().type(Scalars.graphQLObjectScalar("typeName")) instead. You'll get a Map just like for any object input, but with a dynamic structure.
@Simple-Solution If using schema-first with graphql-java directly, you can use a custom scalar via RuntimeWiring.newRuntimeWiring().scalar(Scalars.graphQLObjectScalar("typeN‌​ame")). Mind you, in the 2nd and the 3rd example Scalars is io.leangen.graphql.util.Scalars not graphql.Scalars. Or just copy the SPQR code into your own class.
|
0

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());
}
}

Comments

0

add dependency in your pom file

 <dependency>
  <groupId>com.graphql-java</groupId>
  <artifactId>graphql-java-extended-scalars</artifactId>
  <version>21.0</version>
</dependency>

then write a class to register scalar Component

@Configuration

public class ScalarRegister {

   @Bean
    public RuntimeWiring.Builder addLongScalar(RuntimeWiring.Builder builder) {
        return builder.scalar(ExtendedScalars.GraphQLLong);
    }

}

Then, you can use the Long Scalar, which means the Long type. Several scalar types are available, including GraphQLChar, GraphQLLong, GraphQLBigInteger, Currency, Url, Json, and LocalTime. from creating beans you can extend the scalars

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.