检查apollographql时返回null
我不知道为什么我无法从GraphQl返回任何数据 我很快需要帮助 这是我的代码 我在Nodejs Express中使用了3个文件,这些文件是模式,解析器和数据。 我在nodejs Express中使用了3个文件,即架构,解析器和数据。首先是带有类型
const {gql} = require('apollo-server');
const typeDefs = gql`
type Post {
id: ID
name: String
content: String
genre: String
author: Author
}
type Author {
id: ID
name: String
age: Int
posts: [Post]
}
# The query type, represents the current state of the system
type Query {
posts: [Post]
authors: [Author]
post(id: ID!): Post
author(id: ID!): Author
}
type Mutation {
createAuthor(name: String, age: Int): Author
createPost(name: String, content: String, genre: String, authorId: ID!): Post
}
`
module.exports = typeDefs;
文件解析器的模式文件返回所需的数据。
const mongoDataMethods = require('../data/data')
const resolvers = {
Query: {
posts : async (parent, args, {mongoDataMethods}) => await mongoDataMethods.getAllPosts(),
authors: async (parent, args, {mongoDataMethods}) => await mongoDataMethods.getAllAuthors(),
post: async (parent, {id} ,{mongoDataMethods}) => await mongoDataMethods.getPostById(id),
author: async (parent, {id} ,{mongoDataMethods}) => await mongoDataMethods.getAuthorById(id),
},
Post :{
author: async ({authorId},args,{mongoDataMethods}) => await mongoDataMethods.getAuthorById(authorId)
},
Author:{
posts: async ({id} , args , {mongoDataMethods}) => await mongoDataMethods.getAllPosts({authorId : id})
},
// Mutations
Mutation: {
createPost: async (parent, args, {mongoDataMethods}) =>{
await mongoDataMethods.createPost(args)
},
createAuthor : async (parent, args, {mongoDataMethods}) =>{
await mongoDataMethods.createAuthor(args)
}
}
}
module.exports = resolvers;
接下来是从mongodb获取数据以返回解析器文件的部分问题
const Post = require('../model/Post')
const Author = require('../model/Author')
const mongoDataMethods = {
getAllPosts: async (condition = null) => condition === null ?await Post.find() : await Post.find(condition),
getAllAuthors: async () => await Author.find(),
getAuthorById: async id => await Author.findById(id),
getPostById: async id => await Post.findById(id),
//Mutation
createPost: async (args) => {
const newPost = new Post(args)
return await newPost.save()
},
createAuthor: async (args) =>{
const newAuthor = new Author(args)
return await newAuthor.save()
}
}
module.exports = mongoDataMethods
。当我执行服务器运行并测试命令时,当我运行突变时,我没有得到我想要的返回数据。我以数据为null回到了一个带有数据的JSON,我想找回我想要的值,但没有
mutation Mutation( $name: String, $content: String, $genre: String ,$authorId: ID!) {
createPost(authorId: $authorId, name: $name, content: $content, genre: $genre) {
id
name
content
}
}
带有变量的
{
"content": "Thanks you for that custom hook. I actually managed to figure it out but I didn't use a custom hook instead. How does your custom hook differ from passing in update and using mutationResult?",
"name": "How do you get the return data from a mutation?",
"genre": "action",
"authorId": "62ae509be22091e690dea46a"
}
null ,我没有获得任何数据,它返回
{
"data": {
"createPost": null
}
}
需要帮助,谢谢!
I don't know why i can't get any data return from graphql
I need help soon
This is my code
I have 3 files used in nodejs express which are schema, resolver and data.
I have 3 files used in nodejs express namely schema, resolver and data. The first is the schema file with the types
const {gql} = require('apollo-server');
const typeDefs = gql`
type Post {
id: ID
name: String
content: String
genre: String
author: Author
}
type Author {
id: ID
name: String
age: Int
posts: [Post]
}
# The query type, represents the current state of the system
type Query {
posts: [Post]
authors: [Author]
post(id: ID!): Post
author(id: ID!): Author
}
type Mutation {
createAuthor(name: String, age: Int): Author
createPost(name: String, content: String, genre: String, authorId: ID!): Post
}
`
module.exports = typeDefs;
File resolver returns the required data.
const mongoDataMethods = require('../data/data')
const resolvers = {
Query: {
posts : async (parent, args, {mongoDataMethods}) => await mongoDataMethods.getAllPosts(),
authors: async (parent, args, {mongoDataMethods}) => await mongoDataMethods.getAllAuthors(),
post: async (parent, {id} ,{mongoDataMethods}) => await mongoDataMethods.getPostById(id),
author: async (parent, {id} ,{mongoDataMethods}) => await mongoDataMethods.getAuthorById(id),
},
Post :{
author: async ({authorId},args,{mongoDataMethods}) => await mongoDataMethods.getAuthorById(authorId)
},
Author:{
posts: async ({id} , args , {mongoDataMethods}) => await mongoDataMethods.getAllPosts({authorId : id})
},
// Mutations
Mutation: {
createPost: async (parent, args, {mongoDataMethods}) =>{
await mongoDataMethods.createPost(args)
},
createAuthor : async (parent, args, {mongoDataMethods}) =>{
await mongoDataMethods.createAuthor(args)
}
}
}
module.exports = resolvers;
Next is the part to get data from mongoDB to return to the resolver file
const Post = require('../model/Post')
const Author = require('../model/Author')
const mongoDataMethods = {
getAllPosts: async (condition = null) => condition === null ?await Post.find() : await Post.find(condition),
getAllAuthors: async () => await Author.find(),
getAuthorById: async id => await Author.findById(id),
getPostById: async id => await Post.findById(id),
//Mutation
createPost: async (args) => {
const newPost = new Post(args)
return await newPost.save()
},
createAuthor: async (args) =>{
const newAuthor = new Author(args)
return await newAuthor.save()
}
}
module.exports = mongoDataMethods
the problem occurs in the next step. when i execute the server run and test the commands, when i run the mutation i don't get the return data i want. I get back a json with data as null and I want to get back the values I want but not null
mutation Mutation( $name: String, $content: String, $genre: String ,$authorId: ID!) {
createPost(authorId: $authorId, name: $name, content: $content, genre: $genre) {
id
name
content
}
}
with variables
{
"content": "Thanks you for that custom hook. I actually managed to figure it out but I didn't use a custom hook instead. How does your custom hook differ from passing in update and using mutationResult?",
"name": "How do you get the return data from a mutation?",
"genre": "action",
"authorId": "62ae509be22091e690dea46a"
}
i don't get any data and it return
{
"data": {
"createPost": null
}
}
need help and thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经知道我出了什么问题。
我还没有退还
哈哈
只需从解析器文件的突变部分删除{}
I already know where I went wrong.
I haven't returned it yet
LOL
Just remove the {} from the mutation section of the resolver file