使用 graphql 从数据库中获取数据
Fetch data from database with graphql
我使用 graphql-yoga
创建了一个 graphql 服务器,并使用 volatile
作为我的应用程序的数据库。
到目前为止,发布效果很好,我在尝试获取已添加到数据库的数据时遇到了问题。
这是我的代码,所以你可以帮助我找出我做错了什么。
在我的架构中有下一个。
type Query{
memes:[Meme!]!
meme(id:ID): Meme
text:String!
}
memes
查询失败了。
查询本身就是下一个。
Query:{
memes:()=>fetch(`${VOLATILE_URL}`).then(res=>console.log(res.json())),
meme:(parent,args)=>memes.find(meme=>meme.id===args.id),
}
这就是我添加数据的方式。
let idCount = 0;
const memes = [];
type Mutation{
createMeme(url:String!, text:String!):Meme
}
Mutation:{
createMeme:(parent,args)=>{
const meme = {
id:`${idCount++}`,
url:args.url,
text:args.text,
}
memes.push(meme);
fetch(`${VOLATILE_URL}&val={"memes_list":${memes}}`)
.then(res=>console.log(res));
//fetch(`${VOLATILE_URL}${meme.id}&val={"id":${meme.id},"url":${meme.url},"text":${meme.text}}`)
// .then(res=> console.log(res));
},
}
现在,当访问 https://volatile.wtf/?key=memes
时,响应是我的带有对象数组的对象。
但是这样请求。
query{
memes{
id
url
text
}
}
我得到 Cannot return null for non-nullable field Query.memes
。
我是 graphql 的新手,所以我真的不知道获取这些数据的方法是什么。
提前致谢。
这个解析器 ...
memes:()=>fetch(`${VOLATILE_URL}`).then(res=>console.log(res.json())),
return没什么(解析器应该)
如果您需要调试响应,请在末尾调用return
memes:()=>fetch(`${VOLATILE_URL}`).then(res=> {
let response = res.json();
console.log(response);
return response;
} ),
我使用 graphql-yoga
创建了一个 graphql 服务器,并使用 volatile
作为我的应用程序的数据库。
到目前为止,发布效果很好,我在尝试获取已添加到数据库的数据时遇到了问题。
这是我的代码,所以你可以帮助我找出我做错了什么。
在我的架构中有下一个。
type Query{
memes:[Meme!]!
meme(id:ID): Meme
text:String!
}
memes
查询失败了。
查询本身就是下一个。
Query:{
memes:()=>fetch(`${VOLATILE_URL}`).then(res=>console.log(res.json())),
meme:(parent,args)=>memes.find(meme=>meme.id===args.id),
}
这就是我添加数据的方式。
let idCount = 0;
const memes = [];
type Mutation{
createMeme(url:String!, text:String!):Meme
}
Mutation:{
createMeme:(parent,args)=>{
const meme = {
id:`${idCount++}`,
url:args.url,
text:args.text,
}
memes.push(meme);
fetch(`${VOLATILE_URL}&val={"memes_list":${memes}}`)
.then(res=>console.log(res));
//fetch(`${VOLATILE_URL}${meme.id}&val={"id":${meme.id},"url":${meme.url},"text":${meme.text}}`)
// .then(res=> console.log(res));
},
}
现在,当访问 https://volatile.wtf/?key=memes
时,响应是我的带有对象数组的对象。
但是这样请求。
query{
memes{
id
url
text
}
}
我得到 Cannot return null for non-nullable field Query.memes
。
我是 graphql 的新手,所以我真的不知道获取这些数据的方法是什么。
提前致谢。
这个解析器 ...
memes:()=>fetch(`${VOLATILE_URL}`).then(res=>console.log(res.json())),
return没什么(解析器应该)
如果您需要调试响应,请在末尾调用return
memes:()=>fetch(`${VOLATILE_URL}`).then(res=> {
let response = res.json();
console.log(response);
return response;
} ),