Skip to content

Commit

Permalink
Pagination support for Querydsl and QBE
Browse files Browse the repository at this point in the history
See gh-597
  • Loading branch information
rstoyanchev committed Apr 8, 2023
1 parent 2d071a4 commit d9d67c7
Show file tree
Hide file tree
Showing 8 changed files with 626 additions and 66 deletions.
2 changes: 1 addition & 1 deletion platform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
api(platform("io.projectreactor:reactor-bom:2022.0.5"))
api(platform("io.micrometer:micrometer-bom:1.11.0-M2"))
api(platform("io.micrometer:micrometer-tracing-bom:1.1.0-M2"))
api(platform("org.springframework.data:spring-data-bom:2023.0.0-M3"))
api(platform("org.springframework.data:spring-data-bom:2023.0.0-SNAPSHOT"))
api(platform("org.springframework.security:spring-security-bom:6.1.0-M2"))
api(platform("com.querydsl:querydsl-bom:5.0.0"))
api(platform("io.rsocket:rsocket-bom:1.1.3"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNamedOutputType;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLType;
import graphql.schema.idl.FieldWiringEnvironment;
import graphql.schema.idl.RuntimeWiring;
Expand Down Expand Up @@ -76,15 +77,20 @@ public void configure(RuntimeWiring.Builder builder, List<WiringFactory> contain
interface DataFetcherFactory {

/**
* Create a singe item {@code DataFetcher}.
* Create {@code DataFetcher} for a singe item.
*/
DataFetcher<?> single();

/**
* Create {@code DataFetcher} for multiple items.
* Create {@code DataFetcher} for many items.
*/
DataFetcher<?> many();

/**
* Create {@code DataFetcher} for scrolling.
*/
DataFetcher<?> scrollable();

}


Expand Down Expand Up @@ -127,6 +133,11 @@ public boolean providesDataFetcher(FieldWiringEnvironment environment) {
private String getOutputTypeName(FieldWiringEnvironment environment) {
GraphQLType outputType = removeNonNullWrapper(environment.getFieldType());

if (isConnectionType(outputType)) {
String name = ((GraphQLObjectType) outputType).getName();
return name.substring(0, name.length() - 10);
}

if (outputType instanceof GraphQLList) {
outputType = removeNonNullWrapper(((GraphQLList) outputType).getWrappedType());
}
Expand All @@ -142,6 +153,12 @@ private GraphQLType removeNonNullWrapper(GraphQLType outputType) {
return (outputType instanceof GraphQLNonNull wrapper ? wrapper.getWrappedType() : outputType);
}

private boolean isConnectionType(GraphQLType type) {
return (type instanceof GraphQLObjectType objectType &&
objectType.getName().endsWith("Connection") &&
objectType.getField("edges") != null && objectType.getField("pageInfo") != null);
}

private boolean hasDataFetcherFor(FieldDefinition fieldDefinition) {
if (this.existingQueryDataFetcherPredicate == null) {
Map<String, ?> map = this.builder.build().getDataFetcherForType("Query");
Expand All @@ -168,13 +185,9 @@ public DataFetcher<?> getDataFetcher(FieldWiringEnvironment environment) {
DataFetcherFactory factory = dataFetcherFactories.get(outputTypeName);
Assert.notNull(factory, "Expected DataFetcher factory for typeName '" + outputTypeName + "'");

GraphQLType outputType = removeNonNullWrapper(environment.getFieldType());
if (outputType instanceof GraphQLList) {
return factory.many();
}
else {
return factory.single();
}
GraphQLType type = removeNonNullWrapper(environment.getFieldType());
return (isConnectionType(type) ? factory.scrollable() :
(type instanceof GraphQLList ? factory.many() : factory.single()));
}

}
Expand Down
Loading

0 comments on commit d9d67c7

Please sign in to comment.