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

Extract 'identityFunc' function to be used instead of 'x => x' #1945

Merged
merged 1 commit into from
Jun 2, 2019
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
23 changes: 23 additions & 0 deletions src/jsutils/__tests__/identityFunc-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import identityFunc from '../identityFunc';

describe('identityFunc', () => {
it('returns the first argument it receives', () => {
expect(identityFunc()).to.equal(undefined);
expect(identityFunc(undefined)).to.equal(undefined);
expect(identityFunc(null)).to.equal(null);

const obj = {};
expect(identityFunc(obj)).to.equal(obj);
});
});
15 changes: 15 additions & 0 deletions src/jsutils/identityFunc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

/**
* Returns the first argument it receives.
*/
export default function identityFunc<T>(x: T): T {
return x;
}
12 changes: 12 additions & 0 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';

import identityFunc from '../../jsutils/identityFunc';
import { valueFromASTUntyped } from '../../utilities/valueFromASTUntyped';
import {
type GraphQLType,
type GraphQLNullableType,
Expand Down Expand Up @@ -64,6 +66,16 @@ describe('Type System: Scalars', () => {
).not.to.throw();
});

it('provides default methods if omitted', () => {
const scalar = new GraphQLScalarType({
name: 'Foo',
serialize: () => null,
});

expect(scalar.parseValue).to.equal(identityFunc);
expect(scalar.parseLiteral).to.equal(valueFromASTUntyped);
});

it('rejects a Scalar type not defining serialize', () => {
expect(
// $DisableFlowOnNegativeTest
Expand Down
3 changes: 2 additions & 1 deletion src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import objectEntries from '../polyfills/objectEntries';
import defineToJSON from '../jsutils/defineToJSON';
import defineToStringTag from '../jsutils/defineToStringTag';
import identityFunc from '../jsutils/identityFunc';
import instanceOf from '../jsutils/instanceOf';
import inspect from '../jsutils/inspect';
import invariant from '../jsutils/invariant';
Expand Down Expand Up @@ -559,7 +560,7 @@ export class GraphQLScalarType {
this.name = config.name;
this.description = config.description;
this.serialize = config.serialize;
this.parseValue = config.parseValue || (value => value);
this.parseValue = config.parseValue || identityFunc;
this.parseLiteral = config.parseLiteral || valueFromASTUntyped;
this.astNode = config.astNode;
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import inspect from '../jsutils/inspect';
import invariant from '../jsutils/invariant';
import keyMap from '../jsutils/keyMap';
import keyValMap from '../jsutils/keyValMap';
import identityFunc from '../jsutils/identityFunc';
import { type ObjMap } from '../jsutils/ObjMap';
import { valueFromAST } from './valueFromAST';
import { assertValidSDL } from '../validation/validate';
Expand Down Expand Up @@ -399,7 +400,7 @@ export class ASTDefinitionBuilder {
name: astNode.name.value,
description: getDescription(astNode, this._options),
astNode,
serialize: value => value,
serialize: identityFunc,
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import objectValues from '../polyfills/objectValues';
import inspect from '../jsutils/inspect';
import invariant from '../jsutils/invariant';
import keyValMap from '../jsutils/keyValMap';
import identityFunc from '../jsutils/identityFunc';
import { valueFromAST } from './valueFromAST';
import { parseValue } from '../language/parser';
import {
Expand Down Expand Up @@ -230,7 +231,7 @@ export function buildClientSchema(
return new GraphQLScalarType({
name: scalarIntrospection.name,
description: scalarIntrospection.description,
serialize: value => value,
serialize: identityFunc,
});
}

Expand Down