Skip to content

Commit

Permalink
feat(nullable): add support for setting defaultNullable option
Browse files Browse the repository at this point in the history
wip: refactor name src
  • Loading branch information
MichalLytek committed Apr 4, 2019
1 parent 08b4aa1 commit 1917b33
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export function convertTypeIfScalar(type: any): GraphQLScalarType | undefined {
export function wrapWithTypeOptions<T extends GraphQLType>(
typeOwnerName: string,
type: T,
typeOptions: TypeOptions = {},
typeOptions: TypeOptions,
nullableByDefault: boolean,
): T {
if (
!typeOptions.array &&
Expand All @@ -61,15 +62,21 @@ export function wrapWithTypeOptions<T extends GraphQLType>(

let gqlType: GraphQLType = type;
if (typeOptions.array) {
if (typeOptions.nullable === "items" || typeOptions.nullable === "itemsAndList") {
if (
typeOptions.nullable === "items" ||
typeOptions.nullable === "itemsAndList" ||
(typeOptions.nullable === undefined && nullableByDefault === true)
) {
gqlType = new GraphQLList(gqlType);
} else {
gqlType = new GraphQLList(new GraphQLNonNull(gqlType));
}
}
if (
typeOptions.defaultValue === undefined &&
(!typeOptions.nullable || typeOptions.nullable === "items")
(typeOptions.nullable === false ||
(typeOptions.nullable === undefined && nullableByDefault === false) ||
typeOptions.nullable === "items")
) {
gqlType = new GraphQLNonNull(gqlType);
}
Expand Down
10 changes: 10 additions & 0 deletions src/schema/build-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface BuildContextOptions {
pubSub?: PubSubEngine | PubSubOptions;
globalMiddlewares?: Array<Middleware<any>>;
container?: ContainerType | ContainerGetter<any>;
/**
* Default value for type decorators, like `@Field({ nullable: true })`
*/
nullableByDefault?: boolean;
}

export abstract class BuildContext {
Expand All @@ -37,6 +41,7 @@ export abstract class BuildContext {
static pubSub: PubSubEngine;
static globalMiddlewares: Array<Middleware<any>>;
static container: IOCContainer;
static nullableByDefault: boolean;

/**
* Set static fields with current building context data
Expand Down Expand Up @@ -75,6 +80,10 @@ export abstract class BuildContext {
}

this.container = new IOCContainer(options.container);

if (options.nullableByDefault !== undefined) {
this.nullableByDefault = options.nullableByDefault;
}
}

/**
Expand All @@ -89,6 +98,7 @@ export abstract class BuildContext {
this.pubSub = new PubSub();
this.globalMiddlewares = [];
this.container = new IOCContainer();
this.nullableByDefault = false;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/schema/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ export abstract class SchemaGenerator {
throw new Error(`Cannot determine GraphQL output type for ${typeOwnerName}`!);
}

return wrapWithTypeOptions(typeOwnerName, gqlType, typeOptions);
const { nullableByDefault } = BuildContext;
return wrapWithTypeOptions(typeOwnerName, gqlType, typeOptions, nullableByDefault);
}

private static getGraphQLInputType(
Expand All @@ -569,6 +570,7 @@ export abstract class SchemaGenerator {
throw new Error(`Cannot determine GraphQL input type for ${typeOwnerName}`!);
}

return wrapWithTypeOptions(typeOwnerName, gqlType, typeOptions);
const { nullableByDefault } = BuildContext;
return wrapWithTypeOptions(typeOwnerName, gqlType, typeOptions, nullableByDefault);
}
}

0 comments on commit 1917b33

Please sign in to comment.