- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 570
Cache result of validation #1730
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
          
     Open
      
      
            shmax
  wants to merge
  41
  commits into
  webonyx:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
shmax:validation-cache
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
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.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            41 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      35ce576
              
                cache result of validation
              
              
                shmax 03db33d
              
                remove ttl
              
              
                shmax c3a7eb4
              
                stanning
              
              
                shmax a2c38f4
              
                stanning
              
              
                shmax 6782bad
              
                downgrade to satisfy 7.4
              
              
                shmax 6317931
              
                use custom interface
              
              
                shmax 73c9774
              
                files
              
              
                shmax 6d7ace0
              
                cleanup
              
              
                shmax 708c85b
              
                cleanup
              
              
                shmax f35cb3c
              
                remove trailing comma
              
              
                shmax d71e123
              
                fix tests
              
              
                shmax cf20ef3
              
                formatting
              
              
                shmax 60bffe6
              
                move to file
              
              
                shmax 40c000b
              
                use shorthand, formatting
              
              
                shmax 7d515fd
              
                assert both results
              
              
                shmax c2a82a4
              
                ignore specific error
              
              
                shmax 51d1087
              
                documentation
              
              
                shmax a745345
              
                Autofix
              
              
                autofix-ci[bot] 1277bb3
              
                use serialize
              
              
                shmax 59a0cd7
              
                simplify conditional
              
              
                spawnia 6679cb8
              
                unify formatting
              
              
                spawnia 8d21f38
              
                Merge branch 'master' into validation-cache
              
              
                spawnia de245ff
              
                Polish tests and docs
              
              
                spawnia d166e41
              
                Clean up types
              
              
                spawnia 4b3aafe
              
                include rules in the fun
              
              
                shmax 9683c02
              
                Autofix
              
              
                autofix-ci[bot] 02013c3
              
                update comments
              
              
                shmax 1e55f48
              
                Merge branch 'validation-cache' of github.com:shmax/graphql-php into …
              
              
                shmax 127b68f
              
                remove comment
              
              
                shmax b4865e9
              
                add comments about keys
              
              
                shmax 4ec12df
              
                pass rules to markValidated
              
              
                shmax 707f257
              
                add docs
              
              
                shmax 4c3d419
              
                formatting
              
              
                shmax b5fdd3e
              
                remove redundant
              
              
                shmax 5d9e714
              
                Autofix
              
              
                autofix-ci[bot] b8c62dc
              
                Improve docs
              
              
                spawnia 7a2b3dc
              
                Autofix
              
              
                autofix-ci[bot] e8fedcc
              
                make it more clear that samples are using hand-rolled classes
              
              
                shmax c91420c
              
                Merge branch 'validation-cache' of github.com:shmax/graphql-php into …
              
              
                shmax 22e15cc
              
                remove see reference
              
              
                shmax cbebfce
              
                Autofix
              
              
                autofix-ci[bot] 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
    
  
  
    
              
  
    
      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
    
  
  
    
              |         
                  spawnia marked this conversation as resolved.
              Show resolved
            Hide resolved | 
  
    
      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,46 @@ | ||
| <?php declare(strict_types=1); | ||
|  | ||
| namespace GraphQL\Validator; | ||
|  | ||
| use GraphQL\Language\AST\DocumentNode; | ||
| use GraphQL\Type\Schema; | ||
| use GraphQL\Validator\Rules\ValidationRule; | ||
|  | ||
| /** | ||
| * Implement this interface and pass an instance to GraphQL::executeQuery to enable caching of successful query validations. | ||
| * | ||
| * This can improve performance by skipping validation for known-good combinations of query, schema, and rules. | ||
| * You are responsible for defining how cache keys are computed. | ||
| * | ||
| * Some things to keep in mind when generating keys: | ||
| * - PHP's `serialize()` function is fast, but can't handle certain structures such as closures. | ||
| * - If your `$schema` includes closures or is too large or complex to serialize, | ||
| * consider using a build-time version number or environment-based fingerprint instead. | ||
| * - Keep in mind that there are internal `$rules` that are applied in addition to any you pass in, | ||
| * and it's possible these may shift or expand as the library evolves, | ||
| * so it might make sense to include the library version number in your keys. | ||
| */ | ||
| interface ValidationCache | ||
| { | ||
| /** | ||
| * Determine whether the given schema/AST/rules set has already been successfully validated. | ||
| * | ||
| * This method should return true if the query has previously passed validation for the provided schema. | ||
| * Only successful validations should be considered "cached" — failed validations are not cached. | ||
| * | ||
| * @param array<ValidationRule>|null $rules | ||
| * | ||
| * @return bool true if validation for the given schema + AST + rules is already known to be valid; false otherwise | ||
| */ | ||
| public function isValidated(Schema $schema, DocumentNode $ast, ?array $rules = null): bool; | ||
|  | ||
| /** | ||
| * @param array<ValidationRule>|null $rules | ||
| * | ||
| * Mark the given schema/AST/rules set as successfully validated. | ||
| * | ||
| * This is typically called after a query passes validation. | ||
| * You should store enough information to recognize this combination on future requests. | ||
| */ | ||
| public function markValidated(Schema $schema, DocumentNode $ast, ?array $rules = null): void; | ||
| } | 
  
    
      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,28 @@ | ||
| <?php declare(strict_types=1); | ||
|  | ||
| namespace GraphQL\Tests\Executor\TestClasses; | ||
|  | ||
| use GraphQL\Language\AST\DocumentNode; | ||
| use GraphQL\Tests\PsrValidationCacheAdapter; | ||
| use GraphQL\Type\Schema; | ||
|  | ||
| final class SpyValidationCacheAdapter extends PsrValidationCacheAdapter | ||
| { | ||
| public int $isValidatedCalls = 0; | ||
|  | ||
| public int $markValidatedCalls = 0; | ||
|  | ||
| public function isValidated(Schema $schema, DocumentNode $ast, ?array $rules = null): bool | ||
| { | ||
| ++$this->isValidatedCalls; | ||
|  | ||
| return parent::isValidated($schema, $ast); | ||
| } | ||
|  | ||
| public function markValidated(Schema $schema, DocumentNode $ast, ?array $rules = null): void | ||
| { | ||
| ++$this->markValidatedCalls; | ||
|  | ||
| parent::markValidated($schema, $ast); | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
      
      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.
Good catch on not overwriting
$rules👍