Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Aug 25, 2024
1 parent 480613c commit d73a391
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/execution/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ function coerceVariableValues(

const { name: varName, type: varType } = varSignature;
if (!Object.hasOwn(inputs, varName)) {
if (varSignature.hasDefaultValue) {
coercedValues[varName] = varSignature.getDefaultValue();
if (varDefNode.defaultValue) {
coercedValues[varName] = varSignature.defaultValue;
} else if (isNonNullType(varType)) {
const varTypeStr = inspect(varType);
onError(
Expand Down
41 changes: 8 additions & 33 deletions src/utilities/getVariableSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,18 @@ import { print } from '../language/printer.js';
import type { GraphQLInputType, GraphQLSchema } from '../type/index.js';
import { isInputType } from '../type/index.js';

import type { ConstValueNode, VariableDefinitionNode } from '../index.js';
import type { VariableDefinitionNode } from '../index.js';

import { typeFromAST } from './typeFromAST.js';
import { valueFromAST } from './valueFromAST.js';

/**
* A GraphQLVariableSignature is required to coerce a variable value.
*
* @internal
* */
export class GraphQLVariableSignature {
export interface GraphQLVariableSignature {
name: string;
type: GraphQLInputType;
hasDefaultValue: boolean;
_defaultValue: unknown;

constructor(
name: string,
type: GraphQLInputType,
defaultValueNode: ConstValueNode | undefined,
) {
this.name = name;
this.type = type;
if (defaultValueNode) {
this.hasDefaultValue = true;
this._defaultValue = () => valueFromAST(defaultValueNode, type);
} else {
this.hasDefaultValue = false;
}
}

getDefaultValue(): unknown {
if (typeof this._defaultValue === 'function') {
this._defaultValue = this._defaultValue();
}
return this._defaultValue;
}
defaultValue: unknown;
}

export function getVariableSignature(
Expand All @@ -61,9 +36,9 @@ export function getVariableSignature(
);
}

return new GraphQLVariableSignature(
varName,
varType,
varDefNode.defaultValue,
);
return {
name: varName,
type: varType,
defaultValue: valueFromAST(varDefNode.defaultValue, varType),
};
}

0 comments on commit d73a391

Please sign in to comment.