I am new to GraphQL and trying to implement graphql api with spring boot but facing error There is no scalar implementation for the named 'LocalDateTime' scalar type
pom.xml
<dependency>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
<groupId>com.graphql-java-generator</groupId>
<artifactId>graphql-java-server-runtime</artifactId>
<version>2.8</version>
</dependency>
<plugin>
<groupId>com.graphql-java-generator</groupId>
<artifactId>graphql-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generateServerCode</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaFilePattern>/graphql/emp.graphqls</schemaFilePattern>
<targetFolder>target/generated-sources/graphql-java</targetFolder>
<packageName>com.graphql.generated</packageName>
<customScalars>
<customScalar>
<graphQLTypeName>LocalDateTime</graphQLTypeName>
<javaType>java.time.LocalDateTime</javaType>
<graphQLScalarTypeStaticField>com.graphql.config.LocalDateTimeScalar.createLocalDateTimeScalar
</graphQLScalarTypeStaticField>
</customScalar>
</customScalars>
<copyRuntimeSources>false</copyRuntimeSources>
<generateBatchLoaderEnvironment>true</generateBatchLoaderEnvironment>
<separateUtilityClasses>true</separateUtilityClasses>
<skipGenerationIfSchemaHasNotChanged>true</skipGenerationIfSchemaHasNotChanged>
</configuration>
</plugin>
Custum Scalar class:
public class LocalDateTimeScalar {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static GraphQLScalarType createLocalDateTimeScalar() {
return GraphQLScalarType.newScalar()
.name("LocalDateTime")
.description("Custom scalar for LocalDateTime'")
.coercing(new Coercing<LocalDateTime, String>() {
@Override
public String serialize(Object dataFetcherResult) {
if (dataFetcherResult instanceof LocalDateTime dateTime) {
return FORMATTER.format(dateTime);
}
throw new CoercingSerializeException("Expected a LocalDateTime object.");
}
@Override
public LocalDateTime parseValue(Object input) {
if (input instanceof String dateTimeStr) {
try {
return LocalDateTime.parse(dateTimeStr, FORMATTER);
} catch (DateTimeParseException e) {
throw new CoercingParseValueException(
"Invalid format. Expected 'yyyy-MM-dd HH:mm:ss'.", e);
}
}
throw new CoercingParseValueException("Expected a String value for LocalDateTime.");
}
@Override
public LocalDateTime parseLiteral(Object input) {
if (input instanceof StringValue stringValue) {
try {
return LocalDateTime.parse(stringValue.getValue(), FORMATTER);
} catch (DateTimeParseException e) {
throw new CoercingParseLiteralException(
"Invalid LocalDateTime literal. Expected 'yyyy-MM-dd HH:mm:ss'.", e);
}
}
throw new CoercingParseLiteralException("Expected a StringValue for LocalDateTime literal.");
}
}).build();
}
@Bean
public RuntimeWiringConfigurer runtimeWiringConfigurer() {
return builder -> builder.scalar(createLocalDateTimeScalar());
}
}
Exception: graphql.schema.idl.errors.SchemaProblem: errors=[There is no scalar implementation for the named 'LocalDateTime' scalar type]