0
0
GraphQLquery~10 mins

Why schema defines the API contract in GraphQL - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why schema defines the API contract
Client sends query
Schema validates query
Schema defines types & fields
Server executes query
Response matches schema
Client receives data as promised
The schema acts like a promise between client and server, defining exactly what data can be asked for and what will be returned.
Execution Sample
GraphQL
type Query {
  book(id: ID!): Book
}
type Book {
  id: ID!
  title: String
}
This schema defines a query to get a book by ID, specifying the fields the client can request.
Execution Table
StepActionInput/QuerySchema RoleResult/Output
1Client sends query{ book(id: "1") { id title } }Schema defines 'book' query and 'Book' typeQuery accepted, fields 'id' and 'title' valid
2Schema validates queryCheck if 'book' exists and fields requested are validSchema confirms 'book' and fields existValidation passes
3Server executes queryFetch book with id=1Schema guides data shapeData fetched: { id: "1", title: "GraphQL Basics" }
4Response sent to clientData matches schema shapeSchema ensures response matches contractClient receives { book: { id: "1", title: "GraphQL Basics" } }
5Client uses dataData matches expected types and fieldsSchema contract fulfilledClient can safely use data
6Query with invalid field{ book(id: "1") { id author } }Schema rejects unknown field 'author'Error returned: Field 'author' not found on type 'Book'
7ExitNo further processingSchema enforces contractExecution stops due to validation error
💡 Execution stops when query fields do not match schema, ensuring API contract is enforced.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
querynull{ book(id: "1") { id title } }ValidatedExecutedResponse formedUsed by client
schemaDefinedUsed to validate queryUsed to check fieldsUsed to shape dataEnsures response matchesEnforces API contract
responsenullnullnull{ id: "1", title: "GraphQL Basics" }Sent to clientReceived and used
Key Moments - 2 Insights
Why does the schema reject a query with a field not defined in it?
Because the schema defines exactly which fields exist, any field outside this is invalid, as shown in execution_table row 6 where 'author' is not in 'Book' type.
How does the schema help the client know what data to expect?
The schema specifies the types and fields, so the client can trust the response shape, as seen in rows 4 and 5 where the response matches the schema.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
AThe client sends the query to the server
BThe server executes the query and fetches data shaped by the schema
CThe schema rejects the query due to invalid fields
DThe client receives the response
💡 Hint
Refer to execution_table row 3 where the server fetches data guided by the schema
At which step does the schema reject a query with an invalid field?
AStep 2
BStep 4
CStep 6
DStep 5
💡 Hint
Check execution_table row 6 where the invalid field 'author' causes rejection
If the schema did not define the 'book' query, what would happen when the client sends a query for it?
AThe schema would reject the query during validation
BThe query would be accepted and executed
CThe server would return empty data
DThe client would receive data with null fields
💡 Hint
Schema validation (rows 1-2) ensures only defined queries are accepted
Concept Snapshot
GraphQL schema defines the API contract.
It specifies queries, types, and fields.
Queries must match schema to be valid.
Schema ensures server response matches client expectations.
Invalid queries are rejected before execution.
This guarantees reliable communication.
Full Transcript
In GraphQL, the schema acts as a contract between the client and server. When a client sends a query, the schema first validates it to ensure the requested fields and types exist. If valid, the server executes the query and returns data shaped exactly as the schema defines. This process ensures the client receives data it expects and can safely use. If the query contains fields not in the schema, it is rejected immediately, preventing errors. This contract keeps communication clear and reliable.