-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring Data repositories that support Querydsl are now supported as DataFetchers returning single objects and iterables including projection support.
- Loading branch information
Showing
13 changed files
with
811 additions
and
5 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
3 changes: 2 additions & 1 deletion
3
...s/webmvc-http/src/main/java/io/spring/sample/graphql/repository/ArtifactRepositories.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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package io.spring.sample.graphql.repository; | ||
|
||
import org.springframework.data.querydsl.QuerydslPredicateExecutor; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface ArtifactRepositories extends CrudRepository<ArtifactRepository, String> { | ||
public interface ArtifactRepositories extends CrudRepository<ArtifactRepository, String>, QuerydslPredicateExecutor<ArtifactRepository> { | ||
|
||
} |
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
106 changes: 106 additions & 0 deletions
106
spring-graphql/src/main/java/org/springframework/graphql/data/DtoInstantiatingConverter.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,106 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package org.springframework.graphql.data; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.mapping.PersistentEntity; | ||
import org.springframework.data.mapping.PersistentProperty; | ||
import org.springframework.data.mapping.PersistentPropertyAccessor; | ||
import org.springframework.data.mapping.PreferredConstructor; | ||
import org.springframework.data.mapping.PreferredConstructor.Parameter; | ||
import org.springframework.data.mapping.SimplePropertyHandler; | ||
import org.springframework.data.mapping.context.MappingContext; | ||
import org.springframework.data.mapping.model.EntityInstantiator; | ||
import org.springframework.data.mapping.model.EntityInstantiators; | ||
import org.springframework.data.mapping.model.ParameterValueProvider; | ||
|
||
/** | ||
* {@link Converter} to instantiate DTOs from fully equipped domain objects. | ||
* | ||
* @author Mark Paluch | ||
* @since 1.0.0 | ||
*/ | ||
class DtoInstantiatingConverter<T> implements Converter<Object, T> { | ||
|
||
private final Class<T> targetType; | ||
|
||
private final MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context; | ||
|
||
private final EntityInstantiator instantiator; | ||
|
||
/** | ||
* Creates a new {@link Converter} to instantiate DTOs. | ||
* | ||
* @param dtoType target type | ||
* @param context mapping context to be used | ||
* @param entityInstantiators the instantiators to use for object creation | ||
*/ | ||
public DtoInstantiatingConverter(Class<T> dtoType, | ||
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> context, | ||
EntityInstantiators entityInstantiators) { | ||
this.targetType = dtoType; | ||
this.context = context; | ||
this.instantiator = entityInstantiators | ||
.getInstantiatorFor(context.getRequiredPersistentEntity(dtoType)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T convert(Object source) { | ||
|
||
if (targetType.isInterface()) { | ||
return (T) source; | ||
} | ||
|
||
PersistentEntity<?, ?> sourceEntity = context | ||
.getRequiredPersistentEntity(source.getClass()); | ||
|
||
PersistentPropertyAccessor<?> sourceAccessor = sourceEntity | ||
.getPropertyAccessor(source); | ||
PersistentEntity<?, ?> targetEntity = context | ||
.getRequiredPersistentEntity(targetType); | ||
PreferredConstructor<?, ? extends PersistentProperty<?>> constructor = targetEntity | ||
.getPersistenceConstructor(); | ||
|
||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
Object dto = instantiator | ||
.createInstance(targetEntity, new ParameterValueProvider() { | ||
|
||
@Override | ||
public Object getParameterValue(Parameter parameter) { | ||
return sourceAccessor.getProperty(sourceEntity | ||
.getRequiredPersistentProperty(parameter.getName())); | ||
} | ||
}); | ||
|
||
PersistentPropertyAccessor<?> dtoAccessor = targetEntity | ||
.getPropertyAccessor(dto); | ||
|
||
targetEntity.doWithProperties((SimplePropertyHandler) property -> { | ||
|
||
if (constructor.isConstructorParameter(property)) { | ||
return; | ||
} | ||
|
||
dtoAccessor.setProperty(property, | ||
sourceAccessor.getProperty(sourceEntity | ||
.getRequiredPersistentProperty(property.getName()))); | ||
}); | ||
|
||
return (T) dto; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
spring-graphql/src/main/java/org/springframework/graphql/data/DtoMappingContext.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 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package org.springframework.graphql.data; | ||
|
||
import org.springframework.data.mapping.Association; | ||
import org.springframework.data.mapping.PersistentEntity; | ||
import org.springframework.data.mapping.context.AbstractMappingContext; | ||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; | ||
import org.springframework.data.mapping.model.BasicPersistentEntity; | ||
import org.springframework.data.mapping.model.Property; | ||
import org.springframework.data.mapping.model.SimpleTypeHolder; | ||
import org.springframework.data.util.TypeInformation; | ||
|
||
/** | ||
* Lightweight {@link org.springframework.data.mapping.context.MappingContext} to provide class metadata for entity to DTO mapping. | ||
* | ||
* @author Mark Paluch | ||
* @since 1.0.0 | ||
*/ | ||
class DtoMappingContext extends AbstractMappingContext<DtoMappingContext.DtoPersistentEntity<?>, DtoMappingContext.DtoPersistentProperty> { | ||
|
||
@Override | ||
protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) { | ||
|
||
// No Java std lib type introspection to not interfere with encapsulation. We do not want to get into the business of materializing Java types. | ||
if (type.getType().getName().startsWith("java.") || type.getType().getName() | ||
.startsWith("javax.")) { | ||
return false; | ||
} | ||
return super.shouldCreatePersistentEntityFor(type); | ||
} | ||
|
||
@Override | ||
protected <T> DtoPersistentEntity<?> createPersistentEntity(TypeInformation<T> typeInformation) { | ||
return new DtoPersistentEntity<>(typeInformation); | ||
} | ||
|
||
@Override | ||
protected DtoPersistentProperty createPersistentProperty(Property property, DtoPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) { | ||
return new DtoPersistentProperty(property, owner, simpleTypeHolder); | ||
} | ||
|
||
static class DtoPersistentEntity<T> extends BasicPersistentEntity<T, DtoPersistentProperty> { | ||
|
||
public DtoPersistentEntity(TypeInformation<T> information) { | ||
super(information); | ||
} | ||
} | ||
|
||
static class DtoPersistentProperty extends AnnotationBasedPersistentProperty<DtoPersistentProperty> { | ||
|
||
public DtoPersistentProperty(Property property, PersistentEntity<?, DtoPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) { | ||
super(property, owner, simpleTypeHolder); | ||
} | ||
|
||
@Override | ||
protected Association<DtoPersistentProperty> createAssociation() { | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.