Skip to content

Define 'toStringTag' directly inside class definitions #2367

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

Merged
merged 1 commit into from
Jan 20, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions src/jsutils/defineToStringTag.js

This file was deleted.

11 changes: 7 additions & 4 deletions src/language/source.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @flow strict

import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';

import devAssert from '../jsutils/devAssert';
import defineToStringTag from '../jsutils/defineToStringTag';

type Location = {|
line: number,
Expand Down Expand Up @@ -34,7 +35,9 @@ export class Source {
'column in locationOffset is 1-indexed and must be positive.',
);
}
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
defineToStringTag(Source);
// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'Source';
}
}
5 changes: 5 additions & 0 deletions src/polyfills/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export const SYMBOL_ITERATOR: string =
export const SYMBOL_ASYNC_ITERATOR: string =
// $FlowFixMe Flow doesn't define `Symbol.asyncIterator` yet
typeof Symbol === 'function' ? Symbol.asyncIterator : '@@asyncIterator';

/* istanbul ignore next (See: https://github.com/graphql/graphql-js/issues/2317) */
export const SYMBOL_TO_STRING_TAG: string =
// $FlowFixMe Flow doesn't define `Symbol.toStringTag` yet
typeof Symbol === 'function' ? Symbol.toStringTag : '@@toStringTag';
60 changes: 43 additions & 17 deletions src/type/definition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow strict

import objectEntries from '../polyfills/objectEntries';
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';

import inspect from '../jsutils/inspect';
import keyMap from '../jsutils/keyMap';
Expand All @@ -15,7 +16,6 @@ import isObjectLike from '../jsutils/isObjectLike';
import identityFunc from '../jsutils/identityFunc';
import defineToJSON from '../jsutils/defineToJSON';
import suggestionList from '../jsutils/suggestionList';
import defineToStringTag from '../jsutils/defineToStringTag';
import { type PromiseOrValue } from '../jsutils/PromiseOrValue';
import {
type ObjMap,
Expand Down Expand Up @@ -367,8 +367,12 @@ export function GraphQLList(ofType) {
return '[' + String(this.ofType) + ']';
};

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
defineToStringTag(GraphQLList);
Object.defineProperty(GraphQLList.prototype, SYMBOL_TO_STRING_TAG, {
get() {
return 'GraphQLList';
},
});

defineToJSON(GraphQLList);

/**
Expand Down Expand Up @@ -411,8 +415,12 @@ export function GraphQLNonNull(ofType) {
return String(this.ofType) + '!';
};

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
defineToStringTag(GraphQLNonNull);
Object.defineProperty(GraphQLNonNull.prototype, SYMBOL_TO_STRING_TAG, {
get() {
return 'GraphQLNonNull';
},
});

defineToJSON(GraphQLNonNull);

/**
Expand Down Expand Up @@ -608,10 +616,13 @@ export class GraphQLScalarType {
toString(): string {
return this.name;
}

// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLScalarType';
}
}

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

export type GraphQLScalarSerializer<TExternal> = (
Expand Down Expand Up @@ -743,10 +754,13 @@ export class GraphQLObjectType {
toString(): string {
return this.name;
}

// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLObjectType';
}
}

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

function defineInterfaces(
Expand Down Expand Up @@ -1050,10 +1064,13 @@ export class GraphQLInterfaceType {
toString(): string {
return this.name;
}

// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLInterfaceType';
}
}

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

export type GraphQLInterfaceTypeConfig<TSource, TContext> = {|
Expand Down Expand Up @@ -1149,10 +1166,13 @@ export class GraphQLUnionType {
toString(): string {
return this.name;
}

// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLUnionType';
}
}

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

function defineTypes(
Expand Down Expand Up @@ -1319,10 +1339,13 @@ export class GraphQLEnumType /* <T> */ {
toString(): string {
return this.name;
}

// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLEnumType';
}
}

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

function didYouMeanEnumValue(
Expand Down Expand Up @@ -1468,10 +1491,13 @@ export class GraphQLInputObjectType {
toString(): string {
return this.name;
}

// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLInputObjectType';
}
}

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

function defineInputFieldMap(
Expand Down
9 changes: 6 additions & 3 deletions src/type/directives.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// @flow strict

import objectEntries from '../polyfills/objectEntries';
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';

import inspect from '../jsutils/inspect';
import toObjMap from '../jsutils/toObjMap';
import devAssert from '../jsutils/devAssert';
import instanceOf from '../jsutils/instanceOf';
import defineToJSON from '../jsutils/defineToJSON';
import isObjectLike from '../jsutils/isObjectLike';
import defineToStringTag from '../jsutils/defineToStringTag';
import {
type ReadOnlyObjMap,
type ReadOnlyObjMapLike,
Expand Down Expand Up @@ -111,10 +111,13 @@ export class GraphQLDirective {
toString(): string {
return '@' + this.name;
}

// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLDirective';
}
}

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

export type GraphQLDirectiveConfig = {|
Expand Down
10 changes: 6 additions & 4 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import find from '../polyfills/find';
import objectValues from '../polyfills/objectValues';
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';

import inspect from '../jsutils/inspect';
import toObjMap from '../jsutils/toObjMap';
import devAssert from '../jsutils/devAssert';
import instanceOf from '../jsutils/instanceOf';
import isObjectLike from '../jsutils/isObjectLike';
import defineToStringTag from '../jsutils/defineToStringTag';
import {
type ObjMap,
type ReadOnlyObjMap,
Expand Down Expand Up @@ -283,10 +283,12 @@ export class GraphQLSchema {
assumeValid: this.__validationErrors !== undefined,
};
}
}

// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
defineToStringTag(GraphQLSchema);
// $FlowFixMe Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLSchema';
}
}

type TypeMap = ObjMap<GraphQLNamedType>;

Expand Down