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

Typesafe Access to Mutation Result Not Working #2708

Closed
freedomflyer opened this issue Oct 8, 2019 · 5 comments
Closed

Typesafe Access to Mutation Result Not Working #2708

freedomflyer opened this issue Oct 8, 2019 · 5 comments

Comments

@freedomflyer
Copy link

freedomflyer commented Oct 8, 2019

I may be missing something here, but I expect the data object returned by the mutation hook below to be built in such a way that I can easily access the other properties on the object, such as response.data.login.token, etc. Instead, when I try to access the login object, Typescript tells me that I'm trying to access an object that may be undefined.

Is this the expected behavior of the hook's results?

const response = await loginMutation({
  variables: {
    email: "tester@test.com",
    password: "abc",
  },
})

// Typescript no likey
console.log(response.data.login.token)

// need to use ternary or other check on response.data for Typescript to allow
// me to access login.token. Otherwise Typescript barfs.
console.log(response.data ? response.data.login.token : null)
@lukasluecke
Copy link

I guess that depends on your Schema - if the mutation result data is nullable, then it makes sense to generate "nullable" types as well. Most mutations can fail, and therefore might not always return a data object.

Once optional chaining lands in Typescript (3.7), this access should be a bit cleaner.

@freedomflyer
Copy link
Author

freedomflyer commented Oct 8, 2019

Yeah, I'm surprised this is happening since my schema marks the result as not nullable, which is why I'm reporting it here. Here's the schema:

type Mutation {
  signup(email: String!, password: String!, name: String!): AuthPayload!
  login(email: String!, password: String!): AuthPayload!
}

@lukasluecke
Copy link

Which plugin are you using to generate your hooks? I think it might have to do with the hooks return value including undefined due to possibly not being called / completed yet.

@maapteh
Copy link
Contributor

maapteh commented Oct 8, 2019

Hi, i think it has to do with the fact that in the past data was {}, since version three they made the update that data is initially undefined :) See apollographql/react-apollo#3388 So the TypeScript feedback is solid, data can be undefined :)

const [doLogin, { data, loading, error }] = loginMutation({
  variables: {
    email: "tester@test.com",
    password: "abc",
  },
})

if (loading) { return null; }
if (data) {

}

@dotansimha
Copy link
Owner

@maapteh is right, Apollo changed the way they pass data, and now it's optional, so the data (which is not coming from the generated output, but from apollo-client) could be undefined now. So you need to check for that before using it.

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

No branches or pull requests

4 participants