Skip to content

List & Non-Null Types

List Types

GraphQL list types represent a list of values of a specific type. In @graphql-ts/schema, you can create a list type by calling the g.list function with the type of the list as the argument. They can be used in both input and output types.

const
const Query: GObjectType<unknown, {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
Query
=
const g: GWithContext<{
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
g
.
object: <unknown>(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: {
youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true;
}) => <Fields, Interfaces>(config: {
...;
} & Omit<...>) => GObjectType<...>
object
()({
name: string
name
: "Query",
fields: {
users: GField<unknown, {
ids: GArg<GNonNull<GList<GNonNull<GScalarType<string, string>>>>, false>;
}, GList<GObjectType<UserSource, {
...;
}>>, unknown, {
...;
}>;
} | (() => {
users: GField<unknown, {
ids: GArg<GNonNull<GList<GNonNull<GScalarType<string, string>>>>, false>;
}, GList<GObjectType<UserSource, {
...;
}>>, unknown, {
...;
}>;
})
fields
: {
users: GField<unknown, {
ids: GArg<GNonNull<GList<GNonNull<GScalarType<string, string>>>>, false>;
}, GList<GObjectType<UserSource, {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>>, unknown, {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
users
:
const g: GWithContext<{
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
g
.
field: <unknown, GList<GObjectType<UserSource, {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>>, (_: unknown, args: {
readonly ids: string[];
}, context: {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}) => Promise<...>, {
...;
}>(field: FieldFuncArgs<...> & {
...;
}) => GField<...>
field
({
type: GList<GObjectType<UserSource, {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>>
type
:
const g: GWithContext<{
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
g
.
list: <GObjectType<UserSource, {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>>(of: GObjectType<UserSource, {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>) => GList<...>
list
(
const User: GObjectType<UserSource, {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
User
),
args?: {
ids: GArg<GNonNull<GList<GNonNull<GScalarType<string, string>>>>, false>;
}
args
: {
ids: GArg<GNonNull<GList<GNonNull<GScalarType<string, string>>>>, false>
ids
:
const g: GWithContext<{
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
g
.
arg: <GNonNull<GList<GNonNull<GScalarType<string, string>>>>, undefined>(arg: {
type: GNonNull<GList<GNonNull<GScalarType<string, string>>>>;
description?: Maybe<...>;
extensions?: (Readonly<...> & Readonly<...>) | ... 1 more ... | undefined;
astNode?: Maybe<...>;
deprecationReason?: Maybe<...>;
} & {
...;
}) => GArg<...>
arg
({
type: GNonNull<GList<GNonNull<GScalarType<string, string>>>>
type
:
const g: GWithContext<{
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
g
.
nonNull: <GList<GNonNull<GScalarType<string, string>>>>(of: GList<GNonNull<GScalarType<string, string>>>) => GNonNull<GList<GNonNull<GScalarType<...>>>>
nonNull
(
const g: GWithContext<{
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
g
.
list: <GNonNull<GScalarType<string, string>>>(of: GNonNull<GScalarType<string, string>>) => GList<GNonNull<GScalarType<string, string>>>
list
(
const g: GWithContext<{
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
g
.
nonNull: <GScalarType<string, string>>(of: GScalarType<string, string>) => GNonNull<GScalarType<string, string>>
nonNull
(
const g: GWithContext<{
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}>
g
.
type ID: GScalarType<string, string>
ID
))) }),
},
resolve: ((source: unknown, args: {
readonly ids: string[];
}, context: {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}, info: GraphQLResolveInfo) => InferValueFromOutputType<...>) & ((_: unknown, args: {
...;
}, context: {
...;
}) => Promise<...>)
resolve
(
_: unknown
_
,
args: {
readonly ids: string[];
}
args
,
context: {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}
context
) {
return
context: {
fetchUsers: (ids: string[]) => Promise<UserSource[]>;
}
context
.
fetchUsers: (ids: string[]) => Promise<UserSource[]>
fetchUsers
(
args: {
readonly ids: string[];
}
args
.
ids: string[]
ids
);
},
}),
},
});

Non-Null Types

In GraphQL, by default every type can also be null (and in the case of input types, undefined as well). You can make a type non-null by wrapping it in the g.nonNull function. Non-null types can be used in both input and output types.

type
type UserSource = {
id: string;
name: string;
}
UserSource
= {
id: string
id
: string;
name: string
name
: string };
const
const User: GObjectType<UserSource, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>
User
=
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
object: <UserSource>(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: {
youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true;
}) => <Fields, Interfaces>(config: {
...;
} & Omit<...>) => GObjectType<...>
object
<
type UserSource = {
id: string;
name: string;
}
UserSource
>()({
name: string
name
: "User",
fields: {
id: GField<UserSource, any, GNonNull<GScalarType<string, string>>, ImpliedResolver<any, GNonNull<GScalarType<string, string>>, {
...;
}>, {
...;
}>;
name: GField<...>;
} | (() => {
id: GField<UserSource, any, GNonNull<GScalarType<string, string>>, ImpliedResolver<any, GNonNull<GScalarType<string, string>>, {
...;
}>, {
...;
}>;
name: GField<...>;
})
fields
: {
id: GField<UserSource, any, GNonNull<GScalarType<string, string>>, ImpliedResolver<any, GNonNull<GScalarType<string, string>>, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>
id
:
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
field: <UserSource, GNonNull<GScalarType<string, string>>, unknown, any>(field: FieldFuncArgs<UserSource, any, GNonNull<GScalarType<string, string>>, {
...;
}> & {
...;
}) => GField<...>
field
({
type: GNonNull<GScalarType<string, string>>
type
:
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
nonNull: <GScalarType<string, string>>(of: GScalarType<string, string>) => GNonNull<GScalarType<string, string>>
nonNull
(
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
type ID: GScalarType<string, string>
ID
) }),
name: GField<UserSource, any, GNonNull<GScalarType<string, string>>, ImpliedResolver<any, GNonNull<GScalarType<string, string>>, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>
name
:
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
field: <UserSource, GNonNull<GScalarType<string, string>>, unknown, any>(field: FieldFuncArgs<UserSource, any, GNonNull<GScalarType<string, string>>, {
...;
}> & {
...;
}) => GField<...>
field
({
type: GNonNull<GScalarType<string, string>>
type
:
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
nonNull: <GScalarType<string, string>>(of: GScalarType<string, string>) => GNonNull<GScalarType<string, string>>
nonNull
(
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
type String: GScalarType<string, string>
String
) }),
},
});
const
const Query: GObjectType<unknown, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>
Query
=
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
object: <unknown>(youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction?: {
youOnlyNeedToPassATypeParameterToThisFunctionYouPassTheActualRuntimeArgsOnTheResultOfThisFunction: true;
}) => <Fields, Interfaces>(config: {
...;
} & Omit<...>) => GObjectType<...>
object
()({
name: string
name
: "Query",
fields: {
user: GField<unknown, {
id: GArg<GNonNull<GScalarType<string, string>>, false>;
}, GObjectType<UserSource, {
...;
}>, unknown, {
...;
}>;
} | (() => {
user: GField<unknown, {
id: GArg<GNonNull<GScalarType<string, string>>, false>;
}, GObjectType<UserSource, {
...;
}>, unknown, {
...;
}>;
})
fields
: {
user: GField<unknown, {
id: GArg<GNonNull<GScalarType<string, string>>, false>;
}, GObjectType<UserSource, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>, unknown, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>
user
:
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
field: <unknown, GObjectType<UserSource, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>, (_: unknown, args: {
readonly id: string;
}, context: {
fetchUser: (id: string) => Promise<UserSource | null>;
}) => Promise<...>, {
...;
}>(field: FieldFuncArgs<...> & {
...;
}) => GField<...>
field
({
type: GObjectType<UserSource, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>
type
:
const User: GObjectType<UserSource, {
fetchUser: (id: string) => Promise<UserSource | null>;
}>
User
,
args?: {
id: GArg<GNonNull<GScalarType<string, string>>, false>;
}
args
: {
id: GArg<GNonNull<GScalarType<string, string>>, false>
id
:
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
arg: <GNonNull<GScalarType<string, string>>, undefined>(arg: {
type: GNonNull<GScalarType<string, string>>;
description?: Maybe<string>;
extensions?: (Readonly<...> & Readonly<...>) | ... 1 more ... | undefined;
astNode?: Maybe<...>;
deprecationReason?: Maybe<...>;
} & {
...;
}) => GArg<...>
arg
({
type: GNonNull<GScalarType<string, string>>
type
:
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
nonNull: <GScalarType<string, string>>(of: GScalarType<string, string>) => GNonNull<GScalarType<string, string>>
nonNull
(
const g: GWithContext<{
fetchUser: (id: string) => Promise<UserSource | null>;
}>
g
.
type ID: GScalarType<string, string>
ID
) }),
},
resolve: ((source: unknown, args: {
readonly id: string;
}, context: {
fetchUser: (id: string) => Promise<UserSource | null>;
}, info: GraphQLResolveInfo) => InferValueFromOutputType<...>) & ((_: unknown, args: {
...;
}, context: {
...;
}) => Promise<...>)
resolve
(
_: unknown
_
,
args: {
readonly id: string;
}
args
,
context: {
fetchUser: (id: string) => Promise<UserSource | null>;
}
context
) {
return
context: {
fetchUser: (id: string) => Promise<UserSource | null>;
}
context
.
fetchUser: (id: string) => Promise<UserSource | null>
fetchUser
(
args: {
readonly id: string;
}
args
.
id: string
id
);
},
}),
},
});