Apollo Server and MySQL: 如何直接从数据库获取数据?
Apollo Server and MySQL: How to get data direct from the database?
目前我正在用 ApolloServer/NodeJS 编写 GraphQL API。我有一个 MySQL 数据库。因为我是这个主题的新手,所以我不知道如何直接从数据库中获取数据。我的api向客户端发送文章信息(这是我的api的唯一目的)。客户端可以通过ID搜索特定的文章(每个article/products在数据库中都有一个特定的ID。客户端输入的id应该与数据库中的id进行比较。所以如果id = 1那么api 应该查看数据库中 id = 1 的文章,并将关于这篇文章的请求信息发送给客户端。(我知道 PHP 可能会更好,但我有一些PHP 有问题)。但我不知道如何将其写入我的解析器。
这是我的解析器:
const models = require('../models');
module.exports = {
Query: {
article: (parent, {id}) => {
return models.articledata.find(
(c) => c.id == id
);
}
},
Node: {
__resolveType(node) {
if(node.toObject().name){
return 'Article';
}
}
}
}
这是我的模型:
let articledata = [{
id: 1,
name: 'title',
description: 'Beispiel',
imgs: 'www.test.de/img',
btnLink: 'shoplink',
headline: 'Spicy das neue Spiel',
introduction: 'Beispiel',
definition: 'Beispiel',
tutorial: 'Beispiel',
specsText: 'Beispiel',
endText: 'Beispiel',
age: 12,
duration: 30,
players: 12,
category: 'Beispiel',
designer: 'Beispiel',
language: 'Deutsch',
releaseDate: 2020,
topic: 'Beispiel',
}];
module.exports = { articledata };
这是我的 index.js:
const { ApolloServer } = require('apollo-server');
const schema = require('./schema');
const resolvers = require('./resolvers');
const server = new ApolloServer({
typeDefs: schema,
resolvers,
dataSources: () => {
articleAPI: new ArticleAPI()
}
})
//The listen-method launches a web server
server.listen().then(({ url }) => {
console.log('Server ready at ${url}');
});
当然我在网上搜索过,但似乎没有什么适合我的。很高兴得到你的帮助!!
目前我正在用 ApolloServer/NodeJS 编写 GraphQL API。我有一个 MySQL 数据库。因为我是这个主题的新手,所以我不知道如何直接从数据库中获取数据。我的api向客户端发送文章信息(这是我的api的唯一目的)。客户端可以通过ID搜索特定的文章(每个article/products在数据库中都有一个特定的ID。客户端输入的id应该与数据库中的id进行比较。所以如果id = 1那么api 应该查看数据库中 id = 1 的文章,并将关于这篇文章的请求信息发送给客户端。(我知道 PHP 可能会更好,但我有一些PHP 有问题)。但我不知道如何将其写入我的解析器。
这是我的解析器:
const models = require('../models');
module.exports = {
Query: {
article: (parent, {id}) => {
return models.articledata.find(
(c) => c.id == id
);
}
},
Node: {
__resolveType(node) {
if(node.toObject().name){
return 'Article';
}
}
}
}
这是我的模型:
let articledata = [{
id: 1,
name: 'title',
description: 'Beispiel',
imgs: 'www.test.de/img',
btnLink: 'shoplink',
headline: 'Spicy das neue Spiel',
introduction: 'Beispiel',
definition: 'Beispiel',
tutorial: 'Beispiel',
specsText: 'Beispiel',
endText: 'Beispiel',
age: 12,
duration: 30,
players: 12,
category: 'Beispiel',
designer: 'Beispiel',
language: 'Deutsch',
releaseDate: 2020,
topic: 'Beispiel',
}];
module.exports = { articledata };
这是我的 index.js:
const { ApolloServer } = require('apollo-server');
const schema = require('./schema');
const resolvers = require('./resolvers');
const server = new ApolloServer({
typeDefs: schema,
resolvers,
dataSources: () => {
articleAPI: new ArticleAPI()
}
})
//The listen-method launches a web server
server.listen().then(({ url }) => {
console.log('Server ready at ${url}');
});
当然我在网上搜索过,但似乎没有什么适合我的。很高兴得到你的帮助!!