-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add option printDirective to printSchema #3362
Closed
+203
−37
Closed
Changes from 2 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 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 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 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 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 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 |
---|---|---|
|
@@ -7,47 +7,97 @@ import { print } from '../language/printer'; | |
import { printBlockString } from '../language/blockString'; | ||
|
||
import type { GraphQLSchema } from '../type/schema'; | ||
import { isSchema } 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 { isConstDirectiveNode } 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, | ||
); | ||
} | ||
|
||
/** | ||
* Useful implementation of PrintSchemaOptions.printDirectives for users who | ||
* define their schema through SDL and simply want to print all included directives. | ||
*/ | ||
export const directivesFromAstNodes: PrintSchemaOptions['printDirectives'] = ( | ||
spawnia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
definition, | ||
) => { | ||
if (isSchema(definition)) { | ||
return [ | ||
...(definition.astNode?.directives ?? []), | ||
...(definition?.extensionASTNodes | ||
?.map((node) => node.directives) | ||
?.flat() ?? []), | ||
].filter(isConstDirectiveNode); | ||
} | ||
|
||
return []; | ||
}; | ||
|
||
function isDefinedType(type: GraphQLNamedType): boolean { | ||
return !isSpecifiedScalarType(type) && !isIntrospectionType(type); | ||
} | ||
|
@@ -56,21 +106,37 @@ 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)), | ||
] | ||
.filter(Boolean) | ||
.join('\n\n'); | ||
} | ||
|
||
function printSchemaDefinition(schema: GraphQLSchema): Maybe<string> { | ||
if (schema.description == null && isSchemaOfCommonNames(schema)) { | ||
function printSchemaDefinition( | ||
schema: GraphQLSchema, | ||
options?: PrintSchemaOptions, | ||
): Maybe<string> { | ||
const directives: Array<string> = []; | ||
const printDirectives = options?.printDirectives; | ||
if (printDirectives) { | ||
printDirectives(schema).forEach((directive) => { | ||
directives.push(print(directive)); | ||
}); | ||
} | ||
|
||
if ( | ||
schema.description == null && | ||
directives.length === 0 && | ||
isSchemaOfCommonNames(schema) | ||
) { | ||
return; | ||
} | ||
|
||
|
@@ -91,7 +157,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(' ') | ||
); | ||
} | ||
|
||
/** | ||
|
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: