FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add DataLoader 5.0.0 by dondonz · Pull Request #3958 · graphql-java/graphql-java · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .gradle  (1) .groovy  (9) .java  (3) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion build.gradle
This file contains hidden or 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
Expand Up @@ -105,7 +105,7 @@ tasks.withType(GroovyCompile) {
}
dependencies {
implementation 'org.antlr:antlr4-runtime:' + antlrVersion
api 'com.graphql-java:java-dataloader:4.0.0'
api 'com.graphql-java:java-dataloader:5.0.0'
api 'org.reactivestreams:reactive-streams:' + reactiveStreamsVersion
api "org.jspecify:jspecify:1.0.0"
antlr 'org.antlr:antlr4:' + antlrVersion
Expand Down
This file contains hidden or 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
Expand Up @@ -87,7 +87,7 @@ class DataLoaderCacheCanBeAsyncTest extends Specification {
def valueCache = new CustomValueCache()
valueCache.store.put("a", [id: "cachedA", name: "cachedAName"])

DataLoaderOptions options = DataLoaderOptions.newOptions().setValueCache(valueCache).setCachingEnabled(true)
DataLoaderOptions options = DataLoaderOptions.newOptions().setValueCache(valueCache).setCachingEnabled(true).build()
DataLoader userDataLoader = DataLoaderFactory.newDataLoader(userBatchLoader, options)

registry = DataLoaderRegistry.newRegistry()
Expand Down
This file contains hidden or 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
Expand Up @@ -100,9 +100,9 @@ private static List<List<Department>> getDepartmentsForShops(List<Shop> shops) {

public DataLoader<String, List<Department>> departmentsForShopDataLoader = DataLoaderFactory.newDataLoader(departmentsForShopsBatchLoader);

public DataFetcher<CompletableFuture<List<Department>>> departmentsForShopDataLoaderDataFetcher = environment -> {
public DataFetcher<?> departmentsForShopDataLoaderDataFetcher = environment -> {
Shop shop = environment.getSource();
return departmentsForShopDataLoader.load(shop.getId());
return environment.getDataLoader("departments").load(shop.getId());
};

// Products
Expand Down Expand Up @@ -136,9 +136,9 @@ private static List<List<Product>> getProductsForDepartments(List<Department> de

public DataLoader<String, List<Product>> productsForDepartmentDataLoader = DataLoaderFactory.newDataLoader(productsForDepartmentsBatchLoader);

public DataFetcher<CompletableFuture<List<Product>>> productsForDepartmentDataLoaderDataFetcher = environment -> {
public DataFetcher<?> productsForDepartmentDataLoaderDataFetcher = environment -> {
Department department = environment.getSource();
return productsForDepartmentDataLoader.load(department.getId());
return environment.getDataLoader("products").load(department.getId());
};

private <T> CompletableFuture<T> maybeAsyncWithSleep(Supplier<CompletableFuture<T>> supplier) {
Expand Down
This file contains hidden or 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
Expand Up @@ -2,6 +2,7 @@


import com.google.common.collect.ImmutableList;
import org.dataloader.BatchLoader;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderFactory;

Expand All @@ -26,12 +27,13 @@ public DataLoaderCompanyProductBackend(int companyCount, int projectCount) {
mkCompany(projectCount);
}

projectsLoader = DataLoaderFactory.newDataLoader(keys -> getProjectsForCompanies(keys).thenApply(projects -> keys
BatchLoader<UUID, List<Project>> uuidListBatchLoader = keys -> getProjectsForCompanies(keys).thenApply(projects -> keys
.stream()
.map(companyId -> projects.stream()
.filter(project -> project.getCompanyId().equals(companyId))
.collect(Collectors.toList()))
.collect(Collectors.toList())));
.collect(Collectors.toList()));
projectsLoader = DataLoaderFactory.newDataLoader(uuidListBatchLoader);

}

Expand Down
This file contains hidden or 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
Expand Up @@ -48,7 +48,7 @@ class DataLoaderCompanyProductMutationTest extends Specification {
newTypeWiring("Company").dataFetcher("projects", {
environment ->
DataLoaderCompanyProductBackend.Company source = environment.getSource()
return backend.getProjectsLoader().load(source.getId())
return environment.getDataLoader("projects-dl").load(source.getId())
}))
.type(
newTypeWiring("Query").dataFetcher("companies", {
Expand Down
This file contains hidden or 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
Expand Up @@ -192,7 +192,7 @@ class DataLoaderHangingTest extends Specification {
})
}, executor)
}
}, DataLoaderOptions.newOptions().setMaxBatchSize(5))
}, DataLoaderOptions.newOptions().setMaxBatchSize(5).build())

def dataLoaderSongs = DataLoaderFactory.newDataLoader(new BatchLoader<DataFetchingEnvironment, List<Object>>() {
@Override
Expand All @@ -209,7 +209,7 @@ class DataLoaderHangingTest extends Specification {
})
}, executor)
}
}, DataLoaderOptions.newOptions().setMaxBatchSize(5))
}, DataLoaderOptions.newOptions().setMaxBatchSize(5).build())

