0
votes

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

1 Answers

1
votes

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.