Apollo Graphql Pagination failed with limit












-1















I am trying out Apollo pagination. It works correctly if I do not pass the limit argument from the client and hard code the limit argument in my hasMoreData function. If I were to add in the limit argument, all the data will be returned from my server and it will not paginate. The server side code should be correct (I tested it on GraphQL playground).



This does not work properly:



import React, { Component } from "react";
import {
View,
Text,
ActivityIndicator,
FlatList,
Button,
StyleSheet
} from "react-native";
import { graphql } from "react-apollo";
import gql from "graphql-tag";

let picturesList = ;

class HomeScreen extends Component {
loadMore = () => {
this.props.data.fetchMore({
variables: {
offset: picturesList.length
},
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) {
return prev;
}

return {
...prev,
pictures: [...prev.pictures, ...fetchMoreResult.pictures]
};
}
});
};

render() {
const { loading, pictures, variables } = this.props.data;

picturesList = pictures;

if (loading) {
return <ActivityIndicator size="large" />;
}

//TODO - hard coded the limit as 3 which is not supposed to
let hasMoreData = picturesList.length % 3 === 0;

if (picturesList.length <= variables.offset) {
hasMoreData = false;
}

return (
<View style={styles.root}>
<Button title="Show More" onPress={this.loadMore} />
<FlatList
data={picturesList}
renderItem={({ item }) => (
<View style={styles.contentContainer}>
<Text style={styles.content}>{item.title}</Text>
</View>
)}
keyExtractor={item => item.id}
ListFooterComponent={() =>
hasMoreData ? (
<ActivityIndicator size="large" color="blue" />
) : (
<View />
)
}
/>
</View>
);
}
}

const styles = StyleSheet.create({
root: {
flex: 1
},
content: {
fontSize: 35
},
contentContainer: {
padding: 30
}
});

// adding the limit variable here will cause my server to return all data

const PICTURES_QUERY = gql`
query($offset: Int, $limit: Int) {
pictures(offset: $offset, limit: $limit) {
id
title
pictureUrl
}
}
`;

export default graphql(PICTURES_QUERY)(HomeScreen);


The server-side code, just in case:



pictures: async (_, { offset, limit }) => {
let picturesDB = getConnection()
.getRepository(Picture)
.createQueryBuilder("p");
return picturesDB
.take(limit)
.skip(offset)
.getMany();

}


I have added a default parameter in my GraphQL schema:



type Query {
pictures(offset: Int, limit: Int = 3): [Picture!]!
}









