Schema
What is it
Schema is a contract/promise between the server and client when exchanging data.
Schema defines a collection of types and the relationships among themeselves.
- Unified schema in GraphQL allows
clientsto see exactly what data is available and then request a specific subset of the data with a single optimized query.
type Starship { id: ID! name: String! # length takes argument of unit w/ default valueof METER length(unit: LengthUnit = METER): Float }
Root Types (Entry Points)
There are two types that are special within a schema:
schema { query: Query mutation: Mutation }
Every GraphQL service always have a query type.
- may or may not have a
mutationtype.
These types are the same as a regular object type, but they are special because they define the entry point of every GraphQL queries and mutations.
Type Definitions
typeDefs is synonymous with schema.
- contains the
schemadefinitions written asSDL(Schema Definition Language).
typeDefs can be an array of strings.
- Each string is a GraphQL
schemadefinition.
Apollo Server will merge all the typeDefs together when starting the server.
Template Literal
You can define schema/typeDefs with a template literal with graphql-tag.
Apollo server also provides gql from @apollo/server.
const gql = require("graphql-tag"); const typeDefs = gql` # Schema definitions go here `; module.exports = typeDefs;