Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/carlosstrand/graphql-pagination-go

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/carlosstrand/graphql-pagination-go

  • v0.1.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

Documentation Actions Status Coverage Status

graphql-pagination-go

This library makes it easy to create paginated fields for graphql-go. We currently have the following features:

  • Simple Pagination (data & count)
  • Separated data and count resolvers
  • Resolvers are executed only for requested fields
  • Skip and Limit

Example:

  fields := graphql.Fields{
    "languages": pagination.Paginated(&pagination.PaginatedField{
      Name: "Languages",
      Type: graphql.String,
      Args: nil,
      DataResolve: func(p graphql.ResolveParams, page pagination.Page) (i interface{}, e error) {
          return []string{"Go", "Javascript", "Ruby"}, nil
      },
      CountResolve: func(p graphql.ResolveParams, page pagination.Page) (i interface{}, e error) {
          return 3, nil
      },
    }),
  }
  rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
  schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}

Now you can query as below:

  query {
    languages(limit: 10, skip: 20) {
      data
      count
    }
  }

Skip and Limit

This library already has the limit and skip arguments ready to be used in a query with the database or external service. See the following example:

  var DataResolver = func(p graphql.ResolveParams, page pagination.Page) (i interface{}, e error) {
      users, err := users.FindMany(db.Filter{
        Limit: page.Limit,
        Skip: page.Skip,
      })
      if err != nil {
        return nil, err
      }
      return users, nil
  }

Resolve only requested fields

In some datasources or databases like MongoDB, calling a count comes at an additional cost and is not always used. Thus, this library takes care of resolvers only of the requested fields (data and / or count).

  query {
    languages {
      count
    }
  }

As you can see in example above, only the CountResolve you be called and the query will not have the cost of calling DataResolve because data was not requested.

Resolve Data and Count at once

If your API or your Database already returns Data and Count, you can use DataAndCountResolver. Example:

  fields := graphql.Fields{
    "languages": pagination.Paginated(&PaginatedField{
        Name: "Languages",
        Type: graphql.String,
        Args: nil,
        DataAndCountResolve: func(p graphql.ResolveParams, page Page) (interface{}, int, error) {
            return []string{"Go", "Javascript", "Ruby", "Elixir"}, 4, nil
        },
    }),
  }

FAQs

Package last updated on 28 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc