前置资源
【官网】:https://www.apollographql.com/
【github】:https://github.com/apollographql
入门示例
安装命令: npm install apollo-server graphql
const { ApolloServer, gql } = require('apollo-server');
// 1. 定义 schema
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`;
// 数据准备
const books = [
{
title: 'The Awakening',
author: 'Kate Chopin',
},
{
title: 'City of Glass',
author: 'Paul Auster',
},
];
// 2. 定义 resolver
const resolvers = {
Query: {
books: () => books,
},
};
// 3. 创建 server
const server = new ApolloServer({ typeDefs, resolvers });
// 4. 启动web服务
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
整合express
安装命令: npm install apollo-server-express
此时你将不需要
apollo-server
,将其卸载npm un apollo-server
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
// 1. 定义 schema
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`;
// 数据准备
const books = [
{
title: 'The Awakening',
author: 'Kate Chopin',
},
{
title: 'City of Glass',
author: 'Paul Auster',
},
];
// 2. 定义 resolver
const resolvers = {
Query: {
books: () => books,
},
};
async function startApolloServer() {
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
});
await server.start();
// 将ApolloServer和express进行整合
server.applyMiddleware({ app });
app.use((req, res) => {
res.status(200);
res.send('Hello!');
res.end();
});
await new Promise(resolve => app.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
return { server, app };
}
startApolloServer()
ApolloServer resolvers参数
解析器resolver可以选择接受四个位置参数:(parent, args, context, info)
args参数
args 参数是一个对象,其中包含 GraphQL 客户端操作提供的所有 GraphQL 参数。
// 1. 定义 schema
const typeDefs = gql`
type User {
id: ID!
name: String
}
type Query {
user(id: ID!): User
}
`;
const users = [
{
id: '1',
name: 'Elizabeth Bennet'
},
{
id: '2',
name: 'Fitzwilliam Darcy'
}
];
// 2. 定义 resolver
const resolvers = {
Query: {
user(parent, args, context, info) {
return users.find(user => user.id === args.id);
}
},
};
parent参数
parent为此字段父项的解析器的返回值(即解析器链中的前一个解析器)。对于没有父级的顶级字段的解析器(例如 Query 的字段),该值是从传递给 Apollo Server 的构造函数的 rootValue 函数中获取的。
// 1. 定义 schema
const typeDefs = gql`
# A library has a branch and books
type Library {
branch: String!
books: [Book!]
}
# A book has a title and author
type Book {
title: String!
author: Author!
}
# An author has a name
type Author {
name: String!
}
# Queries can fetch a list of libraries
type Query {
libraries: [Library]
}
`;
//数据准备
const libraries = [
{
branch: 'downtown'
},
{
branch: 'riverside'
},
];
const books = [
{
title: 'The Awakening',
author: 'Kate Chopin',
branch: 'riverside'
},
{
title: 'City of Glass',
author: 'Paul Auster',
branch: 'downtown'
},
];
// 2. 定义 resolver
const resolvers = {
Query: {
libraries() {
return libraries;
}
},
Library: {
books(parent) {
return books.filter(book => book.branch === parent.branch);
}
},
Book: {
author(parent) {
return {
name: parent.author
};
}
}
};
针对该架构的有效查询如下:
query GetBooksByLibrary {
libraries {
books {
author {
name
}
}
}
}
此查询的结果解析器链与查询本身的层次结构相匹配:
这些解析器按上面显示的顺序执行,通过父参数将它们的返回值传递给链中的下一个解析器。解析器链如下所示(每个子链并行执行):