apollographql / apollo-rs

Spec compliant GraphQL Tools in Rust.
Apache License 2.0
566 stars 45 forks source link

question: How to get type name of FieldDefinition's Return Type? #818

Closed alexhancock closed 7 months ago

alexhancock commented 7 months ago

Description

I am wondering if I can get the type name of a FieldDefinition's return type?

For

type Merchant {
  id: ID
  name: String
  businessDetails: BusinessDetails
}

I see how I can get id, name, payments using this example from the docs.

And I see how I could get the names of all ObjectTypeDefinitions, but I am stumped on how to capture the association of field to return type, like the below (see ?????)

for def in doc.definitions() {
  if let cst::Definition::ObjectTypeDefinition(object_type) = def {
    for field_def in object_type.fields_definition().unwrap().field_definitions() {
      println!("{}", field_def.name().unwrap().text()) // === "businessDetails"
      println!("{}", ?????) // === "BusinessDetails"
    }
  }
}
SimonSapin commented 7 months ago

In apollo-parser, that would be the ty method: https://docs.rs/apollo-parser/latest/apollo_parser/cst/struct.FieldDefinition.html#method.ty

(It’s named that way because type is a Rust keyword and can’t be used as a name.)

However, consider using apollo-compiler instead as it provides more convenient APIs. Version 1.0 is still in beta (and doesn’t have as many up-to-date examples yet) but it’s what Apollo Router has been using for a while and where most of our focus is: https://docs.rs/apollo-compiler/1.0.0-beta.12/

SimonSapin commented 7 months ago

Closing as I believe the specific question was answered, and we’ll rework documentation as part of finishing up apollo-compiler 1.0. Feel free to open another issue or discussion.

alexhancock commented 7 months ago

@SimonSapin Sorry I forgot to reply before. apollo-compiler gave me what I needed - thank you!