Querying Interface Fields
We will look at how we can query interface fields directly from a GraphQL API, and how the server will return the appropriate fields based on the type of the queried object.
For the below example, we will use the following GraphQL playground to query the API from.
GraphQL playground –
https://studio.apollographql.com/public/countries/variant/current/explorer
Example –
In the below example, we will query the common fields such as name, and country, and will use inline fragments to query the specific field, native from the Country interface.
query {
country(code: "BR") {
name
currency
... on Country {
native
}
}
}
Output:
Interfaces in GraphQL Schema
GraphQL is a powerful open-source query language for APIs. It is known for its flexibility and efficiency in fetching data from server endpoints. Unlike traditional REST APIs, which often have multiple endpoints for different resources, GraphQL typically exposes a single endpoint that allows clients to request exactly the data they need.
Interfaces in GraphQL help create a common set of fields and types that can be reused by other types or Queries. In this article, we will learn about the interfaces in GraphQL Schema.