Reputation: 2254
I'm new to GraphQL and was curious if I could use it to query an external, third-part API for data. I have an express backend and a react frontend and I'm sending a GET request to a third party API for the price of precious metals.
The API responds with one big object that is filled with things I don't need but I still get via HTTP. For example, API is returning this:
{
"gold_bid_usd_toz": "1303.58",
"gold_ask_usd_toz": "1304.58",
"gold_change_dollar_usd_toz": "1.23",
"gold_change_percent_usd_toz": "0.09%",
"gold_high_usd_toz": "1306.51",
"gold_low_usd_toz": "1299.85",
"gold_londonfix_am": "1320.7",
"gold_londonfix_pm": "1319.92",
"silver_bid_usd_toz": "16.5",
"silver_ask_usd_toz": "16.6",
"silver_change_dollar_usd_toz": "-0.02",
"silver_change_percent_usd_toz": "-0.13%",
"silver_high_usd_toz": "16.6",
"silver_low_usd_toz": "16.46",
"silver_londonfix": "16.6",
...LOTS MORE...
}
If I'm only interested in two of the properties, can I do something like this:
query {
gold_bid_usd_toz
silver_bid_usd_toz
}
Just not sure if I have to control backend data in order to use GraphQL. Thanks.
Upvotes: 1
Views: 1810
Reputation: 156
You can return your request response directly from a resolver as it is.
Then in a frontend query matching this resolver, get only what you asked for.
Backend:
rootQuery {
resolverName: () => getThirdParty
}
Frontend:
query {
resolverName {
whatYouWant1
whatYouWant2
}
}
Both your query and resolver should match your graphql schema but not the third party response.
Upvotes: 4