Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasankavitha authored Jan 9, 2024
2 parents abfdbcb + 9f561d4 commit eac2155
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public CompletableFuture<Integer> concurrent1() {
}

System.out.println("Done concurrent thing 1");
return new Long(System.currentTimeMillis()).intValue();
return Long.valueOf(System.currentTimeMillis()).intValue();
});
System.out.println("Exit concurrent1");

Expand All @@ -59,7 +59,7 @@ public CompletableFuture<Integer> concurrent2() {
}

System.out.println("Done concurrent thing 2");
return new Long(System.currentTimeMillis()).intValue();
return Long.valueOf(System.currentTimeMillis()).intValue();
});
System.out.println("Exit concurrent2");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DgsWebMvcConfigurationPropertiesValidationTest {
context
.withPropertyValues("dgs.graphql.path: /fooql/")
.run { ctx ->
assertThat(ctx).hasFailed().failure.getRootCause().hasMessageContaining("dgs.graphql.path must start with '/' and not end with '/'")
assertThat(ctx).hasFailed().failure.rootCause().hasMessageContaining("dgs.graphql.path must start with '/' and not end with '/'")
}
}

Expand All @@ -45,7 +45,7 @@ class DgsWebMvcConfigurationPropertiesValidationTest {
context
.withPropertyValues("dgs.graphql.path: fooql")
.run { ctx ->
assertThat(ctx).hasFailed().failure.getRootCause().hasMessageContaining("dgs.graphql.path must start with '/' and not end with '/'")
assertThat(ctx).hasFailed().failure.rootCause().hasMessageContaining("dgs.graphql.path must start with '/' and not end with '/'")
}
}

Expand All @@ -63,7 +63,7 @@ class DgsWebMvcConfigurationPropertiesValidationTest {
context
.withPropertyValues("dgs.graphql.graphiql.path: /fooql/")
.run { ctx ->
assertThat(ctx).hasFailed().failure.getRootCause().hasMessageContaining("dgs.graphql.graphiql.path must start with '/' and not end with '/'")
assertThat(ctx).hasFailed().failure.rootCause().hasMessageContaining("dgs.graphql.graphiql.path must start with '/' and not end with '/'")
}
}

Expand All @@ -72,7 +72,7 @@ class DgsWebMvcConfigurationPropertiesValidationTest {
context
.withPropertyValues("dgs.graphql.graphiql.path: fooql")
.run { ctx ->
assertThat(ctx).hasFailed().failure.getRootCause().hasMessageContaining("dgs.graphql.graphiql.path must start with '/' and not end with '/'")
assertThat(ctx).hasFailed().failure.rootCause().hasMessageContaining("dgs.graphql.graphiql.path must start with '/' and not end with '/'")
}
}

Expand All @@ -90,7 +90,7 @@ class DgsWebMvcConfigurationPropertiesValidationTest {
context
.withPropertyValues("dgs.graphql.schema-json.path: /fooql/")
.run { ctx ->
assertThat(ctx).hasFailed().failure.getRootCause().hasMessageContaining("dgs.graphql.schema-json.path must start with '/' and not end with '/'")
assertThat(ctx).hasFailed().failure.rootCause().hasMessageContaining("dgs.graphql.schema-json.path must start with '/' and not end with '/'")
}
}

