Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #904 from jord1e/automatic-dataloader-naming
Automatic data loader naming on classes annotated with nameless @DgsDataLoader
- Loading branch information
Showing
8 changed files
with
292 additions
and
45 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
22 changes: 22 additions & 0 deletions
22
...rc/main/kotlin/com/netflix/graphql/dgs/exceptions/DgsUnnamedDataLoaderOnFieldException.kt
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,22 @@ | ||
/* | ||
* Copyright 2021 Netflix, Inc. | ||
* | ||
* 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 com.netflix.graphql.dgs.exceptions | ||
|
||
import java.lang.reflect.Field | ||
|
||
class DgsUnnamedDataLoaderOnFieldException(field: Field) : | ||
RuntimeException("Field `${field.name}` in class `${field.declaringClass.name}` was annotated with @DgsDataLoader, but the data loader was not given a proper name") |
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
35 changes: 35 additions & 0 deletions
35
graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/utils/DataLoaderNameUtil.kt
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,35 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* | ||
* 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 com.netflix.graphql.dgs.internal.utils | ||
|
||
import com.netflix.graphql.dgs.DgsDataLoader | ||
import com.netflix.graphql.dgs.Internal | ||
|
||
@Internal | ||
object DataLoaderNameUtil { | ||
|
||
/** | ||
* When the [annotation]'s [DgsDataLoader.name] is equal to [DgsDataLoader.GENERATE_DATA_LOADER_NAME], | ||
* the [clazz]'s [Class.getSimpleName] will be used. | ||
* In all other cases the [DgsDataLoader.name] method will be called on [annotation]. | ||
* | ||
* This method does not verify that [annotation] belongs to [clazz] for performance reasons. | ||
*/ | ||
fun getDataLoaderName(clazz: Class<*>, annotation: DgsDataLoader): String { | ||
return if (annotation.name == DgsDataLoader.GENERATE_DATA_LOADER_NAME) clazz.simpleName else annotation.name | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...hql-dgs/src/test/java/com/netflix/graphql/dgs/ExampleBatchLoaderWithoutNameFromField.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,43 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* | ||
* 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 com.netflix.graphql.dgs; | ||
|
||
import org.dataloader.BatchLoader; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@DgsComponent | ||
public class ExampleBatchLoaderWithoutNameFromField { | ||
@DgsDataLoader | ||
public BatchLoader<String, String> batchLoader = keys -> CompletableFuture.supplyAsync(() -> { | ||
List<String> values = new ArrayList<>(); | ||
values.add("a"); | ||
values.add("b"); | ||
values.add("c"); | ||
return values; | ||
}); | ||
|
||
@DgsDataLoader | ||
BatchLoader<String, String> privateBatchLoader = keys -> CompletableFuture.supplyAsync(() -> { | ||
List<String> values = new ArrayList<>(); | ||
values.add("a"); | ||
values.add("b"); | ||
values.add("c"); | ||
return values; | ||
}); | ||
} |
Oops, something went wrong.