Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/extend/src/extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ test("using an existing object type", () => {
`);
});

test("errors when no type ", () => {
test("errors when no type exists with that name", () => {
expect(() => {
extend((base) => ({
query: {
Expand Down Expand Up @@ -525,7 +525,9 @@ test(".scalar works for custom scalars", () => {
},
}),
});
const extended = extend((base) => ({
const extended = extend<{
Something: typeof Something;
}>((base) => ({
query: {
hello: g.field({
type: base.scalar("Something"),
Expand Down
100 changes: 74 additions & 26 deletions packages/extend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) => {
Expand All @@ -113,76 +119,81 @@ export function extend(
typeof extension === "function"
? extension({
schema,
all(name) {
Copy link
Preview

Copilot AI Feb 27, 2025

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.

return getType(name as string) as any;
},
object(name) {
const graphQLType = getType(name);
const graphQLType = getType(name as string);
Copy link
Preview

Copilot AI Feb 27, 2025

Choose a reason for hiding this comment

The 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
const graphQLType = getType(name as string);
const graphQLType = getType(name);

Copilot uses AI. Check for mistakes.

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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down