Introduction
A schema in GraphQL clearly shows what data can be asked for and what will be given back. It acts like a promise between the server and the user.
A schema in GraphQL clearly shows what data can be asked for and what will be given back. It acts like a promise between the server and the user.
type TypeName {
fieldName: FieldType
anotherField: AnotherType
}
schema {
query: QueryType
mutation: MutationType
}type Book {
title: String
author: String
}
type Query {
books: [Book]
}type User {
id: ID!
name: String!
email: String
}
type Query {
user(id: ID!): User
}This simple schema defines a query called 'greeting' that returns a string.
type Query {
greeting: String
}
schema {
query: Query
}The schema helps tools and developers understand what data is available.
Changing the schema means changing the contract, so be careful to keep it consistent.
The schema is like a menu showing what data you can order from the API.
It makes sure everyone agrees on what data is possible and how to ask for it.
This agreement helps avoid errors and makes building apps easier.
type Query { user(id: Int): User }{ user(id: 1) { id name } } but the schema does not define the name field on User?type Query { getUser(id: Int): User }{ getUser(id: 5) { id name } }. What is the likely mistake?Book with fields id, title, and author. How does defining this in the schema help your API users?Pass the test and this pattern moves unknown