【发布时间】:2022-01-10 01:12:56
【问题描述】:
我没有为此应用程序使用 AWS AppSync。我已经创建了 Graphql 架构,我已经制作了自己的解析器。对于每个创建、查询,我都制作了每个 Lambda 函数。我使用了 DynamoDB 单表概念,它是全局二级索引。
我可以创建一个 Book 项目。在 DynamoDB 中,该表如下所示:。
我遇到了返回 Graphql 查询的问题。从 DynamoDB 表中获取 Items 后,我必须使用 Map 函数,然后根据 Graphql type 返回 Items。我觉得这不是有效的方法。 Idk 查询数据的最佳方式。此外,作者和作者查询都为空。
这是我的gitlab-branch。
这是我的 Graphql 架构
import { gql } from 'apollo-server-lambda';
const typeDefs = gql`
enum Genre {
adventure
drama
scifi
}
enum Authors {
AUTHOR
}
# Root Query - all the queries supported by the schema
type Query {
"""
All Authors query
"""
authors(author: Authors): [Author]
books(book: String): [Book]
}
# Root Mutation - all the mutations supported by the schema
type Mutation {
createBook(input: CreateBook!): Book
}
"""
One Author can have many books
"""
type Author {
id: ID!
authorName: String
book: [Book]!
}
"""
Book Schema
"""
type Book {
id: ID!
name: String
price: String
publishingYear: String
publisher: String
author: [Author]
description: String
page: Int
genre: [Genre]
}
input CreateBook {
name: String
price: String
publishingYear: String
publisher: String
author: [CreateAuthor]
description: String
page: Int
genre: [Genre]
}
input CreateAuthor {
authorName: String!
}
`;
export default typeDefs;
这是我创建的图书项目
import AWS from 'aws-sdk';
import { v4 } from 'uuid';
import { CreateBook } from '../../generated/schema';
async function createBook(_: unknown, { input }: { input: CreateBook }) {
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const id = v4();
const authorsName =
input.author &&
input.author.map(function (item) {
return item['authorName'];
});
const params = {
TableName: process.env.ITEM_TABLE ? process.env.ITEM_TABLE : '',
Item: {
PK: `AUTHOR`,
SK: `AUTHORS#${id}`,
GSI1PK: `BOOKS`,
GSI1SK: `BOOK#${input.name}`,
name: input.name,
author: authorsName,
price: input.price,
publishingYear: input.publishingYear,
publisher: input.publisher,
page: input.page,
description: input.description,
genre: input.genre,
},
};
await dynamoDb.put(params).promise();
return {
...input,
id,
};
}
export default createBook;
这是查询全书的方式
import AWS from 'aws-sdk';
async function books(_: unknown, input: { book: string }) {
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: process.env.ITEM_TABLE ? process.env.ITEM_TABLE : '',
IndexName: 'GSI1',
KeyConditionExpression: 'GSI1PK = :hkey',
ExpressionAttributeValues: {
':hkey': `${input.book}`,
},
};
const { Items } = await dynamoDb.query(params).promise();
const allBooks = // NEED TO MAP THE FUNcTION THEN RETURN THE DATA BASED ON GRAPHQL //QUERIES.
Items &&
Items.map((i) => {
const genre = i.genre.filter((i) => i);
return {
name: i.name,
author: i.author,
genre,
};
});
return allBooks;
}
export default books;
这是我的作者查询和控制台结果图片
import AWS from 'aws-sdk';
import { Author, Authors } from '../../generated/schema';
async function authors(
_: unknown,
input: { author: Authors }
): Promise<Author> {
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: process.env.ITEM_TABLE ? process.env.ITEM_TABLE : '',
KeyConditionExpression: 'PK = :hkey',
ExpressionAttributeValues: {
':hkey': `${input.author}`,
},
};
const { Items } = await dynamoDb.query(params).promise();
console.log({ Items }); // I can see the data but don't know how to returns the data like this below type without using map function
// type Author {
// id: ID!
// authorName: String
// book: [Book]!
// }
return Items; // return null in Graphql play ground.
}
export default authors;
编辑:当前解析器映射
// resolver map - src/resolvers/index.ts
const resolvers = {
Query: {
books,
authors,
author,
book,
},
Mutation: {
createBook,
},
};
【问题讨论】:
-
你能帮助澄清你的实际问题/问题是什么吗?是关于 lambda 解析器的编写,比如this SO answer(问题是 AppSync 相关的,Apollo Server 遵循类似的逻辑)?也许给一个客户端查询示例并添加您所说的“高效”处理的意思。
-
问题是如何获得作者姓名和他/她/他们的书。我的 atm 为空。
-
请将您的resolver map 添加到问题中。
-
抱歉回复晚了,一直在关注我????????。这是我的分支:gitlab.com/alak/aws-book-schema-graphql
-
作者和作者的查询我都为空。我是 Graphql 的新手。如果我犯了任何错误,请随时给我反馈
标签: amazon-web-services aws-lambda graphql apollo-server