From 7425f013822d5b302f8398a7b23008ae9f387df3 Mon Sep 17 00:00:00 2001 From: mitchellhamilton Date: Mon, 8 Feb 2021 10:03:39 +1000 Subject: [PATCH] Various fixes --- .changeset/cyan-spies-dress.md | 5 + .../schema/src/types/list-and-non-null.ts | 17 +- packages/schema/src/types/output.ts | 176 ++++++++++-------- .../types-that-do-not-use-context/enum.ts | 1 + .../types-that-do-not-use-context/input.ts | 10 +- .../types-that-do-not-use-context/scalars.ts | 2 + 6 files changed, 129 insertions(+), 82 deletions(-) create mode 100644 .changeset/cyan-spies-dress.md diff --git a/.changeset/cyan-spies-dress.md b/.changeset/cyan-spies-dress.md new file mode 100644 index 0000000..7affa1e --- /dev/null +++ b/.changeset/cyan-spies-dress.md @@ -0,0 +1,5 @@ +--- +"@ts-gql/schema": minor +--- + +Various fixes diff --git a/packages/schema/src/types/list-and-non-null.ts b/packages/schema/src/types/list-and-non-null.ts index f54a7ce..7667f32 100644 --- a/packages/schema/src/types/list-and-non-null.ts +++ b/packages/schema/src/types/list-and-non-null.ts @@ -6,19 +6,26 @@ import { ScalarType, } from "./types-that-do-not-use-context"; -export type ListType = { +export type ListType = { kind: "list"; of: Of; + __context: Context; graphQLType: GraphQLList; }; export function list(of: Of): ListType { - return { kind: "list", of, graphQLType: new GraphQLList(of.graphQLType) }; + return { + kind: "list", + of, + __context: of["__context"], + graphQLType: new GraphQLList(of.graphQLType), + }; } export type NonNullType = { kind: "non-null"; of: Of; + __context: Of["__context"]; graphQLType: GraphQLNonNull; }; @@ -28,6 +35,7 @@ export function nonNull( return { kind: "non-null", of, + __context: of["__context"], graphQLType: new GraphQLNonNull(of.graphQLType) as GraphQLNonNull< Of["graphQLType"] >, @@ -38,8 +46,8 @@ export type TypesExcludingNonNull = | ScalarType | ListType | InputObjectType - | ObjectType - | UnionType> + | ObjectType + | UnionType> | EnumType; export type Types = @@ -48,4 +56,5 @@ export type Types = kind: "non-null"; of: TypesExcludingNonNull; graphQLType: GraphQLNullableType; + __context: unknown; }; diff --git a/packages/schema/src/types/output.ts b/packages/schema/src/types/output.ts index f1f4ab6..1525662 100644 --- a/packages/schema/src/types/output.ts +++ b/packages/schema/src/types/output.ts @@ -33,8 +33,8 @@ type OutputNonNullType = { export type OutputTypeExcludingNonNull = | ScalarType - | ObjectType - | UnionType> + | ObjectType + | UnionType> | EnumType | OutputListType; @@ -48,9 +48,9 @@ type InferValueFromOutputTypeWithoutAddingNull< ? Values[string]["value"] : Type extends OutputListType ? InferValueFromOutputType[] - : Type extends ObjectType + : Type extends ObjectType ? RootVal - : Type extends UnionType> + : Type extends UnionType> ? RootVal : never; @@ -60,10 +60,11 @@ export type InferValueFromOutputType< ? InferValueFromOutputTypeWithoutAddingNull : InferValueFromOutputTypeWithoutAddingNull | null; -export type ObjectType = { +export type ObjectType = { kind: "object"; name: Name; graphQLType: GraphQLObjectType; + __context: Context; __rootVal: RootVal; }; @@ -72,11 +73,12 @@ type MaybePromise = Promise | T; export type OutputFieldResolver< Args extends Record>, OutputType extends OutputTypes, - RootVal + RootVal, + Context > = ( rootVal: RootVal, args: InferValueFromArgs, - context: unknown, + context: Context, info: GraphQLResolveInfo ) => MaybePromise>; @@ -86,13 +88,15 @@ export type OutputField< RootVal, Args extends Record>, OutputType extends OutputTypes, - Key extends string + Key extends string, + Context > = { args?: Args; type: OutputType; __key: Key; __rootVal: RootVal; - resolve?: OutputFieldResolver; + __context: Context; + resolve?: OutputFieldResolver; deprecationReason?: string; description?: string; extensions?: Readonly>; @@ -101,7 +105,8 @@ export function field< RootVal, Args extends { [Key in keyof Args]: Arg }, OutputType extends OutputTypes, - Key extends string + Key extends string, + Context >( field: { args?: Args; @@ -116,100 +121,116 @@ export function field< resolve?: OutputFieldResolver< SomeTypeThatIsntARecordOfArgs extends Args ? {} : Args, OutputType, - RootVal + RootVal, + Context >; } : { resolve: OutputFieldResolver< SomeTypeThatIsntARecordOfArgs extends Args ? {} : Args, OutputType, - RootVal + RootVal, + Context >; }) -): OutputField { +): OutputField { return field as any; } -export function object() { - return function objectInner< - Name extends string, - Fields extends { - [Key in keyof Fields]: OutputField< - RootVal, - any, - any, - Extract - >; - } - >(config: { - name: Name; - description?: string; - deprecationReason?: string; - fields: Fields | (() => Fields); - }): ObjectType { - return { - kind: "object", - name: config.name, - graphQLType: new GraphQLObjectType({ +export const object = bindObjectTypeToContext(); + +export function bindObjectTypeToContext() { + return function object< + RootVal + >(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: { + youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true; + }) { + return function objectInner< + Name extends string, + Fields extends { + [Key in keyof Fields]: OutputField< + RootVal, + any, + any, + Extract, + Context + >; + } + >(config: { + name: Name; + description?: string; + deprecationReason?: string; + fields: Fields | (() => Fields); + }): ObjectType { + return { + kind: "object", name: config.name, - description: config.description, - fields: () => { - const fields = - typeof config.fields === "function" - ? config.fields() - : config.fields; - return Object.fromEntries( - Object.entries( - fields as Record< - string, - OutputField< - any, - Record>, - OutputTypes, - string + graphQLType: new GraphQLObjectType({ + name: config.name, + description: config.description, + fields: () => { + const fields = + typeof config.fields === "function" + ? config.fields() + : config.fields; + return Object.fromEntries( + Object.entries( + fields as Record< + string, + OutputField< + any, + Record>, + OutputTypes, + string, + Context + > > - > - ).map(([key, val]) => [ - key, - { - type: val.type.graphQLType as GraphQLOutputType, - resolve: val.resolve, - deprecationReason: val.deprecationReason, - description: val.description, - args: Object.fromEntries( - Object.entries(val.args || {}).map(([key, val]) => [ - key, - { - type: val.type.graphQLType as GraphQLInputType, - description: val.description, - defaultValue: val.defaultValue, - }, - ]) - ), - extensions: val.extensions, - }, - ]) - ); - }, - }), - __rootVal: undefined as any, + ).map(([key, val]) => [ + key, + { + type: val.type.graphQLType as GraphQLOutputType, + resolve: val.resolve, + deprecationReason: val.deprecationReason, + description: val.description, + args: Object.fromEntries( + Object.entries(val.args || {}).map(([key, val]) => [ + key, + { + type: val.type.graphQLType as GraphQLInputType, + description: val.description, + defaultValue: val.defaultValue, + }, + ]) + ), + extensions: val.extensions, + }, + ]) + ); + }, + }), + __rootVal: undefined as any, + __context: undefined as any, + }; }; }; } -export type UnionType> = { +export type UnionType> = { kind: "union"; __rootVal: TObjectType["__rootVal"]; + __context: TObjectType["__context"]; graphQLType: GraphQLUnionType; }; -export function union>(config: { +export function union< + TObjectType extends ObjectType +>(config: { name: string; description?: string; types: TObjectType[]; resolveType: ( type: TObjectType["__rootVal"], - context: unknown, + context: TObjectType["__context"], info: GraphQLResolveInfo, abstractType: GraphQLUnionType ) => TObjectType["name"]; @@ -223,5 +244,6 @@ export function union>(config: { resolveType: config.resolveType as any, }), __rootVal: undefined as any, + __context: undefined as any, }; } diff --git a/packages/schema/src/types/types-that-do-not-use-context/enum.ts b/packages/schema/src/types/types-that-do-not-use-context/enum.ts index 0518bee..62b30f1 100644 --- a/packages/schema/src/types/types-that-do-not-use-context/enum.ts +++ b/packages/schema/src/types/types-that-do-not-use-context/enum.ts @@ -10,6 +10,7 @@ export type EnumType>> = { kind: "enum"; values: Values; graphQLType: GraphQLEnumType; + __context: unknown; }; export function enumValues( diff --git a/packages/schema/src/types/types-that-do-not-use-context/input.ts b/packages/schema/src/types/types-that-do-not-use-context/input.ts index 6263b95..c415188 100644 --- a/packages/schema/src/types/types-that-do-not-use-context/input.ts +++ b/packages/schema/src/types/types-that-do-not-use-context/input.ts @@ -6,12 +6,14 @@ type InputListType = { kind: "list"; of: Of; graphQLType: GraphQLList; + __context: unknown; }; type InputNonNullType = { kind: "non-null"; of: Of; graphQLType: GraphQLList; + __context: unknown; }; export type InputTypeExcludingNonNull = @@ -61,6 +63,7 @@ export type InputObjectType< > = { kind: "input"; __fields: Fields; + __context: unknown; graphQLType: GraphQLInputObjectType; }; @@ -109,5 +112,10 @@ export function inputObject< ); }, }); - return { kind: "input", __fields: undefined as any, graphQLType }; + return { + kind: "input", + __fields: undefined as any, + __context: undefined, + graphQLType, + }; } diff --git a/packages/schema/src/types/types-that-do-not-use-context/scalars.ts b/packages/schema/src/types/types-that-do-not-use-context/scalars.ts index d1fcbc1..be69504 100644 --- a/packages/schema/src/types/types-that-do-not-use-context/scalars.ts +++ b/packages/schema/src/types/types-that-do-not-use-context/scalars.ts @@ -10,6 +10,7 @@ import { export type ScalarType = { kind: "scalar"; __type: Type; + __context: unknown; graphQLType: GraphQLScalarType; }; @@ -17,6 +18,7 @@ export function custom(scalar: GraphQLScalarType): ScalarType { return { kind: "scalar", __type: undefined as any, + __context: undefined, graphQLType: scalar, }; }