Skip to content

Commit

Permalink
Use empty arrays instead of undefined properties
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Feb 21, 2018
1 parent 499a759 commit 57fddc0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ export class GraphQLObjectType {
name: string;
description: ?string;
astNode: ?ObjectTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<ObjectTypeExtensionNode>;
extensionASTNodes: $ReadOnlyArray<ObjectTypeExtensionNode>;
isTypeOf: ?GraphQLIsTypeOfFn<*, *>;

_typeConfig: GraphQLObjectTypeConfig<*, *>;
Expand All @@ -574,7 +574,7 @@ export class GraphQLObjectType {
this.name = config.name;
this.description = config.description;
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes;
this.extensionASTNodes = config.extensionASTNodes || [];
this.isTypeOf = config.isTypeOf;
this._typeConfig = config;
invariant(typeof config.name === 'string', 'Must provide name.');
Expand Down Expand Up @@ -820,7 +820,7 @@ export class GraphQLInterfaceType {
name: string;
description: ?string;
astNode: ?InterfaceTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<InterfaceTypeExtensionNode>;
extensionASTNodes: $ReadOnlyArray<InterfaceTypeExtensionNode>;
resolveType: ?GraphQLTypeResolver<*, *>;

_typeConfig: GraphQLInterfaceTypeConfig<*, *>;
Expand All @@ -830,7 +830,7 @@ export class GraphQLInterfaceType {
this.name = config.name;
this.description = config.description;
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes;
this.extensionASTNodes = config.extensionASTNodes || [];
this.resolveType = config.resolveType;
this._typeConfig = config;
invariant(typeof config.name === 'string', 'Must provide name.');
Expand Down
4 changes: 2 additions & 2 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class GraphQLSchema {
// Used as a cache for validateSchema().
__validationErrors: ?$ReadOnlyArray<GraphQLError>;
// Referenced by validateSchema().
__allowedLegacyNames: ?$ReadOnlyArray<string>;
__allowedLegacyNames: $ReadOnlyArray<string>;

constructor(config: GraphQLSchemaConfig): void {
// If this schema was built from a source known to be valid, then it may be
Expand Down Expand Up @@ -113,7 +113,7 @@ export class GraphQLSchema {
);
}

this.__allowedLegacyNames = config.allowedLegacyNames;
this.__allowedLegacyNames = config.allowedLegacyNames || [];
this._queryType = config.query;
this._mutationType = config.mutation;
this._subscriptionType = config.subscription;
Expand Down
17 changes: 5 additions & 12 deletions src/type/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ function validateName(
): void {
// If a schema explicitly allows some legacy name which is no longer valid,
// allow it to be assumed valid.
if (
context.schema.__allowedLegacyNames &&
context.schema.__allowedLegacyNames.indexOf(node.name) !== -1
) {
if (context.schema.__allowedLegacyNames.indexOf(node.name) !== -1) {
return;
}
// Ensure names are valid, however introspection types opt out.
Expand Down Expand Up @@ -564,10 +561,8 @@ function getAllObjectNodes(
type: GraphQLObjectType,
): $ReadOnlyArray<ObjectTypeDefinitionNode | ObjectTypeExtensionNode> {
return type.astNode
? type.extensionASTNodes
? [type.astNode].concat(type.extensionASTNodes)
: [type.astNode]
: type.extensionASTNodes || [];
? [type.astNode].concat(type.extensionASTNodes)
: type.extensionASTNodes;
}

function getAllObjectOrInterfaceNodes(
Expand All @@ -579,10 +574,8 @@ function getAllObjectOrInterfaceNodes(
| InterfaceTypeExtensionNode,
> {
return type.astNode
? type.extensionASTNodes
? [type.astNode].concat(type.extensionASTNodes)
: [type.astNode]
: type.extensionASTNodes || [];
? [type.astNode].concat(type.extensionASTNodes)
: type.extensionASTNodes;
}

function getImplementsInterfaceNode(
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('extendSchema', () => {
const testDirective = extendedTwiceSchema.getDirective('test');

expect(query.extensionASTNodes).to.have.lengthOf(2);
expect(testType.extensionASTNodes).to.equal(undefined);
expect(testType.extensionASTNodes).to.have.lengthOf(0);

const restoredExtensionAST = {
kind: Kind.DOCUMENT,
Expand Down
15 changes: 5 additions & 10 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,9 @@ export function extendSchema(
// Support both original legacy names and extended legacy names.
const schemaAllowedLegacyNames = schema.__allowedLegacyNames;
const extendAllowedLegacyNames = options && options.allowedLegacyNames;
const allowedLegacyNames =
schemaAllowedLegacyNames && extendAllowedLegacyNames
? schemaAllowedLegacyNames.concat(extendAllowedLegacyNames)
: schemaAllowedLegacyNames || extendAllowedLegacyNames;
const allowedLegacyNames = extendAllowedLegacyNames
? schemaAllowedLegacyNames.concat(extendAllowedLegacyNames)
: schemaAllowedLegacyNames;

// Then produce and return a Schema with these types.
return new GraphQLSchema({
Expand Down Expand Up @@ -309,9 +308,7 @@ export function extendSchema(
function extendObjectType(type: GraphQLObjectType): GraphQLObjectType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: type.extensionASTNodes;
return new GraphQLObjectType({
name,
Expand All @@ -329,9 +326,7 @@ export function extendSchema(
): GraphQLInterfaceType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: type.extensionASTNodes;
return new GraphQLInterfaceType({
name: type.name,
Expand Down

0 comments on commit 57fddc0

Please sign in to comment.