- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.1k
 
Add option printDirective to printSchema #3362
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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      028edb0
              
                Add option printDirective to printSchema
              
              
                spawnia c39eb2d
              
                Refocus on AST agnostic implementation
              
              
                spawnia da7f51a
              
                Merge branch 'main' into print-schema-directives
              
              
                spawnia 2d1c736
              
                Remove any ties to source directives
              
              
                spawnia 60bce7c
              
                Cleanup
              
              
                spawnia 1ef6e19
              
                Revert ast
              
              
                spawnia 23d557f
              
                Merge branch 'main' into print-schema-directives
              
              
                spawnia 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -8,44 +8,73 @@ import { isPrintableAsBlockString } from '../language/blockString'; | |
| 
     | 
||
| import type { GraphQLSchema } from '../type/schema'; | ||
| import type { GraphQLDirective } from '../type/directives'; | ||
| import { | ||
| DEFAULT_DEPRECATION_REASON, | ||
| isSpecifiedDirective, | ||
| } from '../type/directives'; | ||
| import type { | ||
| GraphQLNamedType, | ||
| GraphQLArgument, | ||
| GraphQLInputField, | ||
| GraphQLScalarType, | ||
| GraphQLEnumType, | ||
| GraphQLObjectType, | ||
| GraphQLEnumValue, | ||
| GraphQLField, | ||
| GraphQLInputField, | ||
| GraphQLInputObjectType, | ||
| GraphQLInterfaceType, | ||
| GraphQLNamedType, | ||
| GraphQLObjectType, | ||
| GraphQLScalarType, | ||
| GraphQLType, | ||
| GraphQLUnionType, | ||
| GraphQLInputObjectType, | ||
| } from '../type/definition'; | ||
| import { isIntrospectionType } from '../type/introspection'; | ||
| import { isSpecifiedScalarType } from '../type/scalars'; | ||
| import { | ||
| DEFAULT_DEPRECATION_REASON, | ||
| isSpecifiedDirective, | ||
| } from '../type/directives'; | ||
| import { | ||
| isScalarType, | ||
| isObjectType, | ||
| isInterfaceType, | ||
| isUnionType, | ||
| isEnumType, | ||
| isInputObjectType, | ||
| isInterfaceType, | ||
| isObjectType, | ||
| isScalarType, | ||
| isUnionType, | ||
| } from '../type/definition'; | ||
| import { isIntrospectionType } from '../type/introspection'; | ||
| import { isSpecifiedScalarType } from '../type/scalars'; | ||
| 
     | 
||
| import type { ConstDirectiveNode } from '../language/ast'; | ||
| 
     | 
||
| import { astFromValue } from './astFromValue'; | ||
| 
     | 
