forked from graphql-java-kickstart/graphql-spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate extended GraphQL scalars
- Loading branch information
1 parent
f9125bf
commit 291140c
Showing
10 changed files
with
237 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ spring: | |
name: graphql-subscription-example | ||
server: | ||
port: 9001 | ||
|
||
graphql: | ||
extended-scalars: BigDecimal |
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
68 changes: 68 additions & 0 deletions
68
...re/src/main/java/graphql/kickstart/spring/web/boot/GraphQLExtendedScalarsInitializer.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,68 @@ | ||
package graphql.kickstart.spring.web.boot; | ||
|
||
import graphql.scalars.ExtendedScalars; | ||
import graphql.schema.GraphQLScalarType; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.context.ApplicationContextException; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@NoArgsConstructor | ||
public class GraphQLExtendedScalarsInitializer implements ApplicationContextInitializer<GenericApplicationContext> { | ||
|
||
@Override | ||
public void initialize(final GenericApplicationContext applicationContext) { | ||
final Collection<String> enabledExtendedScalars = getEnabledExtendedScalars(applicationContext); | ||
final Collection<String> validScalarNames = new HashSet<>(); | ||
ReflectionUtils.doWithFields(ExtendedScalars.class, scalarField -> { | ||
if (Modifier.isPublic(scalarField.getModifiers()) && Modifier.isStatic(scalarField.getModifiers()) | ||
&& scalarField.getType().equals(GraphQLScalarType.class)) { | ||
final GraphQLScalarType graphQLScalarType = (GraphQLScalarType) scalarField.get(null); | ||
if (enabledExtendedScalars.contains(graphQLScalarType.getName())) { | ||
applicationContext.registerBean( | ||
graphQLScalarType.getName(), | ||
GraphQLScalarType.class, | ||
() -> graphQLScalarType | ||
); | ||
} | ||
validScalarNames.add(graphQLScalarType.getName()); | ||
} | ||
}); | ||
verifyEnabledScalars(enabledExtendedScalars, validScalarNames); | ||
} | ||
|
||
private void verifyEnabledScalars( | ||
final Collection<String> enabledExtendedScalars, | ||
final Collection<String> validScalarNames | ||
) { | ||
final Collection<String> invalidScalarNames = new HashSet<>(enabledExtendedScalars); | ||
invalidScalarNames.removeAll(validScalarNames); | ||
if (!invalidScalarNames.isEmpty()) { | ||
throw new ApplicationContextException(String.format( | ||
"Invalid extended scalar name(s) found: %s. Valid names are: %s.", | ||
joinNames(invalidScalarNames), | ||
joinNames(validScalarNames) | ||
) | ||
); | ||
} | ||
} | ||
|
||
private String joinNames(final Collection<String> names) { | ||
return names.stream().sorted().collect(Collectors.joining(", ")); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Set<String> getEnabledExtendedScalars(final GenericApplicationContext applicationContext) { | ||
return (Set<String>) applicationContext.getEnvironment() | ||
.getProperty("graphql.extended-scalars", Collection.class, Collections.emptySet()) | ||
.stream().map(String::valueOf).collect(Collectors.toSet()); | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
graphql-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
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
38 changes: 38 additions & 0 deletions
38
...l/kickstart/spring/web/boot/test/extendedscalars/ExtendedScalarAutoConfigurationTest.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,38 @@ | ||
package graphql.kickstart.spring.web.boot.test.extendedscalars; | ||
|
||
import graphql.scalars.ExtendedScalars; | ||
import graphql.schema.GraphQLScalarType; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
import java.util.AbstractMap; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest( | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, | ||
classes = ExtendedScalarAutoConfigurationTest.ExtendedScalarsTestApplication.class | ||
) | ||
@TestPropertySource(properties = "graphql.extended-scalars=BigDecimal") | ||
@DisplayName("Testing extended scalars auto configuration") | ||
public class ExtendedScalarAutoConfigurationTest { | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Test | ||
@DisplayName("The extended scalars initializer should be properly picked up by Spring auto configuration.") | ||
void testAutoConfiguration() { | ||
assertThat(applicationContext.getBeansOfType(GraphQLScalarType.class)) | ||
.containsOnly(new AbstractMap.SimpleEntry<>("BigDecimal", ExtendedScalars.GraphQLBigDecimal)); | ||
} | ||
|
||
@SpringBootApplication | ||
public static class ExtendedScalarsTestApplication { | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...test/java/graphql/kickstart/spring/web/boot/test/extendedscalars/ExtendedScalarsTest.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,75 @@ | ||
package graphql.kickstart.spring.web.boot.test.extendedscalars; | ||
|
||
import graphql.kickstart.spring.web.boot.GraphQLExtendedScalarsInitializer; | ||
import graphql.scalars.ExtendedScalars; | ||
import graphql.schema.GraphQLScalarType; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.WebApplicationType; | ||
import org.springframework.context.ApplicationContextException; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.core.env.MapPropertySource; | ||
import org.springframework.core.env.StandardEnvironment; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.Collections; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
@DisplayName("Testing extended scalars configuration") | ||
public class ExtendedScalarsTest { | ||
|
||
@Test | ||
@DisplayName("Should throw exception at context initialization when invalid extended scalar name is provided.") | ||
void shouldThrowErrorOnStartupIfExtendedScalarDoesNotExists() { | ||
// GIVEN | ||
final SpringApplication application = setupTestApplication("Long,Short,Datee,BadDecimal"); | ||
// THEN | ||
assertThatExceptionOfType(ApplicationContextException.class) | ||
.isThrownBy(application::run) | ||
.withMessage("Invalid extended scalar name(s) found: BadDecimal, Datee. Valid names are: BigDecimal, " + | ||
"BigInteger, Byte, Char, Date, DateTime, JSON, Locale, Long, NegativeFloat, NegativeInt, " + | ||
"NonNegativeFloat, NonNegativeInt, NonPositiveFloat, NonPositiveInt, Object, PositiveFloat, " + | ||
"PositiveInt, Short, Time, Url."); | ||
} | ||
|
||
@Test | ||
@DisplayName("Should not create any extended scalars by default.") | ||
void shouldNotDeclareAnyExtendedScalarsByDefault() { | ||
// GIVEN | ||
final SpringApplication application = setupTestApplication(null); | ||
// WHEN | ||
final ConfigurableApplicationContext context = application.run(); | ||
// THEN | ||
assertThat(context.getBeansOfType(GraphQLScalarType.class)).isEmpty(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Should declare the configured extended scalars.") | ||
void shouldDeclareTheConfiguredScalars() { | ||
// GIVEN | ||
final SpringApplication application = setupTestApplication("Long,Short,BigDecimal,Date"); | ||
// WHEN | ||
final ConfigurableApplicationContext context = application.run(); | ||
// THEN | ||
assertThat(context.getBeansOfType(GraphQLScalarType.class)) | ||
.containsOnly( | ||
new AbstractMap.SimpleEntry<>("Long", ExtendedScalars.GraphQLLong), | ||
new AbstractMap.SimpleEntry<>("Short", ExtendedScalars.GraphQLShort), | ||
new AbstractMap.SimpleEntry<>("BigDecimal", ExtendedScalars.GraphQLBigDecimal), | ||
new AbstractMap.SimpleEntry<>("Date", ExtendedScalars.Date) | ||
); | ||
} | ||
|
||
private SpringApplication setupTestApplication(final String extendedScalarValue) { | ||
final StandardEnvironment standardEnvironment = new StandardEnvironment(); | ||
standardEnvironment.getPropertySources().addFirst(new MapPropertySource("testProperties", | ||
Collections.singletonMap("graphql.extended-scalars", extendedScalarValue))); | ||
final SpringApplication application = new SpringApplication(GraphQLExtendedScalarsInitializer.class); | ||
application.setWebApplicationType(WebApplicationType.NONE); | ||
application.setEnvironment(standardEnvironment); | ||
return application; | ||
} | ||
} |