You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The generated code for typescript-graphql-request contains a method for each Query or Mutation. The response of these methods contain the result wrapped in a component with the same name as the operation.
For example, the response for a GetProductQuery would be the following:
The content of "getProduct" is the object type "Product". With a middleware its easy to extract always the first object key (i.e. "getProduct") and return this object instead of the original response. However, then we loose the type-safety from the generated methods.
In case of Create or Update Mutations, the difference is if I already have an ID (then its an update) or not (then its a create). However, both options return a "Product" object wrapped in their mutation name:
constsaveProductMutation=async(product: Product)=>{constvariables: CreateProductInput|UpdateProductInput={name: product.name,sku: product.sku,};constresult=isEmpty(product.id)
? awaitgraphql.CreateProduct({input: { ...variables}})
: awaitgraphql.UpdateProduct({input: { ...variables,id: product.id}});// the result contains either "createProduct" or "updateProduct" which both contain the type "Product"return(resultasCreateProductMutation).createProduct||(resultasUpdateProductMutation).updateProduct;};
Instead it would be nice if both CreateProductand UpdateProduct return a Product object.
I'm not an expert in Typescript type definitions. Would it be possible to write a SDK wrapper function that automatically extracts the first component from the resulting type?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The generated code for
typescript-graphql-request
contains a method for each Query or Mutation. The response of these methods contain the result wrapped in a component with the same name as the operation.For example, the response for a
GetProductQuery
would be the following:The content of "getProduct" is the object type "Product". With a middleware its easy to extract always the first object key (i.e. "getProduct") and return this object instead of the original response. However, then we loose the type-safety from the generated methods.
In case of Create or Update Mutations, the difference is if I already have an ID (then its an update) or not (then its a create). However, both options return a "Product" object wrapped in their mutation name:
Instead it would be nice if both
CreateProduct
andUpdateProduct
return aProduct
object.I'm not an expert in Typescript type definitions. Would it be possible to write a SDK wrapper function that automatically extracts the first component from the resulting type?
Beta Was this translation helpful? Give feedback.
All reactions