share|improve this question





























    -1















    I am trying out Apollo pagination. It works correctly if I do not pass the limit argument from the client and hard code the limit argument in my hasMoreData function. If I were to add in the limit argument, all the data will be returned from my server and it will not paginate. The server side code should be correct (I tested it on GraphQL playground).



    This does not work properly:



    import React, { Component } from "react";
    import {
    View,
    Text,
    ActivityIndicator,
    FlatList,
    Button,
    StyleSheet
    } from "react-native";
    import { graphql } from "react-apollo";
    import gql from "graphql-tag";

    let picturesList = ;

    class HomeScreen extends Component {
    loadMore = () => {
    this.props.data.fetchMore({
    variables: {
    offset: picturesList.length
    },
    updateQuery: (prev, { fetchMoreResult }) => {
    if (!fetchMoreResult) {
    return prev;
    }

    return {
    ...prev,
    pictures: [...prev.pictures, ...fetchMoreResult.pictures]
    };
    }
    });
    };

    render() {
    const { loading, pictures, variables } = this.props.data;

    picturesList = pictures;

    if (loading) {
    return <ActivityIndicator size="large" />;
    }

    //TODO - hard coded the limit as 3 which is not supposed to
    let hasMoreData = picturesList.length % 3 === 0;

    if (picturesList.length <= variables.offset) {
    hasMoreData = false;
    }

    return (
    <View style={styles.root}>
    <Button title="Show More" onPress={this.loadMore} />
    <FlatList
    data={picturesList}
    renderItem={({ item }) => (
    <View style={styles.contentContainer}>
    <Text style={styles.content}>{item.title}</Text>
    </View>
    )}
    keyExtractor={item => item.id}
    ListFooterComponent={() =>
    hasMoreData ? (
    <ActivityIndicator size="large" color="blue" />
    ) : (
    <View />
    )
    }
    />
    </View>
    );
    }
    }

    const styles = StyleSheet.create({
    root: {
    flex: 1
    },
    content: {
    fontSize: 35
    },
    contentContainer: {
    padding: 30
    }
    });

    // adding the limit variable here will cause my server to return all data

    const PICTURES_QUERY = gql`
    query($offset: Int, $limit: Int) {
    pictures(offset: $offset, limit: $limit) {
    id
    title
    pictureUrl
    }
    }
    `;

    export default graphql(PICTURES_QUERY)(HomeScreen);


    The server-side code, just in case:



    pictures: async (_, { offset, limit }) => {
    let picturesDB = getConnection()
    .getRepository(Picture)
    .createQueryBuilder("p");
    return picturesDB
    .take(limit)
    .skip(offset)
    .getMany();

    }


    I have added a default parameter in my GraphQL schema:



    type Query {
    pictures(offset: Int, limit: Int = 3): [Picture!]!
    }









    share|improve this question



























      -1












      -1








      -1








      I am trying out Apollo pagination. It works correctly if I do not pass the limit argument from the client and hard code the limit argument in my hasMoreData function. If I were to add in the limit argument, all the data will be returned from my server and it will not paginate. The server side code should be correct (I tested it on GraphQL playground).



      This does not work properly:



      import React, { Component } from "react";
      import {
      View,
      Text,
      ActivityIndicator,
      FlatList,
      Button,
      StyleSheet
      } from "react-native";
      import { graphql } from "react-apollo";
      import gql from "graphql-tag";

      let picturesList = ;

      class HomeScreen extends Component {
      loadMore = () => {
      this.props.data.fetchMore({
      variables: {
      offset: picturesList.length
      },
      updateQuery: (prev, { fetchMoreResult }) => {
      if (!fetchMoreResult) {
      return prev;
      }

      return {
      ...prev,
      pictures: [...prev.pictures, ...fetchMoreResult.pictures]
      };
      }
      });
      };

      render() {
      const { loading, pictures, variables } = this.props.data;

      picturesList = pictures;

      if (loading) {
      return <ActivityIndicator size="large" />;
      }

      //TODO - hard coded the limit as 3 which is not supposed to
      let hasMoreData = picturesList.length % 3 === 0;

      if (picturesList.length <= variables.offset) {
      hasMoreData = false;
      }

      return (
      <View style={styles.root}>
      <Button title="Show More" onPress={this.loadMore} />
      <FlatList
      data={picturesList}
      renderItem={({ item }) => (
      <View style={styles.contentContainer}>
      <Text style={styles.content}>{item.title}</Text>
      </View>
      )}
      keyExtractor={item => item.id}
      ListFooterComponent={() =>
      hasMoreData ? (
      <ActivityIndicator size="large" color="blue" />
      ) : (
      <View />
      )
      }
      />
      </View>
      );
      }
      }

      const styles = StyleSheet.create({
      root: {
      flex: 1
      },
      content: {
      fontSize: 35
      },
      contentContainer: {
      padding: 30
      }
      });

      // adding the limit variable here will cause my server to return all data

      const PICTURES_QUERY = gql`
      query($offset: Int, $limit: Int) {
      pictures(offset: $offset, limit: $limit) {
      id
      title
      pictureUrl
      }
      }
      `;

      export default graphql(PICTURES_QUERY)(HomeScreen);


      The server-side code, just in case:



      pictures: async (_, { offset, limit }) => {
      let picturesDB = getConnection()
      .getRepository(Picture)
      .createQueryBuilder("p");
      return picturesDB
      .take(limit)
      .skip(offset)
      .getMany();

      }


      I have added a default parameter in my GraphQL schema:



      type Query {
      pictures(offset: Int, limit: Int = 3): [Picture!]!
      }









      share|improve this question
















      I am trying out Apollo pagination. It works correctly if I do not pass the limit argument from the client and hard code the limit argument in my hasMoreData function. If I were to add in the limit argument, all the data will be returned from my server and it will not paginate. The server side code should be correct (I tested it on GraphQL playground).



      This does not work properly:



      import React, { Component } from "react";
      import {
      View,
      Text,
      ActivityIndicator,
      FlatList,
      Button,
      StyleSheet
      } from "react-native";
      import { graphql } from "react-apollo";
      import gql from "graphql-tag";

      let picturesList = ;

      class HomeScreen extends Component {
      loadMore = () => {
      this.props.data.fetchMore({
      variables: {
      offset: picturesList.length
      },
      updateQuery: (prev, { fetchMoreResult }) => {
      if (!fetchMoreResult) {
      return prev;
      }

      return {
      ...prev,
      pictures: [...prev.pictures, ...fetchMoreResult.pictures]
      };
      }
      });
      };

      render() {
      const { loading, pictures, variables } = this.props.data;

      picturesList = pictures;

      if (loading) {
      return <ActivityIndicator size="large" />;
      }

      //TODO - hard coded the limit as 3 which is not supposed to
      let hasMoreData = picturesList.length % 3 === 0;

      if (picturesList.length <= variables.offset) {
      hasMoreData = false;
      }

      return (
      <View style={styles.root}>
      <Button title="Show More" onPress={this.loadMore} />
      <FlatList
      data={picturesList}
      renderItem={({ item }) => (
      <View style={styles.contentContainer}>
      <Text style={styles.content}>{item.title}</Text>
      </View>
      )}
      keyExtractor={item => item.id}
      ListFooterComponent={() =>
      hasMoreData ? (
      <ActivityIndicator size="large" color="blue" />
      ) : (
      <View />
      )
      }
      />
      </View>
      );
      }
      }

      const styles = StyleSheet.create({
      root: {
      flex: 1
      },
      content: {
      fontSize: 35
      },
      contentContainer: {
      padding: 30
      }
      });

      // adding the limit variable here will cause my server to return all data

      const PICTURES_QUERY = gql`
      query($offset: Int, $limit: Int) {
      pictures(offset: $offset, limit: $limit) {
      id
      title
      pictureUrl
      }
      }
      `;

      export default graphql(PICTURES_QUERY)(HomeScreen);


      The server-side code, just in case:



      pictures: async (_, { offset, limit }) => {
      let picturesDB = getConnection()
      .getRepository(Picture)
      .createQueryBuilder("p");
      return picturesDB
      .take(limit)
      .skip(offset)
      .getMany();

      }


      I have added a default parameter in my GraphQL schema:



      type Query {
      pictures(offset: Int, limit: Int = 3): [Picture!]!
      }






      react-native graphql react-apollo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 0:17









      Adam Thalhammer

      31




      31










      asked Nov 23 '18 at 3:36









      Hendry LimHendry Lim

      3831414




      3831414
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Managed to pass the limit variable using Apollo HOC pattern...



          export default graphql(PICTURES_QUERY, {
          options: () => ({
          variables: {
          limit: limitAmt
          }
          })
          })(HomeScreen);





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440377%2fapollo-graphql-pagination-failed-with-limit%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Managed to pass the limit variable using Apollo HOC pattern...



            export default graphql(PICTURES_QUERY, {
            options: () => ({
            variables: {
            limit: limitAmt
            }
            })
            })(HomeScreen);





            share|improve this answer




























              0














              Managed to pass the limit variable using Apollo HOC pattern...



              export default graphql(PICTURES_QUERY, {
              options: () => ({
              variables: {
              limit: limitAmt
              }
              })
              })(HomeScreen);





              share|improve this answer


























                0












                0








                0







                Managed to pass the limit variable using Apollo HOC pattern...



                export default graphql(PICTURES_QUERY, {
                options: () => ({
                variables: {
                limit: limitAmt
                }
                })
                })(HomeScreen);





                share|improve this answer













                Managed to pass the limit variable using Apollo HOC pattern...



                export default graphql(PICTURES_QUERY, {
                options: () => ({
                variables: {
                limit: limitAmt
                }
                })
                })(HomeScreen);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 3:54









                Hendry LimHendry Lim

                3831414




                3831414
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440377%2fapollo-graphql-pagination-failed-with-limit%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    RAC Tourist Trophy