Welcome back, fellow developers! Today, we're going to explore one of the most powerful features in GraphQL: the GraphQL Schema. By the end of this lesson, you'll have a solid understanding of how to create, use, and extend GraphQL schemas in your Node.js projects. Let's dive in!
A GraphQL Schema is a description of the types, queries, and mutations available in a GraphQL API. It acts as a contract between the client and the server, outlining what data can be requested and how it can be structured.
To work with GraphQL in Node.js, we'll be using a popular library called apollo-server.
First, let's install the apollo-server package:
npm install apollo-serverNow, let's create a basic schema that defines a Query type with a single field, hello.
const { gql, ApolloServer } = require('apollo-server');
// Define our schema
const typeDefs = gql`
type Query {
hello: String
}
`;
// Define our resolvers
const resolvers = {
Query: {
hello: () => 'Hello, world!',
},
};
// Create the Apollo Server
const server = new ApolloServer({ typeDefs, resolvers });
// Start the server
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});Now, if you run the script above, you'll have a simple GraphQL API available at http://localhost:4000. You can query the hello field directly in the browser:
http://localhost:4000/graphql?query={hello}Result:
{
"data": {
"hello": "Hello, world!"
}
}
What is the purpose of a GraphQL Schema?
Let's expand our schema to include a Person type with fields for name, age, and favorite color. We'll also add a Query field, person, that allows clients to retrieve a single Person by ID.
const { gql, ApolloServer } = require('apollo-server');
// Define our schema
const typeDefs = gql`
type Person {
id: ID!
name: String!
age: Int!
favoriteColor: String
}
type Query {
person(id: ID!): Person
}
`;
// Define our resolvers
const resolvers = {
Person: {
id: (person) => person.id,
name: (person) => person.name,
age: (person) => person.age,
favoriteColor: (person) => person.favoriteColor,
},
Query: {
person: (parent, args) => {
// Find the person by ID
// ...
return {
id: '123',
name: 'John Doe',
age: 30,
favoriteColor: 'Blue',
};
},
},
};
// Create the Apollo Server
const server = new ApolloServer({ typeDefs, resolvers });
// Start the server
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});Now, you can query the person field with a specific ID:
http://localhost:4000/graphql?query={person(id: "123"){name, age, favoriteColor}}Result:
{
"data": {
"person": {
"name": "John Doe",
"age": 30,
"favoriteColor": "Blue"
}
}
}
How would you query the `age` field of the `person` with ID `123`?
In this lesson, we've covered the basics of GraphQL Schemas and learned how to create and expand them in Node.js using the apollo-server library. By understanding the power and flexibility of GraphQL Schemas, you'll be well-equipped to build robust and reliable GraphQL APIs for your projects.
Stay tuned for more advanced topics, and happy coding! 🚀