gosseti
gosseti

Reputation: 975

GraphQL alias top-level data into a nested object

Currently I’m querying the currentUser for a city.

query user {
  currentUser {
    city
  }
}

However the requirements of my front-end requires the city as an object containing label and value.

// this doesn’t work, but is the idea…
query user {
  currentUser {
    city {
      label: city
      value: city
    }
  }
}

Is this possible to do at the GraphQL query level?

Upvotes: 2

Views: 2691

Answers (1)

JV Lobo
JV Lobo

Reputation: 6316

I haven't seen your database model, but you could have a City model, something like:

type City {
  id: ID! @unique
  label: String
  value: String
}

Then, in your User model you could do something like this:

type User {
  id: ID! @unique
  .... other User fields
  city: City
}

With that schema, you would be able to query the city values inside an user:

query user {
  currentUser {
    city {
      label
      value
    }
  }
}

You should adapt that to your needs, but that's the main idea that I think you are looking for.

Upvotes: 2

Related Questions