I’m encountering a problem when trying to access fields from the response of the apiQuery function. Specifically, when I try to access a field like response.data.editMeteringpointMutation.address, TypeScript raises an error saying Property ‘address’ does not exist on type, and I don’t see any other fields either.
Here’s how I call the function:
let response = await apiQuery('editMeteringpointMutation', {
...props.meteringpoint,
metering_point_id: props.meteringpoint.id,
});
However, when I call it with additional parameters as follows:
let response = await apiQuery(
'editMeteringpointMutation',
{
...props.meteringpoint,
metering_point_id: props.meteringpoint.id,
},
['address'],
true
);
Everything works fine. It also works correctly when I pass an array without the allow parameter, where it omits my address.
Why is my object empty when I don’t pass the fields or allow parameters?
Why do I expect it to be populated but it’s not?
The problem lies on Q
i guess
type NestedPick<T, K extends keyof any> = {
[P in keyof T]: Pick<T[P], Extract<K, keyof T[P]>>
};
type NestedOmit<T, K extends keyof any> = {
[P in keyof T]: Omit<T[P], Extract<K, keyof T[P]>>
};
export const apiQuery = async <
T extends keyof typeof mutations | keyof typeof queries,
Allow extends boolean = false,
V = T extends keyof typeof mutations
? (typeof mutations)[T]['__generatedMutationInput']
: T extends keyof typeof queries
? (typeof queries)[T]['__generatedQueryInput']
: never,
R = T extends keyof typeof mutations
? (typeof mutations)[T]['__generatedMutationOutput']
: T extends keyof typeof queries
? (typeof queries)[T]['__generatedQueryOutput']
: never,
P = T extends keyof typeof mutations
? (typeof mutations)[T]['__generatedMutationOutput'][keyof (typeof mutations)[T]['__generatedMutationOutput']]
: T extends keyof typeof queries
? (typeof queries)[T]['__generatedQueryOutput'][keyof (typeof queries)[T]['__generatedQueryOutput']]
: never,
K extends keyof P = keyof P,
Q = [K] extends [never]
? R
: Allow extends true
? NestedPick<R, K>
: NestedOmit<R, K>
>(
queryString: T,
variables: V,
fields?: K[],
allow?: Allow,
authMode?: GraphQLOptions['authMode']
) => {
// Function implementation
}
Screenshots:
When calling without fields or allow:
When calling with fields and allow:
Why is my object empty? I expect it to be populated