0

I am building a graphql schema using graphql-java. Currently I build all the types programmatically and use the following statement to build the schema:

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

I would like to add a custom scalar type for handling JSON objects. All the examples I have seen for adding custom scalar type are using RuntimeWiring. They suggest I should do the following model:

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

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

Since I am not using RuntimeWiring I am wondering how I can add custom scalar type. I would appreciate your help.

Thanks, Rao

1 Answer 1

2

You can directly instantiate a GraphQLScalarType instance and add it to the additional types in the GraphQL builder :

GraphQLScalarType fooScaler = new GraphQLFooScaler();
types.add(fooScaler);

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

The SchemaGenerator is only used when you build the schema from GraphQL SDL. If you manually construct the Schema in codes , you do not need it.

Sign up to request clarification or add additional context in comments.

1 Comment

Good to hear . So please consider to vote up my answer or accept it ? thanks.

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.