-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new scalar Calendar (and GregorianCalendar) as a new DateTime s…
…calar
- Loading branch information
Showing
16 changed files
with
409 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
.../implementation/src/main/java/io/smallrye/graphql/transformation/CalendarTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.smallrye.graphql.transformation; | ||
|
||
import static io.smallrye.graphql.SmallRyeGraphQLServerMessages.msg; | ||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Locale; | ||
|
||
import io.smallrye.graphql.schema.model.Field; | ||
import io.smallrye.graphql.schema.model.Transformation; | ||
|
||
public class CalendarTransformer implements Transformer<Calendar, String> { | ||
|
||
private final String targetClassName; | ||
private final DateFormat dateFormat; | ||
|
||
public CalendarTransformer(final Field field, final String targetClassName) { | ||
this.dateFormat = getDateFormat(field.getTransformation()); | ||
this.targetClassName = targetClassName; | ||
} | ||
|
||
public CalendarTransformer(final Field field) { | ||
this(field, field.getReference().getClassName()); | ||
} | ||
|
||
@Override | ||
public Calendar in(String o) throws Exception { | ||
if (dateFormat == null) { | ||
throw msg.notValidDateOrTimeType(targetClassName); | ||
} | ||
return new Calendar.Builder().setInstant(dateFormat.parse(o)).build(); | ||
} | ||
|
||
@Override | ||
public String out(Calendar o) { | ||
return dateFormat.format(o.getTime()); | ||
} | ||
|
||
private static DateFormat getDateFormat(Transformation formatter) { | ||
if (formatter == null) { | ||
// Default format if no formatter is provided | ||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | ||
} | ||
String format = formatter.getFormat(); | ||
if (format == null) | ||
return null; | ||
String localeTag = formatter.getLocale(); | ||
|
||
// Create SimpleDateFormat with the specified format and locale | ||
Locale locale = (localeTag != null) ? Locale.forLanguageTag(localeTag) : Locale.getDefault(); | ||
return new SimpleDateFormat(format, locale); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
server/integration-tests/src/test/java/io/smallrye/graphql/tests/calendar/CalendarTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package io.smallrye.graphql.tests.calendar; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.URL; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.RunAsClient; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import io.smallrye.graphql.tests.GraphQLAssured; | ||
|
||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class CalendarTest { | ||
|
||
@Deployment | ||
public static WebArchive deployment() { | ||
return ShrinkWrap.create(WebArchive.class, "calendar-test.war") | ||
.addClasses(SomeApi.class); | ||
} | ||
|
||
@ArquillianResource | ||
URL testingURL; | ||
|
||
@Test | ||
public void queryWithCalendarReturnTypeAndArgumentTest() { | ||
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL); | ||
|
||
String response = graphQLAssured | ||
.post("{ someCalendar(calendar: \"2018-05-05T11:50:45.314Z\") }"); | ||
assertThat(response).contains("{\"data\":{\"someCalendar\":\"2018-05-05T11:50:45.314Z\"}}") | ||
.doesNotContain("error"); | ||
} | ||
|
||
@Test | ||
public void queryWithGregorianCalendarReturnTypeAndArgumentTest() { | ||
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL); | ||
|
||
String response = graphQLAssured | ||
.post("{ someGregorianCalendar(calendar: \"2011-05-05T11:50:45.112Z\") }"); | ||
assertThat(response).contains("{\"data\":{\"someGregorianCalendar\":\"2011-05-05T11:50:45.112Z\"}}") | ||
.doesNotContain("error"); | ||
} | ||
|
||
@Test | ||
public void queryWithFormattedCalendarReturnTypeAndArgumentTest() { | ||
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL); | ||
String response = graphQLAssured | ||
.post("{ someFormattedCalendar(calendar: \"2023 04 at 13 hours\") }"); | ||
assertThat(response).contains("{\"data\":{\"someFormattedCalendar\":\"01. April 2023 at 01:00 PM\"}}") | ||
.doesNotContain("error"); | ||
} | ||
|
||
@Test | ||
public void queryWithFormattedGregorianCalendarReturnTypeAndArgumentTest() { | ||
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL); | ||
String response = graphQLAssured | ||
.post("{ someFormattedGregorianCalendar(calendar: \"2023 04 at 13 hours\") }"); | ||
assertThat(response).contains("{\"data\":{\"someFormattedGregorianCalendar\":\"01. April 2023 at 01:00 PM\"}}") | ||
.doesNotContain("error"); | ||
} | ||
|
||
@Test | ||
public void queryWithWrongCalendarFormatTest() { | ||
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL); | ||
|
||
String response = graphQLAssured | ||
.post("{ someCalendar(calendar: \"30th of August 2000\") }"); | ||
assertThat(response).containsIgnoringWhitespaces("{\n" + | ||
" \"errors\": [\n" + | ||
" {\n" + | ||
" \"message\": \"argument 'calendar' with value 'StringValue{value='30th of August 2000'}' is not a valid 'DateTime'\",\n" | ||
+ | ||
" \"locations\": [\n" + | ||
" {\n" + | ||
" \"line\": 1,\n" + | ||
" \"column\": 16\n" + | ||
" }\n" + | ||
" ],\n" + | ||
" \"extensions\": {\n" + | ||
" \"classification\": \"ValidationError\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" ],\n" + | ||
" \"data\": {\n" + | ||
" \"someCalendar\": null\n" + | ||
" }\n" + | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void queryWithWrongGregorianCalendarFormatTest() { | ||
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL); | ||
|
||
String response = graphQLAssured | ||
.post("{ someGregorianCalendar(calendar: \"30th of August 2000\") }"); | ||
assertThat(response).containsIgnoringWhitespaces("{\n" + | ||
" \"errors\": [\n" + | ||
" {\n" + | ||
" \"message\": \"argument 'calendar' with value 'StringValue{value='30th of August 2000'}' is not a valid 'DateTime'\",\n" | ||
+ | ||
" \"locations\": [\n" + | ||
" {\n" + | ||
" \"line\": 1,\n" + | ||
" \"column\": 25\n" + | ||
" }\n" + | ||
" ],\n" + | ||
" \"extensions\": {\n" + | ||
" \"classification\": \"ValidationError\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" ],\n" + | ||
" \"data\": {\n" + | ||
" \"someGregorianCalendar\": null\n" + | ||
" }\n" + | ||
"}"); | ||
} | ||
} |
Oops, something went wrong.