【问题标题】:Problems when making a request with variable in apollo reactjs在 apollo reactjs 中使用变量发出请求时的问题
【发布时间】:2021-09-30 23:48:52
【问题描述】:

我使用石墨烯作为后盾,并在前端使用 apollo 做出反应,但是当尝试发出只有一个对象返回的请求时,它给了我以下错误

error

react 的代码是

import React from 'react'
import { useParams } from 'react-router-dom';
import { gql, useQuery } from '@apollo/client';

const Post = (props) => {

    const { id } = useParams();
    console.log(id);

    const [loading, error, data] = useQuery(GET_POST, {
        variables: {
            postId: id
        }
    })

    if (loading) return null;
    if (error) return `Error! ${error}`;

    return (
        <div>
            Post
        </div>
    )
}


const GET_POST = gql`
query IdPost($postId: ID!){
    idPost(postId:$postId){
        id
        user{
            username
            email
        }
        title
        body
        image
        createdAt
        updatedAt
        published
    }
}
`;

export default Post

我不知道为什么会这样,这里我也留下了我在石墨烯中用于该请求的代码

class Query(graphene.ObjectType):
    all_posts = graphene.List(PostType)
    id_post = graphene.Field(PostType, postId=graphene.ID())

def resolve_all_posts(root, info):
    return Post.objects.filter(published=True)

def resolve_id_post(root, info, postId):
    return Post.objects.get(id=postId)

【问题讨论】:

    标签: reactjs graph typeerror apollo-client graphene-django


    【解决方案1】:

    我相信是的

    const {loading, error, data} = useQuery
    

    没有

    const [loading, error, data] = useQuery
    

    【讨论】:

    • 是的,我很困惑,但我仍然有错误
    • @FernandoLopez,你能告诉我们你的 React 和 Apollo Client 版本是多少?
    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2019-11-06
    相关资源
    最近更新 更多