liveBook · Manning

Graphql schema definition language

As explained in Contract Testing in Action

The GraphQL schema definition language is a declarative language for defining the types and fields exposed by a GraphQL API.

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

How Contract Testing in Action covers it

How deep it goes
Defines Movie and Query types and separates schema structure from resolver behavior, without detailing resolver internals.
Examples
Uses a Movie type, movie and movies queries, MovieAPI data-source methods, and an undeclared rating field that causes query failure.
What you can do after
Build it from scratch by declaring SDL types, attaching resolvers, and configuring them in an Apollo GraphQL server.
How it connects
Connects GraphQL queries with resolvers, data sources, REST APIs, Apollo Server, and contract testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Contract Testing in Action. The section links point to the parts of the book it draws on.