graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Unsure how to troubleshoot "Missing __typename in selection for the <something> interface" error

Open colindean opened this issue 6 years ago • 3 comments

I'm trying out this library and GraphQL for the first time. My usage of graphql-rust is here and the schema is here.

$ cargo test
   Compiling ledger-getquote-blocktap v0.1.0 (/Users/colin/Source/Altangent/ledger-getquote-blocktap)
error: proc-macro derive panicked
 --> src/single_currency.rs:1:10
  |
1 | #[derive(GraphQLQuery)]
  |          ^^^^^^^^^^^^
  |
  = help: message: called `Result::unwrap()` on an `Err` value: ErrorMessage { msg: "Missing __typename in selection for the SingleCurrencyMarkets interface (type: Market)" }

error: aborting due to previous error

or using the CLI tool

RUST_BACKTRACE=1 graphql-client generate schema/single.graphql schema/blocktap.json lol what
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ErrorMessage { msg: "Missing __typename in selection for the SingleCurrencyMarkets interface (type: Market)" }

I don't really understand what's missing or how to figure it out. If it's something missing from the JSON schema, some kind of path to the expected but missing property would really expedite this.

colindean avatar Jan 19 '19 06:01 colindean

Thanks for using the library and your report, it's an ergonomics problem indeed.

For code generation to work, you need to query for the __typename field (which is guaranteed to be there on all spec-compliant graphql servers, most frameworks implement it automatically) when you select fields on unions or interfaces.

In your case Market is an interface, so this would be solved by adding __typename right before (or after) ticker here.

I will keep this issue open because we should improve the error message, and suggest where to add __typename in the query.

tomhoule avatar Jan 19 '19 08:01 tomhoule

Looks like that was exactly what I needed: https://github.com/colindean/ledger-getquote-blocktap/commit/5bb8eeec7530cddcf263a3de13375f61e0906749

Thank you!

colindean avatar Jan 19 '19 08:01 colindean

FWIW I came across the same issue. Changing

query RepoUrl($id: ID!) {
  node(id: $id) {
    ... on Repository {
      url
    }
  }
}

to

query RepoUrl($id: ID!) {
  node(id: $id) {
    __typename
    ... on Repository {
      url
    }
  }
}

did the trick. Perhaps a more detailed error message would help out here.

samuela avatar Feb 22 '20 07:02 samuela