-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce internal getVariableSignature utility
extracted from my fragment arguments scratch branch
- Loading branch information
Showing
2 changed files
with
77 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { GraphQLError } from '../error/GraphQLError.js'; | ||
|
||
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 { typeFromAST } from './typeFromAST.js'; | ||
import { valueFromAST } from './valueFromAST.js'; | ||
|
||
/** | ||
* A GraphQLVariableSignature is required to coerce a variable value. | ||
* | ||
* @internal | ||
* */ | ||
export class 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; | ||
} | ||
} | ||
|
||
export function getVariableSignature( | ||
schema: GraphQLSchema, | ||
varDefNode: VariableDefinitionNode, | ||
): GraphQLVariableSignature | GraphQLError { | ||
const varName = varDefNode.variable.name.value; | ||
const varType = typeFromAST(schema, varDefNode.type); | ||
|
||
if (!isInputType(varType)) { | ||
// Must use input types for variables. This should be caught during | ||
// validation, however is checked again here for safety. | ||
const varTypeStr = print(varDefNode.type); | ||
return new GraphQLError( | ||
`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`, | ||
{ nodes: varDefNode.type }, | ||
); | ||
} | ||
|
||
return new GraphQLVariableSignature( | ||
varName, | ||
varType, | ||
varDefNode.defaultValue, | ||
); | ||
} |