[GRAPHQL] Get started with Apollo Server (ok)
https://www.apollographql.com/docs/apollo-server/getting-started/
mkdir graphql-server-example
cd graphql-server-examplenpm init --yesnpm install apollo-server graphqltouch index.js
Last updated
https://www.apollographql.com/docs/apollo-server/getting-started/
mkdir graphql-server-example
cd graphql-server-examplenpm init --yesnpm install apollo-server graphqltouch index.js
Last updated
const { ApolloServer, gql } = require('apollo-server');
// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against your data.
// Một lược đồ là một tập hợp các định nghĩa kiểu (do đó "typeDefs")
// cùng nhau xác định "hình dạng" của các truy vấn được thực thi dữ liệu của bạn.
const typeDefs = gql`
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
# This "Book" type defines the queryable fields for every book in our data source.
# Nhận xét trong chuỗi GraphQL (chẳng hạn như chuỗi này) bắt đầu bằng ký hiệu băm (#).
# Loại "Sách" này xác định các trường có thể truy vấn cho mọi sách trong nguồn dữ liệu của chúng tôi.
type Book {
title: String
author: String
}
# The "Query" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each. In this
# case, the "books" query returns an array of zero or more Books (defined above).
# Loại "Truy vấn" đặc biệt: nó liệt kê tất cả các truy vấn có sẵn
# máy khách có thể thực thi, cùng với kiểu trả về cho mỗi máy khách. Trong này
# case, truy vấn "books" trả về một mảng không có hoặc nhiều Sách (đã định nghĩa ở trên).
type Query {
books: [Book]
}
`;const books = [
{
title: 'The Awakening',
author: 'Kate Chopin',
},
{
title: 'City of Glass',
author: 'Paul Auster',
},
];// Resolvers define the technique for fetching the types defined in the schema.
// This resolver retrieves books from the "books" array above.
};
// Trình phân giải xác định kỹ thuật tìm nạp các kiểu được xác định trong // lược đồ.
// Trình phân giải này truy xuất sách từ mảng "sách" ở trên.
const resolvers = {
Query: {
books: () => books,
},
};// The ApolloServer constructor requires two parameters: your schema definition
// and your set of resolvers.
// Phương thức khởi tạo ApolloServer yêu cầu hai tham số: lược đồ của bạn định nghĩa
// và tập hợp các trình phân giải của bạn.
const server = new ApolloServer({ typeDefs, resolvers });
// The `listen` method launches a web server.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});node index.js🚀 Server ready at http://localhost:4000/{
books {
title
author
}
}