A movie consumer may need several REST requests to gather related data, while GraphQL lets it request the needed structure through one query. SDL defines the fields that query may use. section 7.1
GraphQL schema definition language (SDL) is a declarative type language for defining the data exposed by a GraphQL API. The schema acts as a consumer contract: a Movie type can expose id, name, and year, while resolver functions separately determine how those values are fetched. section 7.1
A small GraphQL server connects SDL to data sources in a few distinct steps:
- Declare object types such as Movie and query fields such as movie(movieId: Int!) and movies. section 7.2.1
- Implement resolvers that call data-source methods for individual movies or movie collections. section 7.1 section 7.2.1
- Pass typeDefs and resolvers to ApolloServer, with MovieAPI available through the server context. section 7.2.1
const typeDefs = gql`
type Movie { #1
id: Int
name: String
year: Int
}
type Query { #2
movie(movieId: Int!): Movie
movies: [Movie]
}
`;
This schema listing shows how SDL combines object fields, a required movie identifier, and collection and single-item queries. section 7.2.1
Schema validation boundary
If a consumer adds rating to a movie query but rating is absent from the SDL, GraphQL rejects the query. Contract testing remains useful for observing consumer usage, bad output, and status-code differences such as 401 or 403. section 7.1
