-
-
Notifications
You must be signed in to change notification settings - Fork 114
DataFetchResult generaton #1431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kobylynskyi
merged 20 commits into
kobylynskyi:main
from
kirill071992:data-fetch-result-generaton
Feb 24, 2024
Merged
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
67f1607
add new plugin param for DataFetchResult generation
kirill071992 3f76abe
add new plugin param for DataFetchResult generation
kirill071992 73f964b
fix mapping context
kirill071992 6b10129
add test schema
kirill071992 7472db7
update field mapper
kirill071992 6e21534
add tests
kirill071992 142db6f
add tests
kirill071992 3576f35
add fields assertion for generated test files
kirill071992 79b4fdc
Fix logic
kirill071992 2f9342f
Fix code style
kirill071992 b873e45
Fix code style
kirill071992 2c6c564
Fix code style
kirill071992 83d7c21
Fix code style
kirill071992 983c107
Fix description
kirill071992 592957f
Add code generation based on Type name or field name
kirill071992 cdb847c
Add tests
kirill071992 6f40cc5
Fix code style
kirill071992 5ed5834
Fix code style
kirill071992 82ace9e
Merge remote-tracking branch 'upstream/main' into data-fetch-result-g…
kobylynskyi 32ea7a4
Fix codegen-options.md formatting
kobylynskyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
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
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
71 changes: 71 additions & 0 deletions
71
src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenFieldsWithDataFetcherTest.java
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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.kobylynskyi.graphql.codegen; | ||
|
|
||
| import com.kobylynskyi.graphql.codegen.java.JavaGraphQLCodegen; | ||
| import com.kobylynskyi.graphql.codegen.model.MappingConfig; | ||
| import com.kobylynskyi.graphql.codegen.utils.Utils; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName; | ||
| import static java.util.Arrays.asList; | ||
| import static java.util.Collections.singleton; | ||
| import static java.util.Collections.singletonList; | ||
| import static java.util.stream.Collectors.toList; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class GraphQLCodegenFieldsWithDataFetcherTest { | ||
|
|
||
| private final File outputBuildDir = new File("build/generated"); | ||
| private final File outputJavaClassesDir = new File("build/generated/com/github/graphql"); | ||
| private final MappingConfig mappingConfig = new MappingConfig(); | ||
|
|
||
| @BeforeEach | ||
| void init() { | ||
| mappingConfig.setPackageName("com.github.graphql"); | ||
| mappingConfig.setFieldsWithDataFetcherResult(new HashSet<>(singleton("@dataFetcherResult"))); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void cleanup() { | ||
| Utils.deleteDir(outputBuildDir); | ||
| } | ||
|
|
||
| @Test | ||
| void generate_fieldsWithDataFetcherResult() throws Exception { | ||
| mappingConfig.getFieldsWithDataFetcherResult().add("orders"); | ||
| mappingConfig.getFieldsWithDataFetcherResult().add("cart"); | ||
|
|
||
| generate("src/test/resources/schemas/fields-with-data-fetcher-result.graphqls"); | ||
|
|
||
| File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); | ||
| Assertions.assertNotNull(files); | ||
|
|
||
| List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList()); | ||
| assertEquals( | ||
| asList("Cart.java", "Order.java", "QueryResolver.java", "User.java", "UserCurrentQueryResolver.java"), | ||
| generatedFileNames | ||
| ); | ||
|
|
||
| File user = getFileByName(files, "User.java"); | ||
| String userContext = Utils.getFileContent(user.getPath()).trim(); | ||
|
|
||
| assertTrue(userContext.contains("java.util.List<graphql.execution.DataFetcherResult<Order>>")); | ||
| assertTrue(userContext.contains("graphql.execution.DataFetcherResult<Cart>")); | ||
| } | ||
|
|
||
| private void generate(String o) throws IOException { | ||
| new JavaGraphQLCodegen(singletonList(o), outputBuildDir, mappingConfig, | ||
| TestUtils.getStaticGeneratedInfo(mappingConfig)) | ||
| .generate(); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/test/resources/schemas/fields-with-data-fetcher-result.graphqls
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # A GraphQL schema provides a root type for each kind of operation. | ||
| schema { | ||
| # The query root. | ||
| query: Query | ||
| } | ||
|
|
||
| type Query { | ||
| userCurrent: User | ||
| } | ||
|
|
||
| type User { | ||
| username: String! | ||
| email: String! | ||
| orders: [Order!]! @dataFetcherResult | ||
| cart: Cart! @dataFetcherResult | ||
| } | ||
|
|
||
| type Order { | ||
| number: String! | ||
| price: String! | ||
| } | ||
|
|
||
| type Cart { | ||
| id: Long! | ||
| } | ||
|
|
||
| directive @dataFetcherResult on FIELD_DEFINITION |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.