Expand All @@ -99,7 +99,7 @@ class DgsWebMvcConfigurationPropertiesValidationTest {
context
.withPropertyValues("dgs.graphql.schema-json.path: fooql")
.run { ctx ->
assertThat(ctx).hasFailed().failure.getRootCause().hasMessageContaining("dgs.graphql.schema-json.path must start with '/' and not end with '/'")
assertThat(ctx).hasFailed().failure.rootCause().hasMessageContaining("dgs.graphql.schema-json.path must start with '/' and not end with '/'")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ class GraphiQLPathConfigWithCustomGraphQLPathAndServletContextTest(
absPathWithoutContextPath,
String::class.java
)
assertThat(graphqlResponse.statusCodeValue).isEqualTo(HttpStatus.NOT_FOUND.value())
assertThat(graphqlResponse.statusCode).isEqualTo(HttpStatus.NOT_FOUND)

// graphql is available with context path in uri (400 expected as we don't sent proper request)
val absPathWithContextPath = "$rootUri/zuzu"
graphqlResponse = testRestTemplate.getForEntity(
absPathWithContextPath,
String::class.java
)
assertThat(graphqlResponse.statusCodeValue).isEqualTo(HttpStatus.BAD_REQUEST.value())
assertThat(graphqlResponse.statusCode).isEqualTo(HttpStatus.BAD_REQUEST)

val graphiqlResponse = testRestTemplate.getForEntity(
"/graphiql",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DgsWebSocketConfigurationPropertiesValidationTest {
.withPropertyValues("dgs.graphql.websocket.path: /pws/")
.run { ctx ->
Assertions.assertThat(ctx).hasFailed()
.failure.rootCause.hasMessageContaining("dgs.graphql.websocket.path must start with '/' and not end with '/'")
.failure.rootCause().hasMessageContaining("dgs.graphql.websocket.path must start with '/' and not end with '/'")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void execute(@NotNull Runnable task) {
}

@Override
@Deprecated
public void execute(@NotNull Runnable task, long startTimeout) {
throw new UnsupportedOperationException("VirtualThreadTaskExecutor is only supported on JDK 21+");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void execute(@NotNull Runnable task) {
}

@Override
@Deprecated
public void execute(@NotNull Runnable task, long startTimeout) {
var future = new FutureTask<>(task, null);
execute(future);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import reactor.core.publisher.Mono
import java.lang.reflect.InvocationTargetException
import java.util.*
import java.util.Locale
import java.util.Optional
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException
import java.util.concurrent.CompletionStage
import java.util.stream.Collectors

@DgsComponent
open class DefaultDgsFederationResolver() :
Expand All @@ -68,7 +68,6 @@ open class DefaultDgsFederationResolver() :
/**
* Used when the DefaultDgsFederationResolver is extended.
*/
@Suppress("JoinDeclarationAndAssignment")
@Autowired
lateinit var entityFetcherRegistry: EntityFetcherRegistry

Expand All @@ -82,23 +81,25 @@ open class DefaultDgsFederationResolver() :
}

private fun valuesWithMappedScalars(graphQLContext: GraphQLContext, values: Map<String, Any>, scalarMappings: Map<List<String>, Coercing<*, *>>, currentPath: MutableList<String>): Map<String, Any> {
return values.entries.stream()
.map {
currentPath.add(it.key)

val newValue = if (scalarMappings[currentPath] != null) {
scalarMappings[currentPath]!!.parseValue(it.value, graphQLContext, Locale.getDefault())
} else if (it.value is Map<*, *>) {
valuesWithMappedScalars(graphQLContext, it.value as Map<String, Any>, scalarMappings, currentPath)
} else {
it.value
}
return values.mapValues { (key, value) ->
currentPath += key

val converter = scalarMappings[currentPath]

val newValue = if (converter != null) {
converter.parseValue(value, graphQLContext, Locale.getDefault())
} else if (value is Map<*, *>) {
@Suppress("UNCHECKED_CAST")
value as Map<String, Any>
valuesWithMappedScalars(graphQLContext, value, scalarMappings, currentPath)
} else {
value
}

currentPath.removeLast()
currentPath.removeLast()

Pair(it.key, newValue)
}
.collect(Collectors.toMap({ it.first }, { it.second!! }))
newValue!!
}
}

private fun dgsEntityFetchers(env: DataFetchingEnvironment): CompletableFuture<DataFetcherResult<List<Any?>>> {
Expand Down Expand Up @@ -128,7 +129,7 @@ open class DefaultDgsFederationResolver() :
}

if (result == null) {
logger.error("@DgsEntityFetcher returned null for type: $typename")
logger.error("@DgsEntityFetcher returned null for type: {}", typename)
CompletableFuture.completedFuture(null)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider
import com.netflix.graphql.dgs.DgsExecutionResult
import com.netflix.graphql.dgs.context.DgsContext
import com.netflix.graphql.dgs.exceptions.DgsBadRequestException
import com.netflix.graphql.types.errors.TypedGraphQLError
import graphql.ExecutionInput
import graphql.ExecutionResult
import graphql.GraphQL
import graphql.GraphQLContext
import graphql.GraphQLError
import graphql.execution.ExecutionIdProvider
import graphql.execution.ExecutionStrategy
import graphql.execution.instrumentation.Instrumentation
import graphql.execution.preparsed.PreparsedDocumentProvider
import graphql.schema.GraphQLSchema
import org.dataloader.registries.ScheduledDataLoaderRegistry
import org.intellij.lang.annotations.Language
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.util.StringUtils
import java.util.*
import java.util.Optional
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException

object BaseDgsQueryExecutor {

Expand Down Expand Up @@ -104,30 +104,61 @@ object BaseDgsQueryExecutor {
idProvider.ifPresent { graphQLBuilder.executionIdProvider(it) }

val graphQL: GraphQL = graphQLBuilder.build()
val graphQLContextFuture = CompletableFuture<GraphQLContext>()
val dataLoaderRegistry = dataLoaderProvider.buildRegistryWithContextSupplier { graphQLContextFuture.get() }

lateinit var executionInput: ExecutionInput
val dataLoaderRegistry = dataLoaderProvider.buildRegistryWithContextSupplier { executionInput.graphQLContext }

@Suppress("DEPRECATION")
executionInput = ExecutionInput.newExecutionInput()
.query(query)
.operationName(operationName)
.variables(inputVariables)
.dataLoaderRegistry(dataLoaderRegistry)
.context(dgsContext)
.graphQLContext(dgsContext)
.extensions(extensions.orEmpty())
.build()

return try {
@Suppress("DEPRECATION")
val executionInput = ExecutionInput.newExecutionInput()
.query(query)
.operationName(operationName)
.variables(inputVariables)
.dataLoaderRegistry(dataLoaderRegistry)
.context(dgsContext)
.graphQLContext(dgsContext)
.extensions(extensions.orEmpty())
.build()
graphQLContextFuture.complete(executionInput.graphQLContext)
graphQL.executeAsync(executionInput).whenComplete { _, _ ->
if (dataLoaderRegistry is ScheduledDataLoaderRegistry) {
dataLoaderRegistry.close()
val future = graphQL.executeAsync(executionInput)

if (dataLoaderRegistry is AutoCloseable) {
future.whenComplete { _, _ -> dataLoaderRegistry.close() }
}

future.exceptionally { exc ->
val cause = if (exc is CompletionException) {
exc.cause
} else {
exc
}
if (cause is GraphQLError) {
DgsExecutionResult
.builder()
.status(HttpStatus.BAD_REQUEST)
.executionResult(ExecutionResult.newExecutionResult().addError(cause).build())
.build()
} else {
DgsExecutionResult
.builder()
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.executionResult(
ExecutionResult.newExecutionResult().addError(
TypedGraphQLError.newInternalErrorBuilder().build()
).build()
)
.build()
}
}
} catch (e: Exception) {
logger.error("Encountered an exception while handling query {}", query, e)
val errors: List<GraphQLError> = if (e is GraphQLError) listOf<GraphQLError>(e) else emptyList()
CompletableFuture.completedFuture(ExecutionResult.newExecutionResult().errors(errors).build())
val executionResult = ExecutionResult.newExecutionResult()
if (e is GraphQLError) {
executionResult.addError(e)
} else {
executionResult.addError(TypedGraphQLError.newInternalErrorBuilder().build())
}
CompletableFuture.completedFuture(executionResult.build())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class DefaultDgsQueryExecutor(
if (result.errors.size > 0) {
val nullValueError = result.errors.find { it is NonNullableFieldWasNullError }
if (nullValueError != null) {
logger.error(nullValueError.message)
logger.error("{}", nullValueError.message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ class DefaultDgsFederationResolverTest {
// Define a mock movie entity fetcher that throws an EntityNotFoundException for movieId 1
val movieEntityFetcher = object {
@DgsEntityFetcher(name = "Movie")
fun movieEntityFetcher(values: Map<String, Any>, dfe: DgsDataFetchingEnvironment?): Movie {
fun movieEntityFetcher(values: Map<String, Any>): Movie {
if (values["movieId"] == "1") {
throw DgsEntityNotFoundException("No entity found for movieId 1")
}
Expand All @@ -645,7 +645,7 @@ class DefaultDgsFederationResolverTest {
// Define a mock show entity fetcher that throws an EntityNotFoundException for showId 2
val showEntityFetcher = object {
@DgsEntityFetcher(name = "Show")
fun showEntityFetcher(values: Map<String, Any>, dfe: DgsDataFetchingEnvironment?): Show {
fun showEntityFetcher(values: Map<String, Any>): Show {
if (values["showId"] == "2") {
throw DgsEntityNotFoundException("No entity found for showId 2")
}
Expand Down
Loading

0 comments on commit eac2155

Please sign in to comment.