Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch schema types to use ReadonlyArrays #3182

Merged
merged 1 commit into from Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -138,7 +138,7 @@ export type {
GraphQLNamedType,
GraphQLNamedInputType,
GraphQLNamedOutputType,
ThunkArray,
ThunkReadonlyArray,
ThunkObjMap,
GraphQLSchemaConfig,
GraphQLSchemaExtensions,
Expand Down
46 changes: 24 additions & 22 deletions src/type/definition.ts
Expand Up @@ -526,10 +526,12 @@ export function getNamedType(
* Used while defining GraphQL types to allow for circular references in
* otherwise immutable type definitions.
*/
export type ThunkArray<T> = (() => Array<T>) | Array<T>;
export type ThunkReadonlyArray<T> = (() => ReadonlyArray<T>) | ReadonlyArray<T>;
export type ThunkObjMap<T> = (() => ObjMap<T>) | ObjMap<T>;

function resolveArrayThunk<T>(thunk: ThunkArray<T>): Array<T> {
function resolveReadonlyArrayThunk<T>(
thunk: ThunkReadonlyArray<T>,
): ReadonlyArray<T> {
return typeof thunk === 'function' ? thunk() : thunk;
}

Expand Down Expand Up @@ -748,7 +750,7 @@ export class GraphQLObjectType<TSource = any, TContext = any> {
extensionASTNodes: ReadonlyArray<ObjectTypeExtensionNode>;

private _fields: ThunkObjMap<GraphQLField<TSource, TContext>>;
private _interfaces: ThunkArray<GraphQLInterfaceType>;
private _interfaces: ThunkReadonlyArray<GraphQLInterfaceType>;

constructor(config: Readonly<GraphQLObjectTypeConfig<TSource, TContext>>) {
this.name = config.name;
Expand All @@ -775,7 +777,7 @@ export class GraphQLObjectType<TSource = any, TContext = any> {
return this._fields;
}

getInterfaces(): Array<GraphQLInterfaceType> {
getInterfaces(): ReadonlyArray<GraphQLInterfaceType> {
if (typeof this._interfaces === 'function') {
this._interfaces = this._interfaces();
}
Expand Down Expand Up @@ -812,8 +814,8 @@ function defineInterfaces(
config: Readonly<
GraphQLObjectTypeConfig<any, any> | GraphQLInterfaceTypeConfig<any, any>
>,
): Array<GraphQLInterfaceType> {
const interfaces = resolveArrayThunk(config.interfaces ?? []);
): ReadonlyArray<GraphQLInterfaceType> {
const interfaces = resolveReadonlyArrayThunk(config.interfaces ?? []);
devAssert(
Array.isArray(interfaces),
`${config.name} interfaces must be an Array or a function which returns an Array.`,
Expand Down Expand Up @@ -920,7 +922,7 @@ export function argsToArgsConfig(
export interface GraphQLObjectTypeConfig<TSource, TContext> {
name: string;
description?: Maybe<string>;
interfaces?: ThunkArray<GraphQLInterfaceType>;
interfaces?: ThunkReadonlyArray<GraphQLInterfaceType>;
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>;
isTypeOf?: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
extensions?: Maybe<Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>>;
Expand All @@ -930,7 +932,7 @@ export interface GraphQLObjectTypeConfig<TSource, TContext> {

interface GraphQLObjectTypeNormalizedConfig<TSource, TContext>
extends GraphQLObjectTypeConfig<any, any> {
interfaces: Array<GraphQLInterfaceType>;
interfaces: ReadonlyArray<GraphQLInterfaceType>;
fields: GraphQLFieldConfigMap<any, any>;
extensions: Maybe<Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>>;
extensionASTNodes: ReadonlyArray<ObjectTypeExtensionNode>;
Expand Down Expand Up @@ -1112,7 +1114,7 @@ export class GraphQLInterfaceType {
extensionASTNodes: ReadonlyArray<InterfaceTypeExtensionNode>;

private _fields: ThunkObjMap<GraphQLField<any, any>>;
private _interfaces: ThunkArray<GraphQLInterfaceType>;
private _interfaces: ThunkReadonlyArray<GraphQLInterfaceType>;

constructor(config: Readonly<GraphQLInterfaceTypeConfig<any, any>>) {
this.name = config.name;
Expand All @@ -1139,7 +1141,7 @@ export class GraphQLInterfaceType {
return this._fields;
}

getInterfaces(): Array<GraphQLInterfaceType> {
getInterfaces(): ReadonlyArray<GraphQLInterfaceType> {
if (typeof this._interfaces === 'function') {
this._interfaces = this._interfaces();
}
Expand Down Expand Up @@ -1175,7 +1177,7 @@ export class GraphQLInterfaceType {
export interface GraphQLInterfaceTypeConfig<TSource, TContext> {
name: string;
description?: Maybe<string>;
interfaces?: ThunkArray<GraphQLInterfaceType>;
interfaces?: ThunkReadonlyArray<GraphQLInterfaceType>;
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>;
/**
* Optionally provide a custom type resolver function. If one is not provided,
Expand All @@ -1190,7 +1192,7 @@ export interface GraphQLInterfaceTypeConfig<TSource, TContext> {

export interface GraphQLInterfaceTypeNormalizedConfig
extends GraphQLInterfaceTypeConfig<any, any> {
interfaces: Array<GraphQLInterfaceType>;
interfaces: ReadonlyArray<GraphQLInterfaceType>;
fields: GraphQLFieldConfigMap<any, any>;
extensions: Maybe<Readonly<GraphQLInterfaceTypeExtensions>>;
extensionASTNodes: ReadonlyArray<InterfaceTypeExtensionNode>;
Expand Down Expand Up @@ -1240,7 +1242,7 @@ export class GraphQLUnionType {
astNode: Maybe<UnionTypeDefinitionNode>;
extensionASTNodes: ReadonlyArray<UnionTypeExtensionNode>;

private _types: ThunkArray<GraphQLObjectType>;
private _types: ThunkReadonlyArray<GraphQLObjectType>;

constructor(config: Readonly<GraphQLUnionTypeConfig<any, any>>) {
this.name = config.name;
Expand All @@ -1259,7 +1261,7 @@ export class GraphQLUnionType {
);
}

getTypes(): Array<GraphQLObjectType> {
getTypes(): ReadonlyArray<GraphQLObjectType> {
if (typeof this._types === 'function') {
this._types = this._types();
}
Expand Down Expand Up @@ -1293,8 +1295,8 @@ export class GraphQLUnionType {

function defineTypes(
config: Readonly<GraphQLUnionTypeConfig<unknown, unknown>>,
): Array<GraphQLObjectType> {
const types = resolveArrayThunk(config.types);
): ReadonlyArray<GraphQLObjectType> {
const types = resolveReadonlyArrayThunk(config.types);
devAssert(
Array.isArray(types),
`Must provide Array of types or a function which returns such an array for Union ${config.name}.`,
Expand All @@ -1305,7 +1307,7 @@ function defineTypes(
export interface GraphQLUnionTypeConfig<TSource, TContext> {
name: string;
description?: Maybe<string>;
types: ThunkArray<GraphQLObjectType>;
types: ThunkReadonlyArray<GraphQLObjectType>;
/**
* Optionally provide a custom type resolver function. If one is not provided,
* the default implementation will call `isTypeOf` on each implementing
Expand All @@ -1319,7 +1321,7 @@ export interface GraphQLUnionTypeConfig<TSource, TContext> {

interface GraphQLUnionTypeNormalizedConfig
extends GraphQLUnionTypeConfig<any, any> {
types: Array<GraphQLObjectType>;
types: ReadonlyArray<GraphQLObjectType>;
extensions: Maybe<Readonly<GraphQLUnionTypeExtensions>>;
extensionASTNodes: ReadonlyArray<UnionTypeExtensionNode>;
}
Expand Down Expand Up @@ -1365,8 +1367,8 @@ export class GraphQLEnumType /* <T> */ {
astNode: Maybe<EnumTypeDefinitionNode>;
extensionASTNodes: ReadonlyArray<EnumTypeExtensionNode>;

private _values: Array<GraphQLEnumValue /* <T> */>;
private _valueLookup: Map<any /* T */, GraphQLEnumValue>;
private _values: ReadonlyArray<GraphQLEnumValue /* <T> */>;
private _valueLookup: ReadonlyMap<any /* T */, GraphQLEnumValue>;
private _nameLookup: ObjMap<GraphQLEnumValue>;

constructor(config: Readonly<GraphQLEnumTypeConfig /* <T> */>) {
Expand All @@ -1385,7 +1387,7 @@ export class GraphQLEnumType /* <T> */ {
devAssert(typeof config.name === 'string', 'Must provide name.');
}

getValues(): Array<GraphQLEnumValue /* <T> */> {
getValues(): ReadonlyArray<GraphQLEnumValue /* <T> */> {
return this._values;
}

Expand Down Expand Up @@ -1497,7 +1499,7 @@ function didYouMeanEnumValue(
function defineEnumValues(
typeName: string,
valueMap: GraphQLEnumValueConfigMap /* <T> */,
): Array<GraphQLEnumValue /* <T> */> {
): ReadonlyArray<GraphQLEnumValue /* <T> */> {
devAssert(
isPlainObj(valueMap),
`${typeName} values must be an object with value names as keys.`,
Expand Down
4 changes: 2 additions & 2 deletions src/type/directives.ts
Expand Up @@ -56,7 +56,7 @@ export interface GraphQLDirectiveExtensions {
export class GraphQLDirective {
name: string;
description: Maybe<string>;
locations: Array<DirectiveLocationEnum>;
locations: ReadonlyArray<DirectiveLocationEnum>;
args: ReadonlyArray<GraphQLArgument>;
isRepeatable: boolean;
extensions: Maybe<Readonly<GraphQLDirectiveExtensions>>;
Expand Down Expand Up @@ -113,7 +113,7 @@ export class GraphQLDirective {
export interface GraphQLDirectiveConfig {
name: string;
description?: Maybe<string>;
locations: Array<DirectiveLocationEnum>;
locations: ReadonlyArray<DirectiveLocationEnum>;
args?: Maybe<GraphQLFieldConfigArgumentMap>;
isRepeatable?: Maybe<boolean>;
extensions?: Maybe<Readonly<GraphQLDirectiveExtensions>>;
Expand Down
2 changes: 1 addition & 1 deletion src/type/index.ts
Expand Up @@ -76,7 +76,7 @@ export type {
GraphQLNamedType,
GraphQLNamedInputType,
GraphQLNamedOutputType,
ThunkArray,
ThunkReadonlyArray,
ThunkObjMap,
GraphQLArgument,
GraphQLArgumentConfig,
Expand Down
14 changes: 7 additions & 7 deletions src/type/schema.ts
Expand Up @@ -287,8 +287,8 @@ export class GraphQLSchema {
}

getImplementations(interfaceType: GraphQLInterfaceType): {
objects: /* $ReadOnly */ Array<GraphQLObjectType>;
interfaces: /* $ReadOnly */ Array<GraphQLInterfaceType>;
objects: ReadonlyArray<GraphQLObjectType>;
interfaces: ReadonlyArray<GraphQLInterfaceType>;
} {
const implementations = this._implementationsMap[interfaceType.name];
return implementations ?? { objects: [], interfaces: [] };
Expand Down Expand Up @@ -336,7 +336,7 @@ export class GraphQLSchema {
mutation: this.getMutationType(),
subscription: this.getSubscriptionType(),
types: Object.values(this.getTypeMap()),
directives: this.getDirectives().slice(),
directives: this.getDirectives(),
extensions: this.extensions,
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes,
Expand Down Expand Up @@ -367,8 +367,8 @@ export interface GraphQLSchemaConfig extends GraphQLSchemaValidationOptions {
query?: Maybe<GraphQLObjectType>;
mutation?: Maybe<GraphQLObjectType>;
subscription?: Maybe<GraphQLObjectType>;
types?: Maybe<Array<GraphQLNamedType>>;
directives?: Maybe<Array<GraphQLDirective>>;
types?: Maybe<ReadonlyArray<GraphQLNamedType>>;
directives?: Maybe<ReadonlyArray<GraphQLDirective>>;
extensions?: Maybe<Readonly<GraphQLSchemaExtensions>>;
astNode?: Maybe<SchemaDefinitionNode>;
extensionASTNodes?: Maybe<ReadonlyArray<SchemaExtensionNode>>;
Expand All @@ -379,8 +379,8 @@ export interface GraphQLSchemaConfig extends GraphQLSchemaValidationOptions {
*/
export interface GraphQLSchemaNormalizedConfig extends GraphQLSchemaConfig {
description: Maybe<string>;
types: Array<GraphQLNamedType>;
directives: Array<GraphQLDirective>;
types: ReadonlyArray<GraphQLNamedType>;
directives: ReadonlyArray<GraphQLDirective>;
extensions: Maybe<Readonly<GraphQLSchemaExtensions>>;
extensionASTNodes: ReadonlyArray<SchemaExtensionNode>;
assumeValid: boolean;
Expand Down
18 changes: 10 additions & 8 deletions src/utilities/buildASTSchema.ts
Expand Up @@ -78,15 +78,17 @@ export function buildASTSchema(
}
}

const { directives } = config;
// If specified directives were not explicitly declared, add them.
for (const stdDirective of specifiedDirectives) {
if (directives.every((directive) => directive.name !== stdDirective.name)) {
directives.push(stdDirective);
}
}
const directives = [
...config.directives,
// If specified directives were not explicitly declared, add them.
...specifiedDirectives.filter((stdDirective) =>
config.directives.every(
(directive) => directive.name !== stdDirective.name,
),
),
];

return new GraphQLSchema(config);
return new GraphQLSchema({ ...config, directives });
}

/**
Expand Down