Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Casting API.graphql #33

Closed
ajhool opened this issue Jul 15, 2019 · 3 comments
Closed

Type Casting API.graphql #33

ajhool opened this issue Jul 15, 2019 · 3 comments
Labels

Comments

@ajhool
Copy link

ajhool commented Jul 15, 2019

Is your feature request related to a problem? Please describe.
The API.ts codegen is very useful for inputs, but is not as useful for typing results of API.graphql(graphqlOperation(createSomeObject, {})). API.grapqhl returns a GraphQLResult or an Observable -- but it isn't able to tell which one it will return and neither are generic, which causes typing challenges.

Describe the solution you'd like

import { CreateSomeObjectMutation, OnCreateSomeObject } from './API';
const result = API.graphql<Mutation, CreateSomeObjectMutation>(graphqlOperation(createSomeObject, {...})) 

// None of the below code should throw a type error. It should expect the object to have this shape.
if(result.data) {
  result.data.createSomeObject
} else {
  console.log(result.errors);
}
const { fieldA } = result.data.createSomeObject;

const observableResult = API.graphql<Subscription, OnCreateSomeObject>(graphqlOperation(onCreateSomeObject, {...})) 

observableResult.then(....???... )

Describe alternatives you've considered
Type casting the result. This works but is less declarative IMO and requires more code.

Additional context

@haverchuck haverchuck transferred this issue from aws-amplify/amplify-js Jul 15, 2019
@dopry
Copy link

dopry commented Jan 18, 2021

const response: GraphQLResult<CreateTenantMutation> = await API.graphql(options) as GraphQLResult<CreateTenantMutation>
seems to work for me as an interim fix, but it would me much nicer as a generic

@AaronZyLee AaronZyLee transferred this issue from aws-amplify/amplify-cli Jan 27, 2021
@AaronZyLee AaronZyLee added feature-request New feature or request type-gen Issues on graphql-type-generator labels Jan 27, 2021
@AaronZyLee
Copy link
Contributor

We do not have any API.graphql code generation in amplify codegen except for angular, where we do have a type casting for each graphql operation. Could you provide more contexts about how the codegen can help you on your use case? For now this looks like an issue for amplify-js library. If this is the case, I will ask js developers for help.

@AaronZyLee AaronZyLee removed the type-gen Issues on graphql-type-generator label Mar 11, 2021
@phani-srikar
Copy link
Contributor

Closing this due to inactivity. Please re-open if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants