This repository was archived by the owner on Nov 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
User-defined directives / decorators #123
Draft
ianhoffman
wants to merge
12
commits into
slackhq:main
Choose a base branch
from
ianhoffman:ih_profile_directives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
63d3086
wip
ianhoffman 28c0633
get something working
ianhoffman f42c11f
wip
ianhoffman aec6564
wip
ianhoffman 750097b
wip
ianhoffman bc52cda
wip
ianhoffman 355b141
require directives to be registered; do not use HHAST
ianhoffman 4077068
object directives
ianhoffman ec5ff84
Merge remote-tracking branch 'upstream/main' into ih_profile_directives
ianhoffman d26ec52
remove unused function
ianhoffman 334a891
simplify this PR a bit
ianhoffman 53746a7
directives for root fields
ianhoffman 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
|
|
||
|
|
||
|
|
||
| namespace Slack\GraphQL\Codegen; | ||
|
|
||
| use namespace HH\Lib\Str; | ||
| use type Facebook\HackCodegen\HackBuilder; | ||
|
|
||
| trait DirectivesBuilder { | ||
| protected dict<string, vec<string>> $directives; | ||
|
|
||
| protected function buildDirectives(HackBuilder $hb): HackBuilder { | ||
| if ($this->directives) { | ||
| $hb->addLine('vec[') | ||
| ->indent(); | ||
| foreach ($this->directives as $directive => $arguments) { | ||
| $hb->addLinef('new \%s(%s),', $directive, Str\join($arguments, ', ')); | ||
| } | ||
| $hb->unindent()->add(']'); | ||
| } else { | ||
| $hb->add('vec[]'); | ||
| } | ||
| return $hb; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
|
|
||
|
|
||
|
|
||
| namespace Slack\GraphQL\Codegen; | ||
|
|
||
| use namespace HH\Lib\{C, Dict, Str, Vec}; | ||
|
|
||
| final class DirectivesFinder { | ||
| public function __construct( | ||
| private shape( | ||
| ?'fields' => keyset<classname<\Slack\GraphQL\FieldDirective>>, | ||
| ?'objects' => keyset<classname<\Slack\GraphQL\ObjectDirective>>, | ||
| ) $directives, | ||
| private dict<string, string> $hack_class_to_graphql_interface, | ||
| ) {} | ||
|
|
||
| public function findDirectivesForField(\ReflectionMethod $rm): dict<string, vec<string>> { | ||
| return self::findDirectives( | ||
| $this->directives['fields'] ?? keyset[], | ||
| $directive_type ==> $rm->getAttributeClass($directive_type), | ||
| ); | ||
| } | ||
|
|
||
| public function findDirectivesForObject(\ReflectionClass $rc): dict<string, vec<string>> { | ||
| return vec[$rc->getName()] | ||
| |> Vec\concat( | ||
| $$, | ||
| get_interfaces($rc->getName(), $this->hack_class_to_graphql_interface) | ||
| |> Vec\keys($$), | ||
| ) | ||
| |> Vec\reverse($$) | ||
| |> Vec\map($$, $object_name ==> $this->findDirectivesForObjectName($object_name)) | ||
| |> Dict\merge(C\firstx($$), ...Vec\drop($$, 1)); | ||
| } | ||
|
|
||
| <<__Memoize>> | ||
| private function findDirectivesForObjectName(string $object_name): dict<string, vec<string>> { | ||
| $rc = new \ReflectionClass($object_name); | ||
| return self::findDirectives( | ||
| $this->directives['objects'] ?? keyset[], | ||
| $directive_type ==> $rc->getAttributeClass($directive_type), | ||
| ); | ||
| } | ||
|
|
||
| private static function findDirectives<T as \Slack\GraphQL\Directive>( | ||
| keyset<classname<T>> $custom_directive_types, | ||
| (function(classname<T>): ?T) $getter, | ||
| ): dict<string, vec<string>> { | ||
| $directives = dict[]; | ||
|
|
||
| foreach ($custom_directive_types as $directive_type) { | ||
| $directive = $getter($directive_type); | ||
| if ($directive is nonnull) { | ||
| $rc = new \ReflectionClass($directive); | ||
| $directives[$rc->getName()] = $directive->formatArgs(); | ||
| } | ||
| } | ||
|
|
||
| return $directives; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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's easy to miss, but this is what the generated code will look like when we add it to each field: https://github.com/mwildehahn/hack-graphql/pull/123/files#diff-bc6416c0810e5e09cdcf817fbe7fc23de76a1884523575597eb7a6b4bc6b9861R41-R51
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.
I recommend always generating some of the code as part of the PR so that people can see and review the generated code as if it were human-written. Generally speaking it's best to hold generated code to the same high standards as human-written code.
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.
Totally. Do you mean that I should include generated code beyond the code this PR includes currently?
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.
No, I meant that I could not identify by looking at your link that it was a link to lower in this PR. :)
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.
Gotcha. I'd recommend reviewing this whole file: https://github.com/mwildehahn/hack-graphql/pull/123/files#diff-bc6416c0810e5e09cdcf817fbe7fc23de76a1884523575597eb7a6b4bc6b9861
It contains generated code for object and field-level directives.