def dataLoaderRegistry = new DataLoaderRegistry()
dataLoaderRegistry.register("artist.albums", dataLoaderAlbums)
Expand Down
This file contains hidden or 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
Expand Up @@ -11,6 +11,7 @@ import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLSchema
import graphql.schema.StaticDataFetcher
import org.dataloader.DataLoader
import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification

Expand Down Expand Up @@ -69,39 +70,42 @@ class DataLoaderNodeTest extends Specification {
}

class NodeDataFetcher implements DataFetcher {
DataLoader loader
String name

NodeDataFetcher(DataLoader loader) {
this.loader = loader
NodeDataFetcher(String name) {
this.name = name
}

@Override
Object get(DataFetchingEnvironment environment) throws Exception {
return loader.load(environment.getSource())
return environment.getDataLoader(name).load(environment.getSource())
}
}

def "levels of loading"() {

List<List<Node>> nodeLoads = []

DataLoader<Node, List<Node>> loader = new DataLoader<>({ keys ->

def batchLoadFunction = { keys ->
nodeLoads.add(keys)
List<List<Node>> childNodes = new ArrayList<>()
for (Node key : keys) {
childNodes.add(key.childNodes)
}
System.out.println("BatchLoader called for " + keys + " -> got " + childNodes)
return CompletableFuture.completedFuture(childNodes)
})

DataFetcher<?> nodeDataFetcher = new NodeDataFetcher(loader)
}
DataLoader<Node, List<Node>> loader = DataLoaderFactory.newDataLoader(batchLoadFunction)

def nodeTypeName = "Node"
def childNodesFieldName = "childNodes"
def queryTypeName = "Query"
def rootFieldName = "root"

DataFetcher<?> nodeDataFetcher = new NodeDataFetcher(childNodesFieldName)
DataLoaderRegistry registry = new DataLoaderRegistry().register(childNodesFieldName, loader)

GraphQLObjectType nodeType = GraphQLObjectType
.newObject()
.name(nodeTypeName)
Expand Down Expand Up @@ -132,8 +136,6 @@ class DataLoaderNodeTest extends Specification {
.build())
.build()

DataLoaderRegistry registry = new DataLoaderRegistry().register(childNodesFieldName, loader)

ExecutionResult result = GraphQL.newGraphQL(schema)
// .instrumentation(new DataLoaderDispatcherInstrumentation())
.build()
Expand Down
This file contains hidden or 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
Expand Up @@ -9,6 +9,7 @@ import graphql.schema.idl.SchemaGenerator
import graphql.schema.idl.SchemaParser
import org.dataloader.BatchLoader
import org.dataloader.DataLoader
import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification

Expand Down Expand Up @@ -36,7 +37,7 @@ class DataLoaderTypeMismatchTest extends Specification {

def typeDefinitionRegistry = new SchemaParser().parse(sdl)

def dataLoader = new DataLoader<Object, Object>(new BatchLoader<Object, Object>() {
def dataLoader = DataLoaderFactory.newDataLoader(new BatchLoader<Object, Object>() {
@Override
CompletionStage<List<Object>> load(List<Object> keys) {
return CompletableFuture.completedFuture([
Expand All @@ -50,7 +51,7 @@ class DataLoaderTypeMismatchTest extends Specification {
def todosDef = new DataFetcher<CompletableFuture<Object>>() {
@Override
CompletableFuture<Object> get(DataFetchingEnvironment environment) {
return dataLoader.load(environment)
return environment.getDataLoader("getTodos").load(environment)
}
}

Expand Down
This file contains hidden or 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
Expand Up @@ -8,6 +8,7 @@ import graphql.schema.StaticDataFetcher
import graphql.schema.idl.RuntimeWiring
import org.dataloader.BatchLoader
import org.dataloader.DataLoader
import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification

Expand Down Expand Up @@ -40,15 +41,15 @@ class Issue1178DataLoaderDispatchTest extends Specification {

def executor = Executors.newFixedThreadPool(5)

def dataLoader = new DataLoader<Object, Object>(new BatchLoader<Object, Object>() {
def dataLoader = DataLoaderFactory.newDataLoader(new BatchLoader<Object, Object>() {
@Override
CompletionStage<List<Object>> load(List<Object> keys) {
return CompletableFuture.supplyAsync({
return keys.collect({ [id: 'r' + it] })
}, executor)
}
})
def dataLoader2 = new DataLoader<Object, Object>(new BatchLoader<Object, Object>() {
def dataLoader2 = DataLoaderFactory.newDataLoader(new BatchLoader<Object, Object>() {
@Override
CompletionStage<List<Object>> load(List<Object> keys) {
return CompletableFuture.supplyAsync({
Expand All @@ -61,8 +62,8 @@ class Issue1178DataLoaderDispatchTest extends Specification {
dataLoaderRegistry.register("todo.related", dataLoader)
dataLoaderRegistry.register("todo.related2", dataLoader2)

def relatedDf = new MyDataFetcher(dataLoader)
def relatedDf2 = new MyDataFetcher(dataLoader2)
def relatedDf = new MyDataFetcher("todo.related")
def relatedDf2 = new MyDataFetcher("todo.related2")

def wiring = RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
Expand Down Expand Up @@ -119,16 +120,16 @@ class Issue1178DataLoaderDispatchTest extends Specification {

static class MyDataFetcher implements DataFetcher<CompletableFuture<Object>> {

private final DataLoader dataLoader
private final String name

MyDataFetcher(DataLoader dataLoader) {
this.dataLoader = dataLoader
MyDataFetcher(String name) {
this.name = name
}

@Override
CompletableFuture<Object> get(DataFetchingEnvironment environment) {
def todo = environment.source as Map
return dataLoader.load(todo['id'])
return environment.getDataLoader(name).load(todo['id'])
}
}
}
This file contains hidden or 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
Expand Up @@ -12,6 +12,7 @@ import graphql.schema.DataFetchingEnvironment
import graphql.schema.idl.RuntimeWiring
import org.dataloader.BatchLoader
import org.dataloader.DataLoader
import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification

Expand Down Expand Up @@ -168,8 +169,8 @@ class PeopleCompaniesAndProductsDataLoaderTest extends Specification {


private DataLoaderRegistry buildRegistry() {
DataLoader<Integer, Person> personDataLoader = new DataLoader<>(personBatchLoader)
DataLoader<Integer, Company> companyDataLoader = new DataLoader<>(companyBatchLoader)
DataLoader<Integer, Person> personDataLoader = DataLoaderFactory.newDataLoader(personBatchLoader)
DataLoader<Integer, Company> companyDataLoader = DataLoaderFactory.newDataLoader(companyBatchLoader)

DataLoaderRegistry registry = new DataLoaderRegistry()
registry.register("person", personDataLoader)
Expand Down
This file contains hidden or 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
Expand Up @@ -10,6 +10,7 @@ import graphql.schema.idl.MapEnumValuesProvider
import graphql.schema.idl.RuntimeWiring
import org.dataloader.BatchLoader
import org.dataloader.DataLoader
import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry

import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -60,7 +61,7 @@ class StarWarsDataLoaderWiring {

def newDataLoaderRegistry() {
// a data loader for characters that points to the character batch loader
def characterDataLoader = new DataLoader<String, Object>(characterBatchLoader)
def characterDataLoader = DataLoaderFactory.newDataLoader(characterBatchLoader)
new DataLoaderRegistry().register("character", characterDataLoader)
}

Expand Down
This file contains hidden or 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
Expand Up @@ -73,7 +73,7 @@ class DataFetchingEnvironmentImplTest extends Specification {
dfe.getVariables() == variables
dfe.getOperationDefinition() == operationDefinition
dfe.getExecutionId() == executionId
dfe.getDataLoader("dataLoader") == dataLoader
dfe.getDataLoader("dataLoader") != null
}

def "create environment from existing one will copy everything to new instance"() {
Expand Down Expand Up @@ -118,7 +118,7 @@ class DataFetchingEnvironmentImplTest extends Specification {
dfe.getDocument() == dfeCopy.getDocument()
dfe.getOperationDefinition() == dfeCopy.getOperationDefinition()
dfe.getVariables() == dfeCopy.getVariables()
dfe.getDataLoader("dataLoader") == dataLoader
dfe.getDataLoader("dataLoader") != null
dfe.getLocale() == dfeCopy.getLocale()
dfe.getLocalContext() == dfeCopy.getLocalContext()
}
Expand Down
This file contains hidden or 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
Expand Up @@ -171,7 +171,7 @@ public CompletableFuture<Void> clear() {
}
};

DataLoaderOptions options = DataLoaderOptions.newOptions().setValueCache(crossRequestValueCache);
DataLoaderOptions options = DataLoaderOptions.newOptions().setValueCache(crossRequestValueCache).build();

DataLoader<String, Object> dataLoader = DataLoaderFactory.newDataLoader(batchLoader, options);
}
Expand Down Expand Up @@ -260,7 +260,7 @@ public Object getContext() {
//
// this creates an overall context for the dataloader
//
DataLoaderOptions loaderOptions = DataLoaderOptions.newOptions().setBatchLoaderContextProvider(contextProvider);
DataLoaderOptions loaderOptions = DataLoaderOptions.newOptions().setBatchLoaderContextProvider(contextProvider).build();
DataLoader<String, Object> characterDataLoader = DataLoaderFactory.newDataLoader(batchLoaderWithCtx, loaderOptions);

// .... later in your data fetcher
Expand Down

Back | FazBrowse Home | New Git URL