检查apollographql时返回null

发布于 2025-02-08 19:21:18 字数 3538 浏览 1 评论 0原文

我不知道为什么我无法从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2025-02-15 19:21:18

我已经知道我出了什么问题。
我还没有退还
哈哈
只需从解析器文件的突变部分删除{}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文