简体   繁体   English

使用Relay&Graphql在根节点上分页

[英]Pagination on root node with Relay & Graphql

Struggling to get this setup and beating my head against the wall. 努力进行此设置并使我的头靠在墙上。 I have the following structure for my GraphQL DB: 我的GraphQL DB具有以下结构:

Graphql Schema Graphql模式

let Schema = () => {

  let {nodeInterface, nodeField} = nodeDefinitions(
    (globalId) => {

      var {type, id} = fromGlobalId(globalId);
      if (type === 'Grant') {
        return GrantModel.findById(id);
      } else if (type === 'Scope') {
        return ScopeModel.findById(id);
      } else {
        return null;
      }
    },
    (obj) => {
      if (obj instanceof ScopeModel) {
        return scopeType;
      } else if (obj instanceof GrantModel)  {
        return grantType;
      } else {
        return null;
      }
    }
  );


  let grantType = new GraphQLObjectType({
    name: 'Grant',
    fields: () => ({
      id: globalIdField('Grant'),
      name: {type: GraphQLString},
      description: {type: GraphQLString}
    }),
    interface : [nodeInterface]
  });

  let scopeType = new GraphQLObjectType({
    name:'Scope',
    fields: () => ({
      id: globalIdField('Scope'),
      description: {type: GraphQLString},
      name: {type: GraphQLString},
      enabled: {type: GraphQLBoolean},
      grants: {type: new GraphQLList(grantType)}
    })
  });


  let {connectionType: scopeConnection} = connectionDefinitions({nodeType: scopeType});

  let viewerType = new GraphQLObjectType({
    name: 'Viewer',
    fields: () => ({
      scopes: {
        type: scopeConnection,
        args: connectionArgs,
        resolve: async (scope, args) => connectionFromArray(await ScopeModel.find({}), args)
      }
    })
  });

  let schema = new GraphQLSchema({
    query: new GraphQLObjectType({
      name: 'Query',
      fields: () => ({
        viewer: {
          type: viewerType,
          resolve: () => {}
        }
      })
    })

    /*mutation: new GraphQLObjectType({
      name: 'Mutation',
      fields: () => ({
        incrementCounter: {
          type: GraphQLInt,
          resolve: () => 4
        }
      })
    })*/

  });

  return schema;
}

export default Schema;

I have the following react components; 我有以下反应成分; note my index.js is my root component, followed by ScopeList.js and then Scope.js : 请注意,我的index.js是我的根组件,其次是ScopeList.js ,然后是Scope.js

index.js index.js

const IndexQueryPage = graphql`
  query IndexQuery {
    viewer {
      ...ScopeList_scopes
    }
  }
`;

class Index extends React.Component {
  render () {
    return (
      <div className="container">
        <table className="table">
          <thead>
            <tr>
              <th>Date Added</th>
              <th>Service</th>
              <th>Description</th>
              <th>Enabled</th>
            </tr>
          </thead>
          <QueryRenderer
           environment={environment}
           query={IndexQueryPage}
           variables={{}}
           render={({error, props}) => {
             console.log(props);
             if(error) {
               return <tbody><tr><td colSpan='4'></td></tr></tbody>;
             }
             if(!props) {
               return <tbody><tr><td colSpan='4'>Loading.....</td></tr></tbody>;
             }
             return (
               <ScopeList scopes={props.scopes} />
             )
           }}
          />
        </table>
      </div>
    )
  }
}

module.exports = Index;

ScopeList.js ScopeList.js

class ScopeList extends React.Component {
  render() {
    return (
      <tbody>
       {
         this.props.scopes.edges.map(edge => <Scope scope={edge.node} key={edge.node.cursor} />)
       }
      </tbody>
    )
  }
}

module.exports = createPaginationContainer(ScopeList, graphql`
  fragment ScopeList_scopes on Viewer
  @argumentDefinitions(
    count: {type: "Int", defaultValue: 10}
    cursor: {type: "String"}
  )
  {
    scopes (
      first: $count
      after: $cursor
    ) @connection(key: "ScopeList_scopes") {
      edges {
        cursor
        node {
          id
          name
          description
        }
      }     
    }
  }`,
  {
    direction: 'forward',
    getConnectionFromProps(props) {
      return null;
    },
    // This is also the default implementation of `getFragmentVariables` if it isn't provided.
    getFragmentVariables(prevVars, totalCount) {
      return {
        ...prevVars,
        count: totalCount,
      };
    },
    getVariables(props, {count, cursor}, fragmentVariables) {
      return {
        count,
        cursor
      };
    },
    query: graphql`
      query ScopeListQuery(
        $count: Int!
        $cursor: String
      ){
        ...ScopeList_scopes @arguments(count: $count, cursor: $cursor)
      }
   `}
);

Scope.js Scope.js

class Scope extends React.Component {
  render() {
    return (
      <tr key={this.props.scope.id}>
        <td>{this.props.scope.name}</td>
        <td>{this.props.scope.description}</td>
        <td></td>
        <td></td>
      </tr>
    )
  }
}

module.exports = createFragmentContainer(Scope, graphql `
  fragment Scope_scope on Scope {
    id
    name
    description
  }
`);

Essentially I'm trying to setup pagination on the root node scopes however, my understanding is that I needed to decouple this as a root by adding it as a child to viewer . 本质上,我试图在根节点scopes上设置分页,但是我的理解是,我需要通过将其作为子级添加到viewer来使其与根分离。 However, I'm getting the following errors: 但是,出现以下错误:

ERROR in ./app/pages/Index.js
Module not found: Error: You supplied a GraphQL document with validation errors:
component/ScopeList.js: Fragment "ScopeList_scopes" cannot be spread here as objects of type "Query" can never be of type "Viewer".
 @ ./app/pages/Index.js 28:8-53
 @ ./app/component/App.js
 @ ./app/index.js

Can't spread a fragment of type Viewer on the root of a query which is type Query . 无法在查询的根目录Query上散布Viewer类型的片段。 Spread that fragment on the viewer field of the query 将该片段散布在查询的viewer字段中

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

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