-
Notifications
You must be signed in to change notification settings - Fork 1
feat: support custom directives #134
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
32d0f6c
add unit test
danadajian c51350c
pass test but broke others
danadajian 1035d6c
make tests pass
danadajian 6622373
refactor
danadajian 142d164
lock
danadajian f57b917
add test case
danadajian 24a4c29
use customDirectives config
danadajian adecc23
Merge branch 'main' into support-custom-directives
danadajian acddaed
refactor and update test
danadajian 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright 2024 Expedia, 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 | ||
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. | ||
*/ | ||
|
||
import { DirectiveDefinitionNode } from "graphql"; | ||
import { CodegenConfigWithDefaults } from "../config/build-config-with-defaults"; | ||
import { titleCase } from "../utils/title-case"; | ||
|
||
export function buildDirectiveDefinition( | ||
node: DirectiveDefinitionNode, | ||
config: CodegenConfigWithDefaults, | ||
): string { | ||
const directiveName = node.name.value; | ||
const isCustomDirective = config.customDirectives?.includes(directiveName); | ||
if (!isCustomDirective) { | ||
return ""; | ||
} | ||
return `@GraphQLDirective( | ||
name = "${titleCase(directiveName)}", | ||
description = "${node.description?.value ?? ""}", | ||
locations = [${node.locations.map((location) => `graphql.introspection.Introspection.DirectiveLocation.${location.value}`).join(", ")}] | ||
) | ||
annotation class ${titleCase(directiveName)}`; | ||
} |
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
12 changes: 12 additions & 0 deletions
12
test/unit/should_generate_custom_directives/codegen.config.ts
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,12 @@ | ||
import { GraphQLKotlinCodegenConfig } from "../../../src/plugin"; | ||
|
||
export default { | ||
customDirectives: ["myCustomDirective", "myCustomDirective2"], | ||
extraImports: ["should_honor_directiveReplacements_config.*"], | ||
directiveReplacements: [ | ||
{ | ||
directive: "someDirective1", | ||
kotlinAnnotations: ["@SomeAnnotation1"], | ||
}, | ||
], | ||
} satisfies GraphQLKotlinCodegenConfig; |
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,44 @@ | ||
package com.kotlin.generated | ||
|
||
import com.expediagroup.graphql.generator.annotations.* | ||
import should_honor_directiveReplacements_config.* | ||
|
||
@GraphQLDirective( | ||
name = "MyCustomDirective", | ||
description = "A description for MyCustomDirective", | ||
locations = [graphql.introspection.Introspection.DirectiveLocation.OBJECT, graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION, graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT, graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION] | ||
) | ||
annotation class MyCustomDirective | ||
|
||
@GraphQLDirective( | ||
name = "MyCustomDirective2", | ||
description = "", | ||
locations = [graphql.introspection.Introspection.DirectiveLocation.OBJECT, graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION, graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT, graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION] | ||
) | ||
annotation class MyCustomDirective2 | ||
|
||
@MyCustomDirective | ||
@MyCustomDirective2 | ||
@SomeAnnotation1 | ||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT]) | ||
data class MyTypeWithCustomDirectiveOnObject( | ||
val field: String? = null | ||
) | ||
|
||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT]) | ||
data class MyTypeWithCustomDirectiveOnField( | ||
@MyCustomDirective | ||
val field: String? = null | ||
) | ||
|
||
@MyCustomDirective | ||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT]) | ||
data class MyInputWithCustomDirectiveOnObject( | ||
val field: String? = null | ||
) | ||
|
||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT]) | ||
data class MyInputWithCustomDirectiveOnField( | ||
@MyCustomDirective | ||
val field: String? = null | ||
) |
24 changes: 24 additions & 0 deletions
24
test/unit/should_generate_custom_directives/schema.graphql
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,24 @@ | ||
"A description for MyCustomDirective" | ||
directive @myCustomDirective on OBJECT | FIELD_DEFINITION | INPUT_OBJECT | INPUT_FIELD_DEFINITION | ||
directive @myCustomDirective2 on OBJECT | FIELD_DEFINITION | INPUT_OBJECT | INPUT_FIELD_DEFINITION | ||
|
||
directive @someDirective1 on OBJECT | ||
|
||
type MyTypeWithCustomDirectiveOnObject | ||
@myCustomDirective | ||
@myCustomDirective2 | ||
@someDirective1 { | ||
field: String | ||
} | ||
|
||
type MyTypeWithCustomDirectiveOnField { | ||
field: String @myCustomDirective | ||
} | ||
|
||
input MyInputWithCustomDirectiveOnObject @myCustomDirective { | ||
field: String | ||
} | ||
|
||
input MyInputWithCustomDirectiveOnField { | ||
field: String @myCustomDirective | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it doesn't really matter all that much, but why inline the imports?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just easier than manually adding the imports to the top of the file which would require adding them to the
extraImports
config