📅  最后修改于: 2023-12-03 15:31:03.840000             🧑  作者: Mango
GraphQl is a query language created by Facebook to provide a flexible and efficient alternative to REST APIs. Node.js is a popular server-side JavaScript environment for building scalable and high-performance web applications. Combining these two technologies can result in a powerful way to build APIs that can handle complex queries and respond with only the required data.
To get started with GraphQl in Node.js, you need to install the required packages. The most popular package for building GraphQl APIs with Node.js is graphql-yoga
. It is a fully-featured, lightweight GraphQL server implementation that is built on top of the popular express
and apollo-server
.
To install graphql-yoga
, run the following command:
npm install graphql-yoga
The first step in building a GraphQl API is to define its schema. A schema is a type system for your API that defines the data types that can be queried, the operations that can be performed, and the relationships between them.
Here is an example schema for a simple blog API:
type Post {
id: ID!
title: String!
content: String!
author: User!
}
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
type Query {
post(id: ID!): Post
posts: [Post!]!
user(id: ID!): User
users: [User!]!
}
type Mutation {
createPost(title: String!, content: String!, authorId: ID!): Post!
}
This schema defines two types - Post
and User
- and three operations - Query
, Mutation
, and Subscription
. The Query
operation allows clients to fetch data, the Mutation
operation allows clients to modify data, and the Subscription
operation allows clients to receive real-time updates.
Once you have defined your schema, you need to implement the resolvers for each operation. Resolvers are functions that receive the query or mutation and return the data.
Here is an example implementation of a Query
resolver for fetching a post by its ID:
const { posts } = require('./data');
const resolvers = {
Query: {
post: (parent, args, context) => {
const { id } = args;
return posts.find(post => post.id === id);
},
posts: () => {
return posts;
},
user: (parent, args, context) => {
const { id } = args;
return users.find(user => user.id === id);
},
users: () => {
return users;
},
},
Mutation: {
createPost: (parent, args, context) => {
const { title, content, authorId } = args;
const post = {
id: uuidv4(),
title,
content,
authorId,
};
posts.push(post);
return post;
},
},
Post: {
author: (parent, args, context) => {
const { authorId } = parent;
return users.find(user => user.id === authorId);
}
},
User: {
posts: (parent, args, context) => {
const { id } = parent;
return posts.filter(post => post.authorId === id);
}
},
};
This implementation uses an in-memory data source for simplicity. In a real-world application, you would typically use a database or a REST API for fetching and storing data.
To run the GraphQl server, you need to create an instance of the GraphQLServer
class from graphql-yoga
and pass it your schema and resolvers:
const { GraphQLServer } = require('graphql-yoga');
const server = new GraphQLServer({
typeDefs: './schema.graphql',
resolvers,
});
server.start(() => console.log('Server is running on http://localhost:4000'));
This will start the server on http://localhost:4000
. You can then use a GraphQl client to query and mutate data from the server.
GraphQl with Node.js provides a powerful way to build APIs that can handle complex queries and respond with only the required data. By defining a schema and implementing resolvers, you can build scalable and high-performance API that meets your requirements.
Happy Coding!