spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.52k stars 297 forks source link

LocalDateTime field becoming null always #491

Closed Rajesh-Thiyagarajan closed 2 years ago

Rajesh-Thiyagarajan commented 2 years ago

public class LocalDateTimeScalar implements Coercing<LocalDateTime, String> {

    @Override
    public String serialize( Object dataFetcherResult ) throws CoercingSerializeException {

        if ( dataFetcherResult instanceof LocalDateTime ) {
            return ( (LocalDateTime) dataFetcherResult ).format( DateTimeFormatter.ISO_DATE_TIME );
        } else {
            throw new CoercingSerializeException( "Not a valid DateTime" );
        }
    }

    @Override
    public LocalDateTime parseValue( Object input ) throws CoercingParseValueException {

        return LocalDateTime.parse( input.toString(), DateTimeFormatter.ISO_DATE_TIME );
    }

    @Override
    public LocalDateTime parseLiteral( Object input ) throws CoercingParseLiteralException {

        if ( input instanceof StringValue ) {
            return LocalDateTime.parse( ( (StringValue) input ).getValue(), DateTimeFormatter.ISO_DATE_TIME );
        }

        throw new CoercingParseLiteralException( "Value is not a valid ISO date time" );
    }
}

pom.xml


<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
        </dependency>

Response image


**Sample Entity**
@Entity
@Table(name = "template")
public class Template {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long                        id;

    @Column(name = "name")
    private String                      name;

    @Column(name = "created_date")
    private LocalDateTime               createdAt;

    @Column(name = "updated_date")
    private LocalDateTime               updatedAt;

//setter getter

}

package com.template.service.scalar;

import graphql.schema.GraphQLScalarType;

public class Scalars {

    public static GraphQLScalarType localDateTimeType() {

        return GraphQLScalarType.newScalar().name( "LocalDateTime" ).description( "LocalDateTime type" ).coercing( new LocalDateTimeScalar() ).build();
    }
}

Runtimewire config


import org.springframework.graphql.execution.RuntimeWiringConfigurer;
import org.springframework.stereotype.Component;

import com.template.service.scalar.Scalars;

import graphql.schema.idl.RuntimeWiring;

@Component
public class PostsRuntimeWiring implements RuntimeWiringConfigurer {

    @Override
    public void configure( RuntimeWiring.Builder builder ) {

        builder.scalar( Scalars.localDateTimeType() ).build();
    }

}

Could you please someone help me on this!!!

hantsy commented 2 years ago

Do you declare LocalDateTime in your sdl schema?

Check my example, https://github.com/hantsy/spring-graphql-sample/blob/master/spring-graphql/src/main/java/com/example/demo/gql/scalars/LocalDateTimeScalar.java

Rajesh-Thiyagarajan commented 2 years ago

Do you declare LocalDateTime in your sdl schema?

Check my example, https://github.com/hantsy/spring-graphql-sample/blob/master/spring-graphql/src/main/java/com/example/demo/gql/scalars/LocalDateTimeScalar.java

Thanks for your reply @hantsy . I followed for your example all are looks same, but result alone as not expected.

sample schema.graphqls


scalar LocalDateTime

type Query {
    templates(input : TemplateFilter): [Template]
    template(id: Int!): Template
}

type Template {
    id : Int!   
    name : String!
    createdDate : LocalDateTime
    attachment : [TemplateAttachments]
}

type TemplateAttachments {
    id : Int!
    name : String!
}
benneq commented 2 years ago

I see no createdDate in your Entity, there's just createdAt

Rajesh-Thiyagarajan commented 2 years ago

I see no createdDate in your Entity, there's just createdAt

@benneq Thank you very much, already i spent more days on this. It's fixed.