||
| export function printSchema(schema: GraphQLSchema): string { | ||
| export interface PrintSchemaOptions { | ||
| printDirectives?: ( | ||
| definition: | ||
| | GraphQLSchema | ||
| | GraphQLType | ||
| | GraphQLField<unknown, unknown> | ||
| | GraphQLEnumValue | ||
| | GraphQLInputField | ||
| | GraphQLArgument, | ||
| ) => ReadonlyArray<ConstDirectiveNode>; | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there perhaps a better data structure to return? I don't want to go with   | 
||
| } | ||
| 
     | 
||
| export function printSchema( | ||
| schema: GraphQLSchema, | ||
| options?: PrintSchemaOptions, | ||
| ): string { | ||
| return printFilteredSchema( | ||
| schema, | ||
| (n) => !isSpecifiedDirective(n), | ||
| isDefinedType, | ||
| options, | ||
| ); | ||
| } | ||
| 
     | 
||
| export function printIntrospectionSchema(schema: GraphQLSchema): string { | ||
| return printFilteredSchema(schema, isSpecifiedDirective, isIntrospectionType); | ||
| export function printIntrospectionSchema( | ||
| schema: GraphQLSchema, | ||
| options?: PrintSchemaOptions, | ||
| ): string { | ||
| return printFilteredSchema( | ||
| schema, | ||
| isSpecifiedDirective, | ||
| isIntrospectionType, | ||
| options, | ||
| ); | ||
| } | ||
| 
     | 
||
| function isDefinedType(type: GraphQLNamedType): boolean { | ||
| 
        
          
        
         | 
    @@ -56,21 +85,48 @@ function printFilteredSchema( | |
| schema: GraphQLSchema, | ||
| directiveFilter: (type: GraphQLDirective) => boolean, | ||
| typeFilter: (type: GraphQLNamedType) => boolean, | ||
| options?: PrintSchemaOptions, | ||
| ): string { | ||
| const directives = schema.getDirectives().filter(directiveFilter); | ||
| const types = Object.values(schema.getTypeMap()).filter(typeFilter); | ||
| 
     | 
||
| return [ | ||
| printSchemaDefinition(schema), | ||
| printSchemaDefinition(schema, options), | ||
| ...directives.map((directive) => printDirective(directive)), | ||
| ...types.map((type) => printType(type)), | ||
| ...types.map((type) => printType(type, options)), | ||
| ] | ||
| .filter(Boolean) | ||
| .join('\n\n'); | ||
| } | ||
| 
     | 
||
| function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> { | ||
| if (schema.description == null && isSchemaOfCommonNames(schema)) { | ||
| function collectDirectives( | ||
| printDirectives: PrintSchemaOptions['printDirectives'] | undefined, | ||
| definition: | ||
| | GraphQLSchema | ||
| | GraphQLType | ||
| | GraphQLField<unknown, unknown> | ||
| | GraphQLEnumValue | ||
| | GraphQLInputField | ||
| | GraphQLArgument, | ||
| ) { | ||
| if (printDirectives == null) { | ||
| return []; | ||
| } | ||
| 
     | 
||
| return printDirectives(definition).map(print); | ||
| } | ||
| 
     | 
||
| function printSchemaDefinition( | ||
| schema: GraphQLSchema, | ||
| options?: PrintSchemaOptions, | ||
| ): Maybe<string> { | ||
| const directives = collectDirectives(options?.printDirectives, schema); | ||
| 
     | 
||
| if ( | ||
| schema.description == null && | ||
| directives.length === 0 && | ||
| isSchemaOfCommonNames(schema) | ||
| ) { | ||
| return; | ||
| } | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -91,7 +147,12 @@ function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> { | |
| operationTypes.push(` subscription: ${subscriptionType.name}`); | ||
| } | ||
| 
     | 
||
| return printDescription(schema) + `schema {\n${operationTypes.join('\n')}\n}`; | ||
| return ( | ||
| printDescription(schema) + | ||
| ['schema', directives.join(' '), `{\n${operationTypes.join('\n')}\n}`] | ||
| .filter(Boolean) | ||
| .join(' ') | ||
| ); | ||
| } | ||
| 
     | 
||
| /** | ||
| 
          
            
          
           | 
    @@ -128,12 +189,15 @@ function isSchemaOfCommonNames(schema: GraphQLSchema): boolean { | |
| return true; | ||
| } | ||
| 
     | 
||
| export function printType(type: GraphQLNamedType): string { | ||
| export function printType( | ||
| type: GraphQLNamedType, | ||
| options?: PrintSchemaOptions, | ||
| ): string { | ||
| if (isScalarType(type)) { | ||
| return printScalar(type); | ||
| } | ||
| if (isObjectType(type)) { | ||
| return printObject(type); | ||
| return printObject(type, options); | ||
| } | ||
| if (isInterfaceType(type)) { | ||
| return printInterface(type); | ||
| 
          
            
          
           | 
    @@ -167,10 +231,15 @@ function printImplementedInterfaces( | |
| : ''; | ||
| } | ||
| 
     | 
||
| function printObject(type: GraphQLObjectType): string { | ||
| function printObject( | ||
| type: GraphQLObjectType, | ||
| options?: PrintSchemaOptions, | ||
| ): string { | ||
| const directives = collectDirectives(options?.printDirectives, type); | ||
| return ( | ||
| printDescription(type) + | ||
| `type ${type.name}` + | ||
| (directives.length ? ' ' + directives.join(' ') : '') + | ||
| printImplementedInterfaces(type) + | ||
| printFields(type) | ||
| ); | ||
| 
          
            
          
           | 
    ||
  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.
Perhaps splitting this into separate monomorphic functions would make it easier for the end user and avoid them having to do type checks: