-
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.
feat: add experimental support for parsing fragment arguments (#4015)
This is a rebase of #3847 This implements execution of Fragment Arguments, and more specifically visiting, parsing and printing of fragment-spreads with arguments and fragment definitions with variables, as described by the spec changes in graphql/graphql-spec#1081. There are a few amendments in terms of execution and keying the fragment-spreads, these are reflected in mjmahone/graphql-spec#3 The purpose is to be able to independently review all the moving parts, the stacked PR's will contain mentions of open feedback that was present at the time. - [execution changes](JoviDeCroock#2) - [TypeInfo & validation changes](JoviDeCroock#4) - [validation changes in isolation](JoviDeCroock#5) CC @mjmahone the original author --------- Co-authored-by: mjmahone <mahoney.mattj@gmail.com> Co-authored-by: Yaacov Rydzinski <yaacovCR@gmail.com>
- Loading branch information
1 parent
426b017
commit 157aada
Showing
41 changed files
with
2,236 additions
and
247 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,46 @@ | ||
import { GraphQLError } from '../error/GraphQLError.js'; | ||
|
||
import type { VariableDefinitionNode } from '../language/ast.js'; | ||
import { print } from '../language/printer.js'; | ||
|
||
import { isInputType } from '../type/definition.js'; | ||
import type { GraphQLInputType, GraphQLSchema } from '../type/index.js'; | ||
|
||
import { typeFromAST } from '../utilities/typeFromAST.js'; | ||
import { valueFromAST } from '../utilities/valueFromAST.js'; | ||
|
||
/** | ||
* A GraphQLVariableSignature is required to coerce a variable value. | ||
* | ||
* Designed to have comparable interface to GraphQLArgument so that | ||
* getArgumentValues() can be reused for fragment arguments. | ||
* */ | ||
export interface GraphQLVariableSignature { | ||
name: string; | ||
type: GraphQLInputType; | ||
defaultValue: unknown; | ||
} | ||
|
||
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 { | ||
name: varName, | ||
type: varType, | ||
defaultValue: valueFromAST(varDefNode.defaultValue, varType), | ||
}; | ||
} |
Oops, something went wrong.