Combine querydsl and querybyexample into one package
Following the addition of Query By Example, it makes sense to consolidate the two that mirror each other, are closely related, and can share package private support. See gh-191
This commit is contained in:
@@ -34,7 +34,7 @@ import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
import org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor;
|
||||
import org.springframework.graphql.boot.GraphQlAutoConfiguration;
|
||||
import org.springframework.graphql.boot.GraphQlSourceBuilderCustomizer;
|
||||
import org.springframework.graphql.data.querydsl.QuerydslDataFetcher;
|
||||
import org.springframework.graphql.data.query.QuerydslDataFetcher;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,7 +34,7 @@ import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
import org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor;
|
||||
import org.springframework.graphql.boot.GraphQlAutoConfiguration;
|
||||
import org.springframework.graphql.boot.GraphQlSourceBuilderCustomizer;
|
||||
import org.springframework.graphql.data.querydsl.QuerydslDataFetcher;
|
||||
import org.springframework.graphql.data.query.QuerydslDataFetcher;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querybyexample;
|
||||
package org.springframework.graphql.data.query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -111,6 +111,7 @@ class PropertySelection {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
enum EmptyFieldSelection implements FieldSelection {
|
||||
|
||||
INSTANCE;
|
||||
@@ -132,6 +133,7 @@ class PropertySelection {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Hierarchical representation of selected fields. Allows traversing the
|
||||
* object graph with nested fields.
|
||||
@@ -153,6 +155,7 @@ class PropertySelection {
|
||||
|
||||
}
|
||||
|
||||
|
||||
static class DataFetchingFieldSelection implements FieldSelection {
|
||||
|
||||
private final List<SelectedField> selectedFields;
|
||||
@@ -195,4 +198,5 @@ class PropertySelection {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querybyexample;
|
||||
package org.springframework.graphql.data.query;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querydsl;
|
||||
package org.springframework.graphql.data.query;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
@@ -15,15 +15,17 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* {@link graphql.schema.DataFetcher} implementations built on Querydsl, Spring
|
||||
* Data repositories.
|
||||
* {@link graphql.schema.DataFetcher} implementations built on Spring Data
|
||||
* extensions such as Query by Example and Querydsl.
|
||||
*
|
||||
* @see <a href="https://docs.spring.io/spring-data/commons/docs/current/reference/html/#query-by-example">
|
||||
* Spring Data Query By Example</a>
|
||||
* @see <a href="https://docs.spring.io/spring-data/commons/docs/current/reference/html/#core.extensions.querydsl">
|
||||
* Spring Data Querydsl extension</a>
|
||||
* Spring Data Querydsl</a>
|
||||
*/
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package org.springframework.graphql.data.querydsl;
|
||||
package org.springframework.graphql.data.query;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2021 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* {@link graphql.schema.DataFetcher} implementations built on the Spring Data,
|
||||
* Query by Example support.
|
||||
*
|
||||
* @see <a href="https://docs.spring.io/spring-data/commons/docs/current/reference/html/#query-by-example">
|
||||
* Spring Data Query By Example extension</a>
|
||||
*/
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package org.springframework.graphql.data.querybyexample;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
||||
@@ -1,196 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2021 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querydsl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import graphql.schema.DataFetchingFieldSelectionSet;
|
||||
import graphql.schema.SelectedField;
|
||||
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Utility to compute {@link PropertyPath property paths} from
|
||||
* a {@link DataFetchingFieldSelectionSet field selection} considering an underlying
|
||||
* Java type.
|
||||
* <p>
|
||||
* Property paths are created for each selected field that corresponds with a property
|
||||
* on the underlying type. Nested properties are represented with nested paths
|
||||
* if the nesting can be resolved to a concrete type, otherwise the nested path
|
||||
* is considered to be a composite property without further inspection.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class PropertySelection {
|
||||
|
||||
private final List<PropertyPath> propertyPaths;
|
||||
|
||||
private PropertySelection(List<PropertyPath> propertyPaths) {
|
||||
this.propertyPaths = propertyPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a property selection for the given {@link TypeInformation type} and
|
||||
* {@link DataFetchingFieldSelectionSet}.
|
||||
*
|
||||
* @param typeInformation the type to inspect
|
||||
* @param selectionSet the field selection to apply
|
||||
* @return a property selection holding all selectable property paths.
|
||||
*/
|
||||
public static PropertySelection create(TypeInformation<?> typeInformation,
|
||||
DataFetchingFieldSelectionSet selectionSet) {
|
||||
return create(typeInformation, new DataFetchingFieldSelection(selectionSet));
|
||||
}
|
||||
|
||||
private static PropertySelection create(TypeInformation<?> typeInformation, FieldSelection selection) {
|
||||
List<PropertyPath> propertyPaths = collectPropertyPaths(typeInformation,
|
||||
selection,
|
||||
path -> PropertyPath.from(path, typeInformation));
|
||||
return new PropertySelection(propertyPaths);
|
||||
}
|
||||
|
||||
private static List<PropertyPath> collectPropertyPaths(TypeInformation<?> typeInformation,
|
||||
FieldSelection selection, Function<String, PropertyPath> propertyPathFactory) {
|
||||
List<PropertyPath> propertyPaths = new ArrayList<>();
|
||||
|
||||
for (SelectedField selectedField : selection) {
|
||||
|
||||
String propertyName = selectedField.getName();
|
||||
TypeInformation<?> property = typeInformation.getProperty(propertyName);
|
||||
|
||||
if (property == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
PropertyPath propertyPath = propertyPathFactory.apply(propertyName);
|
||||
FieldSelection nestedSelection = selection.select(selectedField);
|
||||
|
||||
List<PropertyPath> pathsToAdd = Collections.singletonList(propertyPath);
|
||||
|
||||
if (!nestedSelection.isEmpty() && property.getActualType() != null) {
|
||||
List<PropertyPath> nestedPaths = collectPropertyPaths(property.getRequiredActualType(),
|
||||
nestedSelection, propertyPath::nested);
|
||||
|
||||
if (!nestedPaths.isEmpty()) {
|
||||
pathsToAdd = nestedPaths;
|
||||
}
|
||||
}
|
||||
|
||||
propertyPaths.addAll(pathsToAdd);
|
||||
}
|
||||
|
||||
return propertyPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the property paths as list.
|
||||
*/
|
||||
public List<String> toList() {
|
||||
return this.propertyPaths.stream().map(PropertyPath::toDotPath)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Hierarchical representation of selected fields. Allows traversing the
|
||||
* object graph with nested fields.
|
||||
*/
|
||||
interface FieldSelection extends Iterable<SelectedField> {
|
||||
|
||||
/**
|
||||
* @return {@code true} if the field selection is empty
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* Obtain the field selection (nested fields) for a given {@code field}.
|
||||
* @param field the field for which nested fields should be obtained
|
||||
* @return the field selection. Can be empty.
|
||||
*/
|
||||
FieldSelection select(SelectedField field);
|
||||
|
||||
}
|
||||
|
||||
static class DataFetchingFieldSelection implements FieldSelection {
|
||||
|
||||
private final List<SelectedField> selectedFields;
|
||||
private final List<SelectedField> allFields;
|
||||
|
||||
DataFetchingFieldSelection(DataFetchingFieldSelectionSet selectionSet) {
|
||||
this.selectedFields = selectionSet.getImmediateFields();
|
||||
this.allFields = selectionSet.getFields();
|
||||
}
|
||||
|
||||
private DataFetchingFieldSelection(List<SelectedField> selectedFields,
|
||||
List<SelectedField> allFields) {
|
||||
this.selectedFields = selectedFields;
|
||||
this.allFields = allFields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return selectedFields.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldSelection select(SelectedField field) {
|
||||
List<SelectedField> selectedFields = new ArrayList<>();
|
||||
|
||||
for (SelectedField selectedField : allFields) {
|
||||
if (field.equals(selectedField.getParentField())) {
|
||||
selectedFields.add(selectedField);
|
||||
}
|
||||
}
|
||||
|
||||
return (selectedFields.isEmpty() ? EmptyFieldSelection.INSTANCE
|
||||
: new DataFetchingFieldSelection(selectedFields, allFields));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<SelectedField> iterator() {
|
||||
return this.selectedFields.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum EmptyFieldSelection implements FieldSelection {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldSelection select(SelectedField field) {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<SelectedField> iterator() {
|
||||
return Collections.emptyIterator();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querydsl;
|
||||
package org.springframework.graphql.data.query;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.graphql.Author;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querydsl;
|
||||
package org.springframework.graphql.data.query;
|
||||
|
||||
import com.querydsl.core.types.Path;
|
||||
import com.querydsl.core.types.PathMetadata;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querydsl;
|
||||
package org.springframework.graphql.data.query;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.graphql.data.querybyexample.jpa;
|
||||
package org.springframework.graphql.data.query.jpa;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querybyexample.jpa;
|
||||
package org.springframework.graphql.data.query.jpa;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.graphql.data.querybyexample.jpa;
|
||||
package org.springframework.graphql.data.query.jpa;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.graphql.data.GraphQlRepository;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querybyexample.jpa;
|
||||
package org.springframework.graphql.data.query.jpa;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
@@ -43,7 +43,7 @@ import org.springframework.data.repository.query.QueryByExampleExecutor;
|
||||
import org.springframework.graphql.BookSource;
|
||||
import org.springframework.graphql.GraphQlResponse;
|
||||
import org.springframework.graphql.GraphQlSetup;
|
||||
import org.springframework.graphql.data.querybyexample.QueryByExampleDataFetcher;
|
||||
import org.springframework.graphql.data.query.QueryByExampleDataFetcher;
|
||||
import org.springframework.graphql.web.WebGraphQlHandler;
|
||||
import org.springframework.graphql.web.WebInput;
|
||||
import org.springframework.graphql.web.WebOutput;
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.graphql.data.querybyexample.mongodb;
|
||||
package org.springframework.graphql.data.query.mongo;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querybyexample.mongodb;
|
||||
package org.springframework.graphql.data.query.mongo;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.graphql.data.querybyexample.mongodb;
|
||||
package org.springframework.graphql.data.query.mongo;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.graphql.data.GraphQlRepository;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.graphql.data.querybyexample.mongodb;
|
||||
package org.springframework.graphql.data.query.mongo;
|
||||
|
||||
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
|
||||
import org.springframework.graphql.data.GraphQlRepository;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querybyexample.mongodb;
|
||||
package org.springframework.graphql.data.query.mongo;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
@@ -45,7 +45,7 @@ import org.springframework.data.repository.query.QueryByExampleExecutor;
|
||||
import org.springframework.graphql.BookSource;
|
||||
import org.springframework.graphql.GraphQlResponse;
|
||||
import org.springframework.graphql.GraphQlSetup;
|
||||
import org.springframework.graphql.data.querybyexample.QueryByExampleDataFetcher;
|
||||
import org.springframework.graphql.data.query.QueryByExampleDataFetcher;
|
||||
import org.springframework.graphql.web.WebGraphQlHandler;
|
||||
import org.springframework.graphql.web.WebInput;
|
||||
import org.springframework.graphql.web.WebOutput;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.graphql.data.querybyexample.mongodb;
|
||||
package org.springframework.graphql.data.query.mongo;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
@@ -44,7 +44,7 @@ import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
|
||||
import org.springframework.graphql.BookSource;
|
||||
import org.springframework.graphql.GraphQlResponse;
|
||||
import org.springframework.graphql.GraphQlSetup;
|
||||
import org.springframework.graphql.data.querybyexample.QueryByExampleDataFetcher;
|
||||
import org.springframework.graphql.data.query.QueryByExampleDataFetcher;
|
||||
import org.springframework.graphql.web.WebGraphQlHandler;
|
||||
import org.springframework.graphql.web.WebInput;
|
||||
import org.springframework.graphql.web.WebOutput;
|
||||
Reference in New Issue
Block a user