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

use Symbol.hasInstance for looser instanceof checks #989

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 18 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import invariant from '../jsutils/invariant';
import isNullish from '../jsutils/isNullish';
import * as Kind from '../language/kinds';

import { assertValidName } from '../utilities/assertValidName';
import attachHasInstanceSymbol from '../utilities/attachHasInstanceSymbol';
import type {
ScalarTypeDefinitionNode,
ObjectTypeDefinitionNode,
Expand Down Expand Up @@ -368,6 +370,8 @@ export class GraphQLScalarType {
inspect: () => string;
}

attachHasInstanceSymbol(GraphQLScalarType);

// Also provide toJSON and inspect aliases for toString.
GraphQLScalarType.prototype.toJSON =
GraphQLScalarType.prototype.inspect =
Expand Down Expand Up @@ -468,6 +472,8 @@ export class GraphQLObjectType {
inspect: () => string;
}

attachHasInstanceSymbol(GraphQLObjectType);

// Also provide toJSON and inspect aliases for toString.
GraphQLObjectType.prototype.toJSON =
GraphQLObjectType.prototype.inspect =
Expand Down Expand Up @@ -749,6 +755,8 @@ export class GraphQLInterfaceType {
inspect: () => string;
}

attachHasInstanceSymbol(GraphQLInterfaceType);

// Also provide toJSON and inspect aliases for toString.
GraphQLInterfaceType.prototype.toJSON =
GraphQLInterfaceType.prototype.inspect =
Expand Down Expand Up @@ -831,6 +839,8 @@ export class GraphQLUnionType {
inspect: () => string;
}

attachHasInstanceSymbol(GraphQLUnionType);

// Also provide toJSON and inspect aliases for toString.
GraphQLUnionType.prototype.toJSON =
GraphQLUnionType.prototype.inspect =
Expand Down Expand Up @@ -999,6 +1009,8 @@ export class GraphQLEnumType/* <T> */ {
inspect: () => string;
}

attachHasInstanceSymbol(GraphQLEnumType);

// Also provide toJSON and inspect aliases for toString.
GraphQLEnumType.prototype.toJSON =
GraphQLEnumType.prototype.inspect =
Expand Down Expand Up @@ -1159,6 +1171,8 @@ export class GraphQLInputObjectType {
inspect: () => string;
}

attachHasInstanceSymbol(GraphQLInputObjectType);

// Also provide toJSON and inspect aliases for toString.
GraphQLInputObjectType.prototype.toJSON =
GraphQLInputObjectType.prototype.inspect =
Expand Down Expand Up @@ -1233,6 +1247,8 @@ export class GraphQLList<T: GraphQLType> {
inspect: () => string;
}

attachHasInstanceSymbol(GraphQLList);

// Also provide toJSON and inspect aliases for toString.
GraphQLList.prototype.toJSON =
GraphQLList.prototype.inspect =
Expand Down Expand Up @@ -1279,6 +1295,8 @@ export class GraphQLNonNull<T: GraphQLNullableType> {
inspect: () => string;
}

attachHasInstanceSymbol(GraphQLNonNull);

// Also provide toJSON and inspect aliases for toString.
GraphQLNonNull.prototype.toJSON =
GraphQLNonNull.prototype.inspect =
Expand Down
3 changes: 3 additions & 0 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { GraphQLDirective, specifiedDirectives } from './directives';
import { __Schema } from './introspection';
import find from '../jsutils/find';
import invariant from '../jsutils/invariant';
import attachHasInstanceSymbol from '../utilities/attachHasInstanceSymbol';
import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators';


Expand Down Expand Up @@ -222,6 +223,8 @@ export class GraphQLSchema {
}
}

attachHasInstanceSymbol(GraphQLSchema);

type TypeMap = { [typeName: string]: GraphQLNamedType };

type GraphQLSchemaConfig = {
Expand Down
18 changes: 18 additions & 0 deletions src/utilities/__tests__/attachHasInstanceSymbol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, it} from 'mocha';
import chai from 'chai';
import attachHasInstanceSymbol from '../attachHasInstanceSymbol';
import { GraphQLInputObjectType as RealInputType } from '../../type';


describe('attachHasInstanceSymbol()', () => {
it('passes instanceof checks for types for other package instances', () => {
class GraphQLInputObjectType {
constructor() {}
}

attachHasInstanceSymbol(GraphQLInputObjectType);

chai.expect(new GraphQLInputObjectType() instanceof RealInputType)
.to.equal(true);
});
});
25 changes: 25 additions & 0 deletions src/utilities/attachHasInstanceSymbol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

// eslint-disable-next-line flowtype/no-weak-types
export default function attachHasInstanceSymbol(ctor: Function): void {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what a better type here would be maybe GraphQLType?

if (typeof Symbol === 'undefined' || !Symbol.for || !Symbol.hasInstance) {
return;
}
const tag = `@@typeof/${ctor.name}`;

Object.defineProperty(ctor, Symbol.hasInstance, {
value: function $hasInstance(instance) {
return instance && instance[Symbol.for(tag)] === true;
},
});

ctor.prototype[Symbol.for(tag)] = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Symbol.for() and not Symbol()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one fetches a global symbol by name vs creating a new symbol:

Symbol('foo') == Symbol('foo') // false
Symbol.for('foo') == Symbol.for('foo') // true

Copy link

@felixfbecker felixfbecker Oct 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, but why is that desirable here? Imo the symbol should be private or if it's public, it should be exported, no retrievable for everyone through a magic string. You just need to save the symbol in a variable and reuse it. Otherwise you might as well just use a regular property.

Copy link
Author

@jquense jquense Oct 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not using a symbol for privacy, it's to avoid muddying up people's namespace on the class for things they can use as property names. With the symbol you don't have to worry (nearly as much) about accidentally overwriting the tag in an instance when you just wanted to define an unrelated property. Incidentally this approach is how React tags elements as "ReactElements"

}