Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Feb 8, 2021
1 parent 7f02010 commit 7425f01
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-spies-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ts-gql/schema": minor
---

Various fixes
17 changes: 13 additions & 4 deletions packages/schema/src/types/list-and-non-null.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import {
ScalarType,
} from "./types-that-do-not-use-context";

export type ListType<Of extends Types> = {
export type ListType<Of extends Types, Context = unknown> = {
kind: "list";
of: Of;
__context: Context;
graphQLType: GraphQLList<Of["graphQLType"]>;
};

export function list<Of extends Types>(of: Of): ListType<Of> {
return { kind: "list", of, graphQLType: new GraphQLList(of.graphQLType) };
return {
kind: "list",
of,
__context: of["__context"],
graphQLType: new GraphQLList(of.graphQLType),
};
}

export type NonNullType<Of extends TypesExcludingNonNull> = {
kind: "non-null";
of: Of;
__context: Of["__context"];
graphQLType: GraphQLNonNull<Of["graphQLType"]>;
};

Expand All @@ -28,6 +35,7 @@ export function nonNull<Of extends TypesExcludingNonNull>(
return {
kind: "non-null",
of,
__context: of["__context"],
graphQLType: new GraphQLNonNull(of.graphQLType) as GraphQLNonNull<
Of["graphQLType"]
>,
Expand All @@ -38,8 +46,8 @@ export type TypesExcludingNonNull =
| ScalarType<any>
| ListType<any>
| InputObjectType<any>
| ObjectType<any, string>
| UnionType<ObjectType<any, string>>
| ObjectType<any, string, any>
| UnionType<ObjectType<any, string, any>>
| EnumType<any>;

export type Types =
Expand All @@ -48,4 +56,5 @@ export type Types =
kind: "non-null";
of: TypesExcludingNonNull;
graphQLType: GraphQLNullableType;
__context: unknown;
};
176 changes: 99 additions & 77 deletions packages/schema/src/types/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type OutputNonNullType<Of extends OutputTypeExcludingNonNull> = {

export type OutputTypeExcludingNonNull =
| ScalarType<any>
| ObjectType<any, string>
| UnionType<ObjectType<any, string>>
| ObjectType<any, string, any>
| UnionType<ObjectType<any, string, any>>
| EnumType<any>
| OutputListType<any>;

Expand All @@ -48,9 +48,9 @@ type InferValueFromOutputTypeWithoutAddingNull<
? Values[string]["value"]
: Type extends OutputListType<infer Value>
? InferValueFromOutputType<Value>[]
: Type extends ObjectType<infer RootVal, string>
: Type extends ObjectType<infer RootVal, string, any>
? RootVal
: Type extends UnionType<ObjectType<infer RootVal, string>>
: Type extends UnionType<ObjectType<infer RootVal, string, any>>
? RootVal
: never;

Expand All @@ -60,10 +60,11 @@ export type InferValueFromOutputType<
? InferValueFromOutputTypeWithoutAddingNull<Value>
: InferValueFromOutputTypeWithoutAddingNull<Type> | null;

export type ObjectType<RootVal, Name extends string> = {
export type ObjectType<RootVal, Name extends string, Context> = {
kind: "object";
name: Name;
graphQLType: GraphQLObjectType;
__context: Context;
__rootVal: RootVal;
};

Expand All @@ -72,11 +73,12 @@ type MaybePromise<T> = Promise<T> | T;
export type OutputFieldResolver<
Args extends Record<string, Arg<any>>,
OutputType extends OutputTypes,
RootVal
RootVal,
Context
> = (
rootVal: RootVal,
args: InferValueFromArgs<Args>,
context: unknown,
context: Context,
info: GraphQLResolveInfo
) => MaybePromise<InferValueFromOutputType<OutputType>>;

Expand All @@ -86,13 +88,15 @@ export type OutputField<
RootVal,
Args extends Record<string, Arg<any>>,
OutputType extends OutputTypes,
Key extends string
Key extends string,
Context
> = {
args?: Args;
type: OutputType;
__key: Key;
__rootVal: RootVal;
resolve?: OutputFieldResolver<Args, OutputType, RootVal>;
__context: Context;
resolve?: OutputFieldResolver<Args, OutputType, RootVal, Context>;
deprecationReason?: string;
description?: string;
extensions?: Readonly<GraphQLFieldExtensions<RootVal, unknown>>;
Expand All @@ -101,7 +105,8 @@ export function field<
RootVal,
Args extends { [Key in keyof Args]: Arg<any, any> },
OutputType extends OutputTypes,
Key extends string
Key extends string,
Context
>(
field: {
args?: Args;
Expand All @@ -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<RootVal, Args, OutputType, Key> {
): OutputField<RootVal, Args, OutputType, Key, Context> {
return field as any;
}

export function object<RootVal>() {
return function objectInner<
Name extends string,
Fields extends {
[Key in keyof Fields]: OutputField<
RootVal,
any,
any,
Extract<Key, string>
>;
}
>(config: {
name: Name;
description?: string;
deprecationReason?: string;
fields: Fields | (() => Fields);
}): ObjectType<RootVal, Name> {
return {
kind: "object",
name: config.name,
graphQLType: new GraphQLObjectType({
export const object = bindObjectTypeToContext<unknown>();

export function bindObjectTypeToContext<Context>() {
return function object<
RootVal
>(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: {
youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true;
}) {
return function objectInner<
Name extends string,
Fields extends {
[Key in keyof Fields]: OutputField<
RootVal,
any,
any,
Extract<Key, string>,
Context
>;
}
>(config: {
name: Name;
description?: string;
deprecationReason?: string;
fields: Fields | (() => Fields);
}): ObjectType<RootVal, Name, Context> {
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<string, Arg<InputType, any>>,
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<string, Arg<InputType, any>>,
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<TObjectType extends ObjectType<any, string>> = {
export type UnionType<TObjectType extends ObjectType<any, string, any>> = {
kind: "union";
__rootVal: TObjectType["__rootVal"];
__context: TObjectType["__context"];
graphQLType: GraphQLUnionType;
};

export function union<TObjectType extends ObjectType<any, string>>(config: {
export function union<
TObjectType extends ObjectType<any, string, any>
>(config: {
name: string;
description?: string;
types: TObjectType[];
resolveType: (
type: TObjectType["__rootVal"],
context: unknown,
context: TObjectType["__context"],
info: GraphQLResolveInfo,
abstractType: GraphQLUnionType
) => TObjectType["name"];
Expand All @@ -223,5 +244,6 @@ export function union<TObjectType extends ObjectType<any, string>>(config: {
resolveType: config.resolveType as any,
}),
__rootVal: undefined as any,
__context: undefined as any,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type EnumType<Values extends Record<string, EnumValue<any>>> = {
kind: "enum";
values: Values;
graphQLType: GraphQLEnumType;
__context: unknown;
};

export function enumValues<Values extends readonly string[]>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ type InputListType<Of extends InputTypeExcludingNonNull> = {
kind: "list";
of: Of;
graphQLType: GraphQLList<Of["graphQLType"]>;
__context: unknown;
};

type InputNonNullType<Of extends InputType> = {
kind: "non-null";
of: Of;
graphQLType: GraphQLList<Of["graphQLType"]>;
__context: unknown;
};

export type InputTypeExcludingNonNull =
Expand Down Expand Up @@ -61,6 +63,7 @@ export type InputObjectType<
> = {
kind: "input";
__fields: Fields;
__context: unknown;
graphQLType: GraphQLInputObjectType;
};

Expand Down Expand Up @@ -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,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import {
export type ScalarType<Type> = {
kind: "scalar";
__type: Type;
__context: unknown;
graphQLType: GraphQLScalarType;
};

export function custom<Type>(scalar: GraphQLScalarType): ScalarType<Type> {
return {
kind: "scalar",
__type: undefined as any,
__context: undefined,
graphQLType: scalar,
};
}
Expand Down

0 comments on commit 7425f01

Please sign in to comment.