简体   繁体   English

反应中继在片段上无效使用@refetchable

[英]react-relay Invalid use of @refetchable on fragment

I am learning react-relay (v13) and have a problem specifying a pagination fragment with @refetchable.我正在学习 react-relay (v13),在使用 @refetchable 指定分页片段时遇到问题。 It is a very basic structure and everything works if @refetchable is not used (ie I can query grapql server, obtain data, useFragment to render same query/fragment without pagination)这是一个非常基本的结构,如果不使用@refetchable 一切正常(即我可以查询 grapql 服务器,获取数据,使用 Fragment 来呈现相同的查询/片段而无需分页)

When I introduce @refetchable in the ProjectsFragment_user the react-relay compiler gives error:当我在 ProjectsFragment_user 中引入 @refetchable 时,react-relay 编译器给出了错误:

[ERROR] ✖︎ Invalid use of @refetchable on fragment 'ProjectsFragment_user', only supported are fragments on:
 - the Viewer type
 - the Query type
 - the Node interface or types implementing the Node interface
 - @fetchable type

Now this is confusing as ProjectsFragment_user is on a query that looks like this:现在这很令人困惑,因为 ProjectsFragment_user 在一个看起来像这样的查询上:

// Dashboard.js
export const dashboardQuery = graphql`
query DashboardQuery ($id: ID) {
  user (id: $id){
    id 
    name
    ...ProjectsFragment_user
  }
}
`;
// then 
const  data = useLazyLoadQuery(dashboardQuery, {}, {},);

//Projects.js defines fragment

const {data, loadNext} = usePaginationFragment(graphql`
    fragment ProjectsFragment_user on User 
    @argumentDefinitions(
        first: {type: "Int", defaultValue: 10}
        after: {type: "String"}
        before: {type: "String"}
      )
    @refetchable(queryName: "ProjectsListPaginationQuery")
    {
            projects (first: $first, after: $after, before: $before)
            @connection(key: "ProjectsList_projects") {
                total
                edges {
                  node {
                   id
                   name
          
                    members {
                     total
                     edges {
                     node {
                        member_id
                        member {
                        name
                        }
                        role
                      }
                     }
                   }
                  }
              }
            }
        }
    `,
    props?.projects);

Note, if just useFragment on ProjectsFragment_user and remove @refetchable I can fetch and render data.请注意,如果只是在 ProjectsFragment_user 上使用 Fragment 并删除 @refetchable,我就可以获取和呈现数据。

Does anyone have any clues why @refetchable is not allowed here??有没有人知道为什么这里不允许@refetchable?

And to answer my own question (after days of trying to figure it out).并回答我自己的问题(经过几天的尝试)。 The Schema needs to be specific that Type is extending Node interface otherwise react will complain as per above. Schema 需要具体说明 Type 正在扩展 Node 接口,否则反应会按照上面的方式抱怨。

So, I cnahged Project from所以,我从

type Project {
   id: ID!
   name: String
}

to

type Project implements Node {
   id: ID!
   name: String
}

and React-Relay will compile now. React-Relay 现在将编译。 I wish relay error messages are a bit clearer as there are sooo many things that can go wrong and as relay is pretty opinionated about things better error messages would be a great help.我希望中继错误消息更清楚一些,因为有太多的事情可能 go 出错,而且中继对事情非常自以为是,更好的错误消息将是一个很大的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM