diff --git a/.changeset/cuddly-wolves-sort.md b/.changeset/cuddly-wolves-sort.md new file mode 100644 index 0000000..08c33fd --- /dev/null +++ b/.changeset/cuddly-wolves-sort.md @@ -0,0 +1,6 @@ +--- +"@graphql-ts/extend": minor +"@graphql-ts/schema": minor +--- + +Type parameters named `RootVal` have been renamed to `Source` and properties named `__rootVal` have been renamed to `__source`. This won't require code changes unless you've relied on the `__rootVal` properties(which you shouldn't). diff --git a/package.json b/package.json index 21de968..798502a 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "typescript": "^4.3.5" }, "scripts": { - "postinstall": "preconstruct dev && manypkg check", + "postinstall": "preconstruct dev", "release": "preconstruct build && changeset publish", "version": "changeset version && pnpm i --frozen-lockfile=false", "test": "jest", diff --git a/packages/extend/package.json b/packages/extend/package.json index d65a9cb..e0d60ce 100644 --- a/packages/extend/package.json +++ b/packages/extend/package.json @@ -11,7 +11,7 @@ "@babel/runtime": "^7.9.2" }, "peerDependencies": { - "@graphql-ts/schema": "^0.4.0", + "@graphql-ts/schema": "^0.5.0", "graphql": "^15.0.0" }, "devDependencies": { diff --git a/packages/extend/src/index.ts b/packages/extend/src/index.ts index 0093984..84b115b 100644 --- a/packages/extend/src/index.ts +++ b/packages/extend/src/index.ts @@ -67,8 +67,29 @@ const builtinScalars = new Set(specifiedScalarTypes.map((x) => x.name)); * })(originalSchema); * ``` * + * To use existing types from the schema you're extending, you can provide a + * function and use the {@link BaseSchemaMeta} passed into the function to use + * existing types in the schema. + * + * ```ts + * const originalSchema = new GraphQLSchema({ ...etc }); + * + * const extendedSchema = extend((base) => ({ + * query: { + * something: graphql.field({ + * type: base.object("Something"), + * resolve() { + * return { something: true }; + * }, + * }), + * }, + * }))(originalSchema); + * ``` + * + * See {@link BaseSchemaMeta} for how to get other types from the schema + * * `extend` will currently throw an error if the query or mutation types are - * used in other types like this + * used in other types like this. This will be allowed in a future version. * * ```graphql * type Query { @@ -80,7 +101,7 @@ export function extend( extension: | Extension | readonly Extension[] - | ((current: BaseSchemaInfo) => Extension | readonly Extension[]) + | ((base: BaseSchemaMeta) => Extension | readonly Extension[]) ): (schema: GraphQLSchema) => GraphQLSchema { return (schema) => { const getType = (name: string) => { @@ -334,16 +355,23 @@ function flattenExtensions( } /** + * Any + * * Note the distinct usages of `any` vs `unknown` is intentional. * - * - The `unknown` used for the rootVal is because the root val isn't known and it - * should generally be used here because these fields are on the query and - * mutation types + * - The `unknown` used for the source type is because the source isn't known and + * it shouldn't generally be used here because these fields are on the query + * and mutation types * - The first `any` used for the `Args` type parameter is used because `Args` is * invariant so only `Record>` would work with * it. The arguable unsafety here doesn't really matter because people will * always use `graphql.field` - * - The `any` in `OutputType` and the last type argument `` + * - The `any` in `OutputType` and the last type argument mean that a field that + * requires any context can be provided. This is unsafe, the only way this + * could arguably be made more "safe" is by making this unknown which would + * requiring casting or make `extend` and etc. generic over a `Context` but + * given this is immediately used on an arbitrary {@link GraphQLSchema} so the + * type would immediately be thrown away, it would be pretty much pointless. */ type FieldsOnAnything = { [key: string]: Field, string, any>; @@ -363,7 +391,7 @@ export type Extension = { * query: { * isLoggedIn: graphql.field({ * type: graphql.Boolean, - * resolve(rootVal, args, context, info) { + * resolve(source, args, context, info) { * // ... * }, * }), @@ -384,7 +412,7 @@ export type Extension = { * mutation: { * createPost: graphql.field({ * type: graphql.Boolean, - * resolve(rootVal, args, context, info) { + * resolve(source, args, context, info) { * // ... * }, * }), @@ -407,15 +435,11 @@ export type Extension = { }; /** - * Information about the GraphQL schema that is being extended. Currently this - * only exposes the {@link GraphQLSchema} object. In the future, this will be - * extended to allow easily getting a type that exists in the base schema and - * wrapping it in a `@graphql-ts/schema` type to use in the extension. + * This object contains the schema being extended and functions to get wrapped * - * See the caveats mentioned {@link wrap} to learn about the lack of type safety - * guarantees with these APIs + * Note that the just like {@link wrap}, all of the GraphQL types returned */ -export type BaseSchemaInfo = { +export type BaseSchemaMeta = { schema: GraphQLSchema; /** * Gets an object type from the existing GraphQL schema and wraps it in an @@ -455,7 +479,7 @@ export type BaseSchemaInfo = { * type: base.inputObject("Something"), * }), * }, - * resolve(rootVal, { something }) { + * resolve(source, { something }) { * console.log(something); * return ""; * }, @@ -484,7 +508,7 @@ export type BaseSchemaInfo = { * type: base.enum("Something"), * }), * }, - * resolve(rootVal, { something }) { + * resolve(source, { something }) { * return something; * }, * }), @@ -560,7 +584,7 @@ export type BaseSchemaInfo = { * type: base.scalar("JSON"), * }), * }, - * resolve(rootVal, { something }) { + * resolve(source, { something }) { * return something; * }, * }), diff --git a/packages/extend/src/wrap.ts b/packages/extend/src/wrap.ts index 2f00f85..abde588 100644 --- a/packages/extend/src/wrap.ts +++ b/packages/extend/src/wrap.ts @@ -6,6 +6,8 @@ * return GraphQL types that accept/can be used as any type and can be used with * any context. If you know more specific types for the GraphQL types, you * should cast them with `as`. + * + * @module */ import { @@ -51,7 +53,7 @@ export function object( kind: "object", graphQLType, __context: undefined as any, - __rootVal: undefined as any, + __source: undefined as any, }; } @@ -68,7 +70,7 @@ export function object( * args: { * something: graphql.arg({ type: someInputObjectType }), * }, - * resolve(rootVal, { something }) { + * resolve(source, { something }) { * console.log(something); * // ... * }, @@ -99,7 +101,7 @@ export function inputObject( * args: { * something: graphql.arg({ type: wrap.enum(someEnumType) }), * }, - * resolve(rootVal, { something }) { + * resolve(source, { something }) { * console.log(something); * // ... * }, @@ -150,7 +152,7 @@ export function union( return { kind: "union", __context: undefined as any, - __rootVal: undefined as any, + __source: undefined as any, graphQLType, }; } @@ -181,7 +183,7 @@ function interfaceType( return { kind: "interface", __context: undefined as any, - __rootVal: undefined as any, + __source: undefined as any, __fields: undefined as any, graphQLType, }; diff --git a/packages/schema/src/api-without-context/input.ts b/packages/schema/src/api-without-context/input.ts index b7ea798..25ae35e 100644 --- a/packages/schema/src/api-without-context/input.ts +++ b/packages/schema/src/api-without-context/input.ts @@ -112,7 +112,7 @@ export type InputObjectType }> = * args: { * something: graphql.arg({ type: graphql.String }), * }, - * resolve(rootVal, { something }) { + * resolve(source, { something }) { * return something || somethingElse; * }, * }); @@ -145,7 +145,7 @@ export type InputObjectType }> = * args: { * something: graphql.arg({ type: graphql.nonNull(graphql.String) }), * }, - * resolve(rootVal, { something }) { + * resolve(source, { something }) { * // `something` will always be a string * return something; * }, @@ -176,7 +176,7 @@ export type Arg< * args: { * something: graphql.arg({ type: graphql.String }), * }, - * resolve(rootVal, { something }) { + * resolve(source, { something }) { * return something || somethingElse; * }, * }); diff --git a/packages/schema/src/api-without-context/list-and-non-null.ts b/packages/schema/src/api-without-context/list-and-non-null.ts index d3e77d4..a9ed65e 100644 --- a/packages/schema/src/api-without-context/list-and-non-null.ts +++ b/packages/schema/src/api-without-context/list-and-non-null.ts @@ -28,7 +28,7 @@ export type ListType> = { * graphql.field({ * type: graphql.String, * args: { thing: graphql.arg({ type: graphql.list(graphql.String) }) }, - * resolve(rootVal, { thing }) { + * resolve(source, { thing }) { * const theThing: undefined | null | Array = thing; * return ""; * }, @@ -118,7 +118,7 @@ export type NonNullType> = { * }), * }, * type: graphql.String, - * resolve(rootVal, args) { + * resolve(source, args) { * // both of these will always be a string * args.someNonNullAndRequiredArg; * args.someNonNullButOptionalArg; @@ -153,7 +153,7 @@ export type NonNullType> = { * ```ts * graphql.field({ * type: graphql.nonNull(graphql.String), - * resolve(rootVal, args) { + * resolve(source, args) { * return "something"; * }, * }); diff --git a/packages/schema/src/output.ts b/packages/schema/src/output.ts index 67dc7b3..9c81e41 100644 --- a/packages/schema/src/output.ts +++ b/packages/schema/src/output.ts @@ -96,12 +96,12 @@ type InferValueFromOutputTypeWithoutAddingNull> = // sadly functions that are iterables will be allowed by this type but not allowed by graphql-js // (though tbh, i think the chance of that causing problems is quite low) object & Iterable> - : Type extends ObjectType - ? RootVal - : Type extends UnionType - ? RootVal - : Type extends InterfaceType - ? RootVal + : Type extends ObjectType + ? Source + : Type extends UnionType + ? Source + : Type extends InterfaceType + ? Source : never; type OutputNonNullTypeForInference> = @@ -115,22 +115,22 @@ export type InferValueFromOutputType> = >; /** A GraphQL object type which should be created using {@link object `graphql.object`}. */ -export type ObjectType = { +export type ObjectType = { kind: "object"; graphQLType: GraphQLObjectType; __context: (context: Context) => void; - __rootVal: RootVal; + __source: Source; }; type MaybePromise = Promise | T; export type FieldResolver< - RootVal, + Source, Args extends Record>, TType extends OutputType, Context > = ( - rootVal: RootVal, + source: Source, args: InferValueFromArgs, context: Context, info: GraphQLResolveInfo @@ -141,7 +141,7 @@ export type FieldResolver< * {@link field `graphql.field`}. */ export type Field< - RootVal, + Source, Args extends Record>, TType extends OutputType, Key extends string, @@ -150,13 +150,13 @@ export type Field< args?: Args; type: TType; __key: Key; - __rootVal: (rootVal: RootVal) => void; + __source: (source: Source) => void; __context: (context: Context) => void; - resolve?: FieldResolver; + resolve?: FieldResolver; deprecationReason?: string; description?: string; extensions?: Readonly< - GraphQLFieldExtensions> + GraphQLFieldExtensions> >; }; @@ -177,7 +177,7 @@ export type InterfaceField< type SomeTypeThatIsntARecordOfArgs = string; type FieldFuncResolve< - RootVal, + Source, Args extends { [Key in keyof Args]: Arg }, Type extends OutputType, Key extends string, @@ -193,8 +193,8 @@ type FieldFuncResolve< // }), // }, // }); - [Key] extends [keyof RootVal] - ? RootVal[Key] extends + [Key] extends [keyof Source] + ? Source[Key] extends | InferValueFromOutputType | (( args: InferValueFromArgs, @@ -203,7 +203,7 @@ type FieldFuncResolve< ) => InferValueFromOutputType) ? { resolve?: FieldResolver< - RootVal, + Source, SomeTypeThatIsntARecordOfArgs extends Args ? {} : Args, Type, Context @@ -211,7 +211,7 @@ type FieldFuncResolve< } : { resolve: FieldResolver< - RootVal, + Source, SomeTypeThatIsntARecordOfArgs extends Args ? {} : Args, Type, Context @@ -219,7 +219,7 @@ type FieldFuncResolve< } : { resolve: FieldResolver< - RootVal, + Source, SomeTypeThatIsntARecordOfArgs extends Args ? {} : Args, Type, Context @@ -227,7 +227,7 @@ type FieldFuncResolve< }; type FieldFuncArgs< - RootVal, + Source, Args extends { [Key in keyof Args]: Arg }, Type extends OutputType, Key extends string, @@ -237,17 +237,17 @@ type FieldFuncArgs< type: Type; deprecationReason?: string; description?: string; - extensions?: Readonly>; -} & FieldFuncResolve; + extensions?: Readonly>; +} & FieldFuncResolve; export type FieldFunc = < - RootVal, + Source, Type extends OutputType, Key extends string, Args extends { [Key in keyof Args]: Arg } = {} >( - field: FieldFuncArgs -) => Field; + field: FieldFuncArgs +) => Field; function bindFieldToContext(): FieldFunc { return function field(field) { @@ -263,21 +263,21 @@ export type InterfaceToInterfaceFields< > = Interface extends InterfaceType ? Fields : never; type InterfaceFieldToOutputField< - RootVal, + Source, Context, TField extends InterfaceField, Key extends string > = TField extends InterfaceField - ? Field + ? Field : never; type InterfaceFieldsToOutputFields< - RootVal, + Source, Context, Fields extends { [Key in keyof Fields]: InterfaceField } > = { [Key in keyof Fields]: InterfaceFieldToOutputField< - RootVal, + Source, Context, Fields[Key], Extract @@ -291,12 +291,12 @@ type UnionToIntersection = (T extends any ? (x: T) => any : never) extends ( : never; export type InterfacesToOutputFields< - RootVal, + Source, Context, - Interfaces extends readonly InterfaceType[] + Interfaces extends readonly InterfaceType[] > = UnionToIntersection< InterfaceFieldsToOutputFields< - RootVal, + Source, Context, InterfaceToInterfaceFields > @@ -308,21 +308,21 @@ export type InterfacesToOutputFields< * See the docs of {@link object `graphql.object`} for more details. */ export type ObjectTypeFunc = < - RootVal + Source >(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: { youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true; }) => < Fields extends { [Key in keyof Fields]: Field< - RootVal, + Source, any, any, Extract, Context >; } & - InterfacesToOutputFields, - Interfaces extends readonly InterfaceType[] = [] + InterfacesToOutputFields, + Interfaces extends readonly InterfaceType[] = [] >(config: { name: string; fields: Fields | (() => Fields); @@ -358,7 +358,7 @@ export type ObjectTypeFunc = < * ```ts * const Node = graphql.interface<{ kind: string }>()({ * name: "Node", - * resolveType: (rootVal) => rootVal.kind, + * resolveType: (source) => source.kind, * fields: { * id: graphql.interfaceField({ type: graphql.ID }), * }, @@ -375,8 +375,8 @@ export type ObjectTypeFunc = < */ interfaces?: [...Interfaces]; isTypeOf?: GraphQLIsTypeOfFn; - extensions?: Readonly>; -}) => ObjectType; + extensions?: Readonly>; +}) => ObjectType; function bindObjectTypeToContext(): ObjectTypeFunc { return function object() { @@ -398,7 +398,7 @@ function bindObjectTypeToContext(): ObjectTypeFunc { }, extensions: config.extensions, }), - __rootVal: undefined as any, + __source: undefined as any, __context: undefined as any, }; }; @@ -436,9 +436,9 @@ function buildFields( ); } -export type UnionType = { +export type UnionType = { kind: "union"; - __rootVal: RootVal; + __source: Source; __context: (context: Context) => void; graphQLType: GraphQLUnionType; }; @@ -450,12 +450,12 @@ export type UnionTypeFunc = < description?: string; types: TObjectType[]; resolveType?: ( - type: TObjectType["__rootVal"], + type: TObjectType["__source"], context: Context, info: GraphQLResolveInfo, abstractType: GraphQLUnionType ) => string; -}) => UnionType; +}) => UnionType; function bindUnionTypeToContext(): UnionTypeFunc { return function union(config) { @@ -467,20 +467,20 @@ function bindUnionTypeToContext(): UnionTypeFunc { types: config.types.map((x) => x.graphQLType), resolveType: config.resolveType as any, }), - __rootVal: undefined as any, + __source: undefined as any, __context: undefined as any, }; }; } export type FieldsFunc = < - RootVal + Source >(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: { youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true; }) => < Fields extends { [Key in keyof Fields]: Field< - RootVal, + Source, any, any, Extract, @@ -500,7 +500,7 @@ function bindFieldsToContext(): FieldsFunc { } type InterfaceFieldFuncArgs< - RootVal, + Source, Args extends { [Key in keyof Args]: Arg }, Type extends OutputType, Context @@ -509,15 +509,15 @@ type InterfaceFieldFuncArgs< type: Type; deprecationReason?: string; description?: string; - extensions?: Readonly>; + extensions?: Readonly>; }; export type InterfaceFieldFunc = < - RootVal, + Source, Type extends OutputType, Args extends { [Key in keyof Args]: Arg } = {} >( - field: InterfaceFieldFuncArgs + field: InterfaceFieldFuncArgs ) => InterfaceField; function bindInterfaceFieldToContext(): InterfaceFieldFunc { @@ -527,7 +527,7 @@ function bindInterfaceFieldToContext(): InterfaceFieldFunc { } export type InterfaceType< - RootVal, + Source, Fields extends Record< string, InterfaceField, Context> @@ -535,14 +535,14 @@ export type InterfaceType< Context > = { kind: "interface"; - __rootVal: (rootVal: RootVal) => void; + __source: (source: Source) => void; __context: (context: Context) => void; __fields: Fields; graphQLType: GraphQLInterfaceType; }; export type InterfaceTypeFunc = < - RootVal + Source >(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: { youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true; }) => < @@ -550,16 +550,16 @@ export type InterfaceTypeFunc = < [Key in keyof Fields]: InterfaceField, Context>; } & UnionToIntersection>, - Interfaces extends readonly InterfaceType[] = [] + Interfaces extends readonly InterfaceType[] = [] >(config: { name: string; description?: string; deprecationReason?: string; interfaces?: [...Interfaces]; - resolveType?: GraphQLTypeResolver; + resolveType?: GraphQLTypeResolver; fields: Fields | (() => Fields); extensions?: Readonly; -}) => InterfaceType; +}) => InterfaceType; function bindInterfaceTypeToContext(): InterfaceTypeFunc { return function interfaceType() { @@ -580,7 +580,7 @@ function bindInterfaceTypeToContext(): InterfaceTypeFunc { return buildFields(fields as any); }, }), - __rootVal: undefined as any, + __source: undefined as any, __context: undefined as any, __fields: undefined as any, }; @@ -596,9 +596,9 @@ export type GraphQLSchemaAPIWithContext = { * `graphql.inputObject`. * * When calling `graphql.object`, you must provide a type parameter that is - * the root value of the object type. The root value what you receive as the - * first argument of resolvers on this type and what you must return from - * resolvers of fields that return this type. + * the source of the object type. The source is what you receive as the first + * argument of resolvers on this type and what you must return from resolvers + * of fields that return this type. * * ```ts * const Person = graphql.object<{ name: string }>()({ @@ -617,11 +617,11 @@ export type GraphQLSchemaAPIWithContext = { * * ## Writing resolvers * - * To do anything other than just return a field from the RootVal, you need to - * provide a resolver. + * To do anything other than just return a field from the source type, you + * need to provide a resolver. * * Note: TypeScript will force you to provide a resolve function if the field - * in the RootVal and the GraphQL field don't match + * in the source type and the GraphQL field don't match * * ```ts * const Person = graphql.object<{ name: string }>()({ @@ -630,8 +630,8 @@ export type GraphQLSchemaAPIWithContext = { * name: graphql.field({ type: graphql.String }), * excitedName: graphql.field({ * type: graphql.String, - * resolve(rootVal, args, context, info) { - * return `${rootVal.name}!`; + * resolve(source, args, context, info) { + * return `${source.name}!`; * }, * }), * }, @@ -642,14 +642,14 @@ export type GraphQLSchemaAPIWithContext = { * * GraphQL types will often contain references to themselves and to make * TypeScript allow that, you need have an explicit type annotation of - * `graphql.ObjectType` along with making `fields` a function that + * `graphql.ObjectType` along with making `fields` a function that * returns the object. * * ```ts - * type PersonRootVal = { name: string; friends: PersonRootVal[] }; + * type PersonSource = { name: string; friends: PersonSource[] }; * - * const Person: graphql.ObjectType = - * graphql.object()({ + * const Person: graphql.ObjectType = + * graphql.object()({ * name: "Person", * fields: () => ({ * name: graphql.field({ type: graphql.String }), @@ -736,7 +736,7 @@ export type GraphQLSchemaAPIWithContext = { * * ```ts * type Field< - * RootVal, + * Source, * Args extends Record>, * TType extends OutputType, * Key extends string, @@ -745,9 +745,9 @@ export type GraphQLSchemaAPIWithContext = { * ``` * * There's two especially notable bits in there which need to be inferred from - * elsewhere, the `RootVal` and `Key` type params. + * elsewhere, the `Source` and `Key` type params. * - * The `RootVal` is pretty simple and it's quite simple to see why + * The `Source` is pretty simple and it's quite simple to see why * `graphql.fields` is useful here. You could explicitly write it with * resolvers on the first arg but you'd have to do that on every field which * would get very repetitive and wouldn't work for fields without resolvers. @@ -762,7 +762,7 @@ export type GraphQLSchemaAPIWithContext = { * is that *the key that a field is at is part of its TypeScript type*. * * This is important to be able to represent the fact that a resolver is - * optional if the `RootVal` has a property at the `Key` that matches the output type. + * optional if the `Source` has a property at the `Key` that matches the output type. * * ```ts * // this is allowed @@ -772,7 +772,7 @@ export type GraphQLSchemaAPIWithContext = { * * const someFields = graphql.fields<{ name: string }>()({ * someName: graphql.field({ - * // a resolver is required here since the RootVal is missing a `someName` property + * // a resolver is required here since the Source is missing a `someName` property * type: graphql.String, * }), * }); @@ -817,9 +817,9 @@ export type GraphQLSchemaAPIWithContext = { * }, * }); * - * type PersonRootVal = { __typename: "Person"; name: string }; + * type PersonSource = { __typename: "Person"; name: string }; * - * const Person = graphql.object()({ + * const Person = graphql.object()({ * name: "Person", * interfaces: [Entity], * fields: { @@ -827,12 +827,12 @@ export type GraphQLSchemaAPIWithContext = { * }, * }); * - * type OrganisationRootVal = { + * type OrganisationSource = { * __typename: "Organisation"; * name: string; * }; * - * const Organisation = graphql.object()({ + * const Organisation = graphql.object()({ * name: "Organisation", * interfaces: [Entity], * fields: { @@ -849,8 +849,8 @@ export type GraphQLSchemaAPIWithContext = { * object types and `resolveType` on interface and union types. Note * `@graphql-ts/schema` **does not aim to strictly type the implementation of * `resolveType` and `isTypeOf`**. If you don't provide `resolveType` or - * `isTypeOf`, a `__typename` property on the root value will be used, if that - * fails, an error will be thrown at runtime. + * `isTypeOf`, a `__typename` property on the source type will be used, if + * that fails, an error will be thrown at runtime. * * ## Fields vs Interface Fields * diff --git a/packages/schema/src/schema-api.ts b/packages/schema/src/schema-api.ts index cf4a8a2..ed738aa 100644 --- a/packages/schema/src/schema-api.ts +++ b/packages/schema/src/schema-api.ts @@ -78,7 +78,7 @@ * title: graphql.arg({ type: graphql.String }), * }, * type: Todo, - * resolve(rootVal, { title }) { + * resolve(source, { title }) { * const todo = { title }; * todos.push(todo); * return todo; @@ -193,20 +193,20 @@ export type Type = graphql.Type; export type NullableOutputType = graphql.NullableOutputType; export type OutputType = graphql.OutputType; export type Field< - RootVal, + Source, Args extends Record>, TType extends OutputType, Key extends string -> = graphql.Field; +> = graphql.Field; export type FieldResolver< - RootVal, + Source, Args extends Record>, TType extends OutputType -> = graphql.FieldResolver; -export type ObjectType = graphql.ObjectType; -export type UnionType = graphql.UnionType; +> = graphql.FieldResolver; +export type ObjectType = graphql.ObjectType; +export type UnionType = graphql.UnionType; export type InterfaceType< - RootVal, + Source, Fields extends Record< string, graphql.InterfaceField< @@ -215,7 +215,7 @@ export type InterfaceType< Context > > -> = graphql.InterfaceType; +> = graphql.InterfaceType; export type InterfaceField< Args extends Record>, TType extends OutputType diff --git a/test-project/index.test-d.ts b/test-project/index.test-d.ts index 918572e..faa343e 100644 --- a/test-project/index.test-d.ts +++ b/test-project/index.test-d.ts @@ -228,8 +228,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ graphql.fields<{ path: string; listKey: string }>()({ thing: graphql.field({ - resolve(rootVal) { - return { fieldPath: rootVal.path, listKey: rootVal.listKey }; + resolve(source) { + return { fieldPath: source.path, listKey: source.listKey }; }, type: graphql.nonNull( graphql.object()({ @@ -242,8 +242,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ values: graphql.enumValues(["read", "hidden"]), }) ), - async resolve(rootVal, args, context) { - console.log(rootVal, args, context); + async resolve(source, args, context) { + console.log(source, args, context); return "read" as const; }, }), @@ -274,7 +274,7 @@ graphql.object<{ id: string } | { id: boolean }>()({ }), }); - const sharedFieldsWithUnkownRootVal = graphql.fields()({ + const sharedFieldsWithUnkownSource = graphql.fields()({ other: graphql.field({ type: graphql.nonNull(graphql.String), resolve() { @@ -287,13 +287,13 @@ graphql.object<{ id: string } | { id: boolean }>()({ name: "", fields: { ...sharedFields, - ...sharedFieldsWithUnkownRootVal, + ...sharedFieldsWithUnkownSource, }, }); graphql.object<{ other: string }>()({ name: "", - fields: sharedFieldsWithUnkownRootVal, + fields: sharedFieldsWithUnkownSource, }); graphql.object<{ other: string }>()({ @@ -352,8 +352,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ fields: { a: typesWithContextA.field({ type: graphql.String, - resolve(rootVal, args, context) { - console.log(rootVal, args, context); + resolve(source, args, context) { + console.log(source, args, context); expectType<{ something: boolean }>(context); return ""; }, @@ -365,8 +365,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ fields: { a: typesWithContextB.field({ type: fromA, - resolve(rootVal, args, context) { - console.log(rootVal, args, context); + resolve(source, args, context) { + console.log(source, args, context); expectType<{ something: boolean; other: string }>(context); return {}; }, @@ -379,8 +379,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ a: typesWithContextA.field({ // @ts-expect-error type: fromBWithA, - resolve(rootVal, args, context) { - console.log(rootVal, args, context); + resolve(source, args, context) { + console.log(source, args, context); expectType<{ something: boolean }>(context); }, }), @@ -392,8 +392,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ a: typesWithContextA.field({ // @ts-expect-error type: graphql.list(fromBWithA), - resolve(rootVal, args, context) { - console.log(rootVal, args, context); + resolve(source, args, context) { + console.log(source, args, context); expectType<{ something: boolean }>(context); }, }), @@ -405,8 +405,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ a: typesWithContextA.field({ // @ts-expect-error type: graphql.list(graphql.list(fromBWithA)), - resolve(rootVal, args, context) { - console.log(rootVal, args, context); + resolve(source, args, context) { + console.log(source, args, context); expectType<{ something: boolean }>(context); }, }), @@ -418,8 +418,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ a: typesWithContextA.field({ // @ts-expect-error type: graphql.nonNull(fromBWithA), - resolve(rootVal, args, context) { - console.log(rootVal, args, context); + resolve(source, args, context) { + console.log(source, args, context); expectType<{ something: boolean }>(context); }, }), @@ -431,8 +431,8 @@ graphql.object<{ id: string } | { id: boolean }>()({ a: typesWithContextA.field({ // @ts-expect-error type: graphql.list(graphql.nonNull(fromBWithA)), - resolve(rootVal, args, context) { - console.log(rootVal, args, context); + resolve(source, args, context) { + console.log(source, args, context); expectType<{ something: boolean }>(context); }, }), @@ -550,8 +550,8 @@ graphql.object()({ id: graphql.field({ type: graphql.ID, - resolve(rootVal, args) { - console.log(rootVal); + resolve(source, args) { + console.log(source); // @ts-expect-error args.something; return ""; diff --git a/test-project/schema-api/index.ts b/test-project/schema-api/index.ts index 16be18f..d99428d 100644 --- a/test-project/schema-api/index.ts +++ b/test-project/schema-api/index.ts @@ -16,25 +16,25 @@ export type Type = graphqltsSchema.Type; export type NullableOutputType = graphqltsSchema.NullableOutputType; export type OutputType = graphqltsSchema.OutputType; export type Field< - RootVal, + Source, Args extends Record>, TType extends OutputType, Key extends string -> = graphqltsSchema.Field; +> = graphqltsSchema.Field; export type FieldResolver< - RootVal, + Source, Args extends Record>, TType extends OutputType -> = graphqltsSchema.FieldResolver; -export type ObjectType = graphqltsSchema.ObjectType; -export type UnionType = graphqltsSchema.UnionType; +> = graphqltsSchema.FieldResolver; +export type ObjectType = graphqltsSchema.ObjectType; +export type UnionType = graphqltsSchema.UnionType; export type InterfaceType< - RootVal, + Source, Fields extends Record< string, graphqltsSchema.InterfaceField > -> = graphqltsSchema.InterfaceType; +> = graphqltsSchema.InterfaceType; export type InterfaceField< Args extends Record>, TType extends OutputType