Repository navigation
docs: add type inference with TypedDocument for variableValues - #2560
Conversation
|
This will check for nested properties in GQL types, such as those used in patch operations, and it will report an error for additional properties in the variableValues, as it should because having additional properties not required by the query results in a GQL error. E.g. Object literal may only specify known properties, and 'a' does not exist in type `SomeGqlType`
|
Added another commit, now it properly check for nested properties in the Example: The query, just for completeness: const q = graphql(`
mutation Patch($rowId: Int!, $sliderPatch: sliderPatch!) {
updateSliderByRowId(
input: {sliderPatch: $sliderPatch, rowId: $rowId}
) {
clientMutationId
slider {
enDescription
esDescription
id
image
rowId
url
}
}
}
`)Here is where it gets better reporting, ignoring the first argument of const res = await executeDocument({}, q, {
sliderPatch:
{ enDescription, esDescription, image, url, a: "" },
// highlights ^ `a` and reports error!
rowId
})Error reported: Object literal may only specify known properties, and 'a' does not exist in type '{ url?: string | null | undefined; esDescription?: string | null | undefined; enDescription?: string | null | undefined; image?: string | null | undefined; }' |
Necessary for proper inference of ExecutionResult
| export async function executeDocument<TData = any, TVariables = any>( | ||
| export async function executeDocument<TDoc extends TypedDocumentNode<TData, TVariables>, TData = any, TVariables = any>( | ||
| requestContext: Partial<Grafast.RequestContext>, | ||
| document: DocumentNode | TypedDocumentNode<TData, TVariables>, | ||
| variableValues?: Record<string, unknown> | null, | ||
| document: TDoc, | ||
| variableValues?: VariablesOf<typeof document>, | ||
| operationName?: string, | ||
| ): Promise<ExecutionResult<TData, TVariables>> { | ||
| ): Promise<ExecutionResult<ResultOf<typeof document>, TVariables>> { |
There was a problem hiding this comment.
I don't think we need generics for TData/TVariables, maybe something like this would work:
export async function executeDocument<TDoc extends TypedDocumentNode | DocumentNode, TExtensions = ObjMap<unknown>>(
requestContext: Partial<Grafast.RequestContext>,
document: TDoc,
variableValues?: VariablesOf<TDoc>,
operationName?: string,
): Promise<ExecutionResult<ResultOf<TDoc>, TExtensions>> {(Also the second parameter to ExecutionResult is TExtensions not TVariables - I recognize this was already incorrect.)
|
Hi @Sleepful, gentle nudge to see if you're interested in the above change? |
|
Thanks for taking care of the diff and merging it 💙 |
Description
Better type inference, I am not a fan of
as anybut that's what it takes for now, and allowsvariableValuesto be typed-checked atexecuteDocumentcallsite