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

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

Closed
alexhancock opened this issue Jan 30, 2024 · 3 comments
Closed
Labels
documentation Improvements or additions to documentation triage

Comments

@alexhancock
Copy link

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"
    }
  }
}
@alexhancock alexhancock added documentation Improvements or additions to documentation triage labels Jan 30, 2024
@SimonSapin
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Author

alexhancock commented Feb 13, 2024

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

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

No branches or pull requests

2 participants