Skip to content

Commit

Permalink
Factor out into utility function, change applyToStringTag naming to m…
Browse files Browse the repository at this point in the history
…atch

Also realized we weren't doing the same thing for directives, so fixed that as well.
  • Loading branch information
leebyron committed Jun 11, 2018
1 parent f12eacb commit 58bf40e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
17 changes: 17 additions & 0 deletions src/jsutils/defineToJSON.js
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

/**
* The `applyToJSON()` function defines toJSON() and inspect() prototype
* methods which are aliases for toString().
*/
export default function applyToJSON(classObject: Class<any>): void {
classObject.prototype.toJSON = classObject.prototype.inspect =
classObject.prototype.toString;
}
File renamed without changes.
4 changes: 2 additions & 2 deletions src/language/source.js
Expand Up @@ -8,7 +8,7 @@
*/

import invariant from '../jsutils/invariant';
import applyToStringTag from '../jsutils/applyToStringTag';
import defineToStringTag from '../jsutils/defineToStringTag';

type Location = {
line: number,
Expand Down Expand Up @@ -44,4 +44,4 @@ export class Source {
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(Source);
defineToStringTag(Source);
57 changes: 20 additions & 37 deletions src/type/definition.js
Expand Up @@ -7,7 +7,8 @@
* @flow strict
*/

import applyToStringTag from '../jsutils/applyToStringTag';
import defineToJSON from '../jsutils/defineToJSON';
import defineToStringTag from '../jsutils/defineToStringTag';
import instanceOf from '../jsutils/instanceOf';
import inspect from '../jsutils/inspect';
import invariant from '../jsutils/invariant';
Expand Down Expand Up @@ -349,11 +350,11 @@ export function GraphQLList(ofType) {
}
}

// Also provide toJSON and inspect aliases for toString.
const listProto: any = GraphQLList.prototype;
listProto.toString = listProto.toJSON = listProto.inspect = function toString() {
// Need to cast through any to alter the prototype.
(GraphQLList.prototype: any).toString = function toString() {
return '[' + String(this.ofType) + ']';
};
defineToJSON(GraphQLList);

/**
* Non-Null Type Wrapper
Expand Down Expand Up @@ -390,11 +391,11 @@ export function GraphQLNonNull(ofType) {
}
}

// Also provide toJSON and inspect aliases for toString.
const nonNullProto: any = GraphQLNonNull.prototype;
nonNullProto.toString = nonNullProto.toJSON = nonNullProto.inspect = function toString() {
// Need to cast through any to alter the prototype.
(GraphQLNonNull.prototype: any).toString = function toString() {
return String(this.ofType) + '!';
};
defineToJSON(GraphQLNonNull);

/**
* These types wrap and modify other types
Expand Down Expand Up @@ -579,14 +580,11 @@ export class GraphQLScalarType {
toString(): string {
return this.name;
}

// Also provide toJSON and inspect aliases for toString.
toJSON: () => string = this.toString;
inspect: () => string = this.toString;
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(GraphQLScalarType);
defineToStringTag(GraphQLScalarType);
defineToJSON(GraphQLScalarType);

export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
name: string,
Expand Down Expand Up @@ -681,14 +679,11 @@ export class GraphQLObjectType {
toString(): string {
return this.name;
}

// Also provide toJSON and inspect aliases for toString.
toJSON: () => string = this.toString;
inspect: () => string = this.toString;
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(GraphQLObjectType);
defineToStringTag(GraphQLObjectType);
defineToJSON(GraphQLObjectType);

function defineInterfaces(
type: GraphQLObjectType,
Expand Down Expand Up @@ -930,14 +925,11 @@ export class GraphQLInterfaceType {
toString(): string {
return this.name;
}

// Also provide toJSON and inspect aliases for toString.
toJSON: () => string = this.toString;
inspect: () => string = this.toString;
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(GraphQLInterfaceType);
defineToStringTag(GraphQLInterfaceType);
defineToJSON(GraphQLInterfaceType);

export type GraphQLInterfaceTypeConfig<TSource, TContext> = {
name: string,
Expand Down Expand Up @@ -1010,14 +1002,11 @@ export class GraphQLUnionType {
toString(): string {
return this.name;
}

// Also provide toJSON and inspect aliases for toString.
toJSON: () => string = this.toString;
inspect: () => string = this.toString;
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(GraphQLUnionType);
defineToStringTag(GraphQLUnionType);
defineToJSON(GraphQLUnionType);

function defineTypes(
unionType: GraphQLUnionType,
Expand Down Expand Up @@ -1126,14 +1115,11 @@ export class GraphQLEnumType /* <T> */ {
toString(): string {
return this.name;
}

// Also provide toJSON and inspect aliases for toString.
toJSON: () => string = this.toString;
inspect: () => string = this.toString;
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(GraphQLEnumType);
defineToStringTag(GraphQLEnumType);
defineToJSON(GraphQLEnumType);

function defineEnumValues(
type: GraphQLEnumType,
Expand Down Expand Up @@ -1260,14 +1246,11 @@ export class GraphQLInputObjectType {
toString(): string {
return this.name;
}

// Also provide toJSON and inspect aliases for toString.
toJSON: () => string = this.toString;
inspect: () => string = this.toString;
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(GraphQLInputObjectType);
defineToStringTag(GraphQLInputObjectType);
defineToJSON(GraphQLInputObjectType);

export type GraphQLInputObjectTypeConfig = {
name: string,
Expand Down
10 changes: 8 additions & 2 deletions src/type/directives.js
Expand Up @@ -13,7 +13,8 @@ import type {
} from './definition';
import { GraphQLNonNull } from './definition';
import { GraphQLString, GraphQLBoolean } from './scalars';
import applyToStringTag from '../jsutils/applyToStringTag';
import defineToStringTag from '../jsutils/defineToStringTag';
import defineToJSON from '../jsutils/defineToJSON';
import instanceOf from '../jsutils/instanceOf';
import invariant from '../jsutils/invariant';
import type { DirectiveDefinitionNode } from '../language/ast';
Expand Down Expand Up @@ -75,10 +76,15 @@ export class GraphQLDirective {
});
}
}

toString(): string {
return '@' + this.name;
}
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(GraphQLDirective);
defineToStringTag(GraphQLDirective);
defineToJSON(GraphQLDirective);

export type GraphQLDirectiveConfig = {
name: string,
Expand Down
4 changes: 2 additions & 2 deletions src/type/schema.js
Expand Up @@ -33,7 +33,7 @@ import {
import type { GraphQLError } from '../error/GraphQLError';
import inspect from '../jsutils/inspect';
import { __Schema } from './introspection';
import applyToStringTag from '../jsutils/applyToStringTag';
import defineToStringTag from '../jsutils/defineToStringTag';
import find from '../jsutils/find';
import instanceOf from '../jsutils/instanceOf';
import invariant from '../jsutils/invariant';
Expand Down Expand Up @@ -233,7 +233,7 @@ export class GraphQLSchema {
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
applyToStringTag(GraphQLSchema);
defineToStringTag(GraphQLSchema);

type TypeMap = ObjMap<GraphQLNamedType>;

Expand Down

0 comments on commit 58bf40e

Please sign in to comment.