#GraphQL #GraphQLSchema #WebDevelopment #API #BackendDevelopment #SchemaDesign #SoftwareDevelopment #Programming #Developer #TechTips #WebTech #APIDesign #LearnGraphQL #GraphQLTutorial #Coding #JavaScript #APIs #TechSkills #TechTrends #WebDev
Understanding GraphQL Schema
A GraphQL schema is a central part of any GraphQL server implementation. It defines the types of data that can be queried and manipulated through the GraphQL API and outlines the structure of queries, mutations, and subscriptions that clients can execute.
Key Concepts
Types and Fields:
Types: Represent the shape of the objects you can fetch from your GraphQL server. Common types include object types, scalar types, enums, interfaces, and unions.
Fields: Represent the properties of these objects and can reference other types or scalar values.
Root Types:
Query: Defines the read operations that clients can execute to fetch data.
Mutation: Defines the write operations that clients can execute to modify data.
Subscription: Defines real-time operations that clients can subscribe to for updates.
Scalar Types:
The basic data types in GraphQL, such as Int, Float, String, Boolean, and ID.
Object Types:
Custom types defined in the schema that represent complex data structures, composed of fields that can be scalars or other object types.
Non-Null and List Types:
Use ! to denote non-nullable fields.
Use [] to denote lists of types.
Example Schema
Here's an example GraphQL schema defined using the GraphQL Schema Definition Language (SDL):
Define a custom scalar type
scalar Date
Define an object type
type Book {
id: ID!
title: String!
author: Author!
publishedDate: Date
}
Define another object type
type Author {
id: ID!
name: String!
books: [Book!]!
}
Define the root Query type
type Query {
books: [Book!]!
book(id: ID!): Book
authors: [Author!]!
author(id: ID!): Author
}
Define the root Mutation type
type Mutation {
addBook(title: String!, authorId: ID!): Book!
updateBook(id: ID!, title: String, authorId: ID, publishedDate: Date): Book
deleteBook(id: ID!): Book
}
Define the root Subscription type
type Subscription {
bookAdded: Book!
}
Detailed Breakdown
Scalar Types:
Basic types: Int, Float, String, Boolean, ID.
Custom scalar example: Date.
Object Types:
Book and Author are object types. Each has fields that can be other object types or scalar types.
The Book type has fields like id, title, author, and publishedDate.
The Author type has fields like id, name, and books.
Root Query Type:
Defines entry points for reading data.
Example queries: fetching all books, a specific book by ID, all authors, and a specific author by ID.
Root Mutation Type:
Defines entry points for modifying data.
Example mutations: adding a new book, updating an existing book, and deleting a book.
Root Subscription Type:
Defines entry points for real-time updates.
Example subscription: listening for new books being added.
Implementing the Schema in Code
Using Apollo Server, you can define the above schema in your code:
Step 1: Set Up Your Project
Create a New Directory for Your Project:
mkdir my-graphql-server
cd my-graphql-server
Initialize a New Node.js Project:
npm init -y
Install Dependencies:
npm install apollo-server graphql
Step 2: Define Your Schema and Resolvers
Create a File Named schema.js:
Define your GraphQL schema using SDL.
java script Code :
const { gql } = require('apollo-server');
const typeDefs = gql`
scalar Date
type Book {
id: ID!
title: String!
author: Author!
publishedDate: Date
}
type Author {
id: ID!
name: String!
books: [Book!]!
}
type Query {
books: [Book!]!
book(id: ID!): Book
authors: [Author!]!
author(id: ID!): Author
}
type Mutation {
addBook(title: String!, authorId: ID!): Book!
updateBook(id: ID!, title: String, authorId: ID, publishedDate: Date): Book
deleteBook(id: ID!): Book
}
type Subscription {
bookAdded: Book!
}
`;
module.exports = typeDefs;
Create a File Named resolvers.js:
Implement the resolver functions that fetch and manipulate the data for your schema.
Chapter :
00:00 Understanding GraphQL Schema
00:31 Key Concepts
02:15 Example Schema
02:45 Implementing the Schema in Code & Setup Apollo Server
04:25 Summery
Thank you for watching this video
Everyday Be coding