-
Notifications
You must be signed in to change notification settings - Fork 1
Typed base schema meta #17
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
Draft
emmatown
wants to merge
1
commit into
main
Choose a base branch
from
typed-base-schema-meta
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -26,18 +26,24 @@ import { | |||||
parse, | ||||||
ObjectTypeDefinitionNode, | ||||||
print, | ||||||
GraphQLInterfaceType, | ||||||
GraphQLInputObjectType, | ||||||
GraphQLUnionType, | ||||||
GraphQLScalarType, | ||||||
} from "graphql"; | ||||||
|
||||||
import { | ||||||
GField, | ||||||
GObjectType, | ||||||
GArg, | ||||||
GEnumType, | ||||||
GUnionType, | ||||||
GInterfaceType, | ||||||
GScalarType, | ||||||
GOutputType, | ||||||
GInputObjectType, | ||||||
GInputType, | ||||||
GArg, | ||||||
GInterfaceField, | ||||||
} from "@graphql-ts/schema"; | ||||||
|
||||||
const builtinScalars = new Set(specifiedScalarTypes.map((x) => x.name)); | ||||||
|
@@ -91,11 +97,11 @@ const builtinScalars = new Set(specifiedScalarTypes.map((x) => x.name)); | |||||
* } | ||||||
* ``` | ||||||
*/ | ||||||
export function extend( | ||||||
export function extend<Types extends Record<string, NamedType>>( | ||||||
extension: | ||||||
| Extension | ||||||
| readonly Extension[] | ||||||
| ((base: BaseSchemaMeta) => Extension | readonly Extension[]) | ||||||
| ((base: BaseSchemaMeta<Types>) => Extension | readonly Extension[]) | ||||||
): (schema: GraphQLSchema) => GraphQLSchema { | ||||||
return (schema) => { | ||||||
const getType = (name: string) => { | ||||||
|
@@ -113,76 +119,81 @@ export function extend( | |||||
typeof extension === "function" | ||||||
? extension({ | ||||||
schema, | ||||||
all(name) { | ||||||
return getType(name as string) as any; | ||||||
}, | ||||||
object(name) { | ||||||
const graphQLType = getType(name); | ||||||
const graphQLType = getType(name as string); | ||||||
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. [nitpick] The repeated explicit cast 'name as string' might indicate that the 'getType' function's type signature could be refined to accept a more specific type, thereby reducing the need for such casts.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
if (!isObjectType(graphQLType)) { | ||||||
throw new Error( | ||||||
`There is a type named ${JSON.stringify( | ||||||
name | ||||||
)} in the schema being extended but it is not an object type` | ||||||
); | ||||||
} | ||||||
return graphQLType; | ||||||
return graphQLType as any; | ||||||
}, | ||||||
inputObject(name) { | ||||||
const graphQLType = getType(name); | ||||||
const graphQLType = getType(name as string); | ||||||
if (!isInputObjectType(graphQLType)) { | ||||||
throw new Error( | ||||||
`There is a type named ${JSON.stringify( | ||||||
name | ||||||
)} in the schema being extended but it is not an input object type` | ||||||
); | ||||||
} | ||||||
return graphQLType; | ||||||
return graphQLType as any; | ||||||
}, | ||||||
enum(name) { | ||||||
const graphQLType = getType(name); | ||||||
const graphQLType = getType(name as string); | ||||||
if (!isEnumType(graphQLType)) { | ||||||
throw new Error( | ||||||
`There is a type named ${JSON.stringify( | ||||||
name | ||||||
)} in the schema being extended but it is not an enum type` | ||||||
); | ||||||
} | ||||||
return graphQLType; | ||||||
return graphQLType as any; | ||||||
}, | ||||||
interface(name) { | ||||||
const graphQLType = getType(name); | ||||||
const graphQLType = getType(name as string); | ||||||
if (!isInterfaceType(graphQLType)) { | ||||||
throw new Error( | ||||||
`There is a type named ${JSON.stringify( | ||||||
name | ||||||
)} in the schema being extended but it is not an interface type` | ||||||
); | ||||||
} | ||||||
return graphQLType; | ||||||
return graphQLType as any; | ||||||
}, | ||||||
scalar(name) { | ||||||
if (builtinScalars.has(name)) { | ||||||
if (builtinScalars.has(name as string)) { | ||||||
throw new Error( | ||||||
`The names of built-in scalars cannot be passed to BaseSchemaInfo.scalar but ${name} was passed` | ||||||
`The names of built-in scalars cannot be passed to BaseSchemaInfo.scalar but ${ | ||||||
name as string | ||||||
} was passed` | ||||||
); | ||||||
} | ||||||
const graphQLType = getType(name); | ||||||
const graphQLType = getType(name as string); | ||||||
if (!isScalarType(graphQLType)) { | ||||||
throw new Error( | ||||||
`There is a type named ${JSON.stringify( | ||||||
name | ||||||
)} in the schema being extended but it is not a scalar type` | ||||||
); | ||||||
} | ||||||
return graphQLType; | ||||||
return graphQLType as any; | ||||||
}, | ||||||
union(name) { | ||||||
const graphQLType = getType(name); | ||||||
const graphQLType = getType(name as string); | ||||||
if (!isUnionType(graphQLType)) { | ||||||
throw new Error( | ||||||
`There is a type named ${JSON.stringify( | ||||||
name | ||||||
)} in the schema being extended but it is not a union type` | ||||||
); | ||||||
} | ||||||
return graphQLType; | ||||||
return graphQLType as any; | ||||||
}, | ||||||
}) | ||||||
: extension | ||||||
|
@@ -426,12 +437,39 @@ export type Extension = { | |||||
// unreferencedConcreteInterfaceImplementations?: ObjectType<Context, any>[]; | ||||||
}; | ||||||
|
||||||
export type NamedType = | ||||||
| GObjectType<any, unknown> | ||||||
| GInputObjectType<{ [key: string]: GArg<GInputType, boolean> }> | ||||||
| GEnumType<Record<string, unknown>> | ||||||
| GUnionType<unknown, unknown> | ||||||
| GInterfaceType< | ||||||
unknown, | ||||||
Record<string, GInterfaceField<any, GOutputType<unknown>, unknown>>, | ||||||
unknown | ||||||
> | ||||||
| GScalarType<unknown>; | ||||||
|
||||||
export type NamedTypes = Record<string & {}, NamedType>; | ||||||
|
||||||
type TypeOfKind<Types extends Record<string, NamedType>, Constraint> = { | ||||||
[K in keyof Types]: Extract<Types[K], Constraint> extends never ? never : K; | ||||||
}[keyof Types]; | ||||||
|
||||||
/** | ||||||
* This object contains the schema being extended and functions to get GraphQL | ||||||
* types from the schema. | ||||||
*/ | ||||||
export type BaseSchemaMeta = { | ||||||
export type BaseSchemaMeta< | ||||||
Types extends Record<string, NamedType> = NamedTypes, | ||||||
> = { | ||||||
schema: GraphQLSchema; | ||||||
all< | ||||||
Name extends { | ||||||
[K in keyof Types]: string extends K ? never : K; | ||||||
}[keyof Types], | ||||||
>( | ||||||
name: Name | ||||||
): Types[Name]; | ||||||
/** | ||||||
* Gets an {@link GObjectType object type} from the existing GraphQL schema. If | ||||||
* there is no object type in the existing schema with the name passed, an | ||||||
|
@@ -452,7 +490,9 @@ export type BaseSchemaMeta = { | |||||
* }))(originalSchema); | ||||||
* ``` | ||||||
*/ | ||||||
object(name: string): GObjectType<unknown, unknown>; | ||||||
object<Name extends TypeOfKind<Types, GraphQLObjectType>>( | ||||||
name: Name | ||||||
): Extract<Types[Name], GraphQLObjectType>; | ||||||
/** | ||||||
* Gets an {@link GInputObjectType input object type} from the existing GraphQL | ||||||
* schema. If there is no input object type in the existing schema with the | ||||||
|
@@ -479,9 +519,9 @@ export type BaseSchemaMeta = { | |||||
* }))(originalSchema); | ||||||
* ``` | ||||||
*/ | ||||||
inputObject( | ||||||
name: string | ||||||
): GInputObjectType<{ [key: string]: GArg<any, boolean> }, boolean>; | ||||||
inputObject<Name extends TypeOfKind<Types, GraphQLInputObjectType>>( | ||||||
name: Name | ||||||
): Extract<Types[Name], GraphQLInputObjectType>; | ||||||
/** | ||||||
* Gets an {@link GEnumType enum type} from the existing GraphQL schema. If | ||||||
* there is no enum type in the existing schema with the name passed, an error | ||||||
|
@@ -507,7 +547,9 @@ export type BaseSchemaMeta = { | |||||
* }))(originalSchema); | ||||||
* ``` | ||||||
*/ | ||||||
enum(name: string): GEnumType<Record<string, unknown>>; | ||||||
enum<Name extends TypeOfKind<Types, GEnumType<any>>>( | ||||||
name: Name | ||||||
): Extract<Types[Name], GEnumType<any>>; | ||||||
/** | ||||||
* Gets a {@link GUnionType union type} from the existing GraphQL schema. If | ||||||
* there is no union type in the existing schema with the name passed, an | ||||||
|
@@ -528,7 +570,9 @@ export type BaseSchemaMeta = { | |||||
* }))(originalSchema); | ||||||
* ``` | ||||||
*/ | ||||||
union(name: string): GUnionType<unknown, unknown>; | ||||||
union<Name extends TypeOfKind<Types, GraphQLUnionType>>( | ||||||
name: Name | ||||||
): Extract<Types[Name], GraphQLUnionType>; | ||||||
/** | ||||||
* Gets an {@link GInterfaceType interface type} from the existing GraphQL | ||||||
* schema. If there is no interface type in the existing schema with the name | ||||||
|
@@ -549,7 +593,9 @@ export type BaseSchemaMeta = { | |||||
* }))(originalSchema); | ||||||
* ``` | ||||||
*/ | ||||||
interface(name: string): GInterfaceType<unknown, any, unknown>; | ||||||
interface<Name extends TypeOfKind<Types, GraphQLInterfaceType>>( | ||||||
name: Name | ||||||
): Extract<Types[Name], GraphQLInterfaceType>; | ||||||
/** | ||||||
* Gets a {@link GScalarType scalar type} from the existing GraphQL schema. If | ||||||
* there is no scalar type in the existing schema with the name passed, an | ||||||
|
@@ -578,7 +624,9 @@ export type BaseSchemaMeta = { | |||||
* }))(originalSchema); | ||||||
* ``` | ||||||
*/ | ||||||
scalar(name: string): GScalarType<unknown>; | ||||||
scalar<Name extends TypeOfKind<Types, GraphQLScalarType<any>>>( | ||||||
name: Name | ||||||
): Extract<Types[Name], GraphQLScalarType<any>>; | ||||||
}; | ||||||
|
||||||
function findObjectTypeUsages( | ||||||
|
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.
The 'all' method lacks an else branch to handle the case where 'getType(name as string)' does not match any of the known GraphQL type checks. Consider adding an error throw for unsupported types.
Copilot uses AI. Check for mistakes.