npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-util

v0.4.0

Published

## ApolloDataWrapper * Reuse in ItemData & ListData * Use as render props Component

Downloads

36

Readme

Apollo Util

ApolloDataWrapper

  • Reuse in ItemData & ListData
  • Use as render props Component
type ApolloDataWrapperProps = {
  data: {
    error?: any,
    loading: boolean,
  },
  dataField?: string,
  renderLoading?: () => any,
  renderGlobalError?: (error: any) => any,
  renderError?: (error: any) => any,
  renderData?: (result: any) => any,
  children?: (result: any) => any,
}

Props

data

data passed from apollo

dataField

Root data name, default to result, assume that we have request only 1. For example

{
  result: post {
    error
    entity {
      _id
      name
    }
  }
}

getAll

If provided, get data return from graphql instead of data.result. Useful when query multiple field

renderLoading: () => Component | string

Custom render loading state

renderGlobalError: () => Component | string

Custom render global error state, ussually wrong syntax or network errors.

renderData?: (result: any) => Component | string

Custom render data

children

Use as custom render data

Example

import React from 'react'
import { graphql } from 'react-apollo'
import gql from 'graphql-tag'

const query = gql`
query ($id: String) {
  result: post(id: $id) {
    error
    entity {
      _id
      name
    }
  }
}
`

// Note that data is passed from Apollo
const PostDetail = ({ data }) => (
  <ApolloData data={data}>
  ({ error, entity }) => (
    <div>{entity.name}</div>
  )
  </ApolloData>
)
export default graphql(query)(PostDetail)

configureApollo({ uri }: { uri: string })

A simple function to create you Apollo Client by only provide an url endpoint

createItemData(query: any, idField: string = '_id')

Example Usage:

// @flow

import React from 'react'
import { createItemDataComponent } from 'apollo-util'

const query = gql`
query ($_id: String) {
  result: post(_id: $_id) {
    error
    entity {
      _id
      name
    }
  }
}
`

const ItemData = createItemDataComponent(query)

function Post({ _id }: { _id: string }) {
  return (
    <ItemData variables={{ _id }}>
      {({ entity }) => (
        <div>
          <h1>{entity.name}</h1>
        </div>
        )}
    </ItemData>
  )
}

export default Post

createListData(query: any, whitelist: string[] = ['page', 'limit', 'total', 'sort'])

// @flow

import React from 'react'
import { createItemDataComponent } from 'apollo-util'
import detailQuery from 'queries/route/detail.gql'

const ItemData = createItemDataComponent(detailQuery)

function Posts({ page, limit, sort }: any) {
  return (
    <ItemData variables={{ page, limit, sort }}>
      {({ entities }) => (
        <div>
          {entities.map(item => (
            <div key={item._id}>{item.name}</div>
          ))}
        </div>
        )}
    </ItemData>
  )
}

export default Posts

createConnectCreateConfig

Use to get the result data & merge it into Apollo cache

createConnectCreateConfig({
  mutationName,
  listQuery,
  getVariables,
}: {
  mutationName: string,
  listQuery?: any,
  getVariables?: (props) => any,
}
  • mutationName Example:
mutation ($name: String!) {
  createPost(name: $name) {
    ...
  }
}

In this case, mutationName must be createPost

  • Apollo cache query by a query & variables that provided to it. listQuery & getVariables is for that purpose

createConnectEditConfig(submitFnName?: string = 'submitEdit')

It will pass a submitEdit function to your component

createConnectRemoveConfig

Use to get the result data & remove it into Apollo cache

createConnectRemoveConfig({
  mutationName,
  listQuery,
  getVariables,
}: {
  mutationName: string,
  listQuery?: any,
  getVariables?: (props) => any,
})

Usage: same as createConnectCreateConfig

TODO

  • [ ] Tests