All Projects β†’ department-stockholm β†’ apollo-component

department-stockholm / apollo-component

Licence: ISC license
A render component for easy querying and mutating of your GraphQL API.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to apollo-component

Fullstack Apollo Express Postgresql Boilerplate
πŸ’₯ A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
Stars: ✭ 1,079 (+7607.14%)
Mutual labels:  apollo-client, apollographql
Fullstack Apollo React Boilerplate
πŸ’₯A sophisticated Apollo in React boilerplate project.
Stars: ✭ 136 (+871.43%)
Mutual labels:  apollo-client, apollographql
Graphql Codegen Hasura
code-generator plugins for hasura/apollo-gql/typescript development
Stars: ✭ 113 (+707.14%)
Mutual labels:  apollo-client, apollographql
Link state demo
πŸš€ Demonstrate how to support multiple stores in Apollo Link State
Stars: ✭ 30 (+114.29%)
Mutual labels:  apollo-client, apollographql
jest-gql
βœ…πŸš€GraphQL based tests for Jest and Apollo
Stars: ✭ 33 (+135.71%)
Mutual labels:  apollo-client, apollographql
Githunt React
[DEPRECATED] πŸ”ƒ An example app frontend built with Apollo Client and React
Stars: ✭ 1,036 (+7300%)
Mutual labels:  apollo-client, apollographql
React Mobx State Tree
Create React App with MobX State Tree, Styled Components and GraphQL
Stars: ✭ 127 (+807.14%)
Mutual labels:  apollo-client, apollographql
angular-apollo-starter
Learning apollo-client, graphql with angular6.x
Stars: ✭ 16 (+14.29%)
Mutual labels:  apollo-client, apollographql
pokedex-react
A Pokedex App using and teaching Apollo and React
Stars: ✭ 47 (+235.71%)
Mutual labels:  apollo-client, apollographql
GitHunt-Polymer
An example of a client-side app built with Polymer and Apollo Client.
Stars: ✭ 13 (-7.14%)
Mutual labels:  apollo-client, apollographql
Apollo Link Maxage
An Apollo Link to invalidate cached queries
Stars: ✭ 23 (+64.29%)
Mutual labels:  apollo-client, apollographql
gitstar
Github Client built with React Apollo
Stars: ✭ 15 (+7.14%)
Mutual labels:  apollo-client, apollographql
Fullstack Apollo Express Mongodb Boilerplate
πŸ’₯A sophisticated GraphQL with Apollo, Express and MongoDB boilerplate project.
Stars: ✭ 301 (+2050%)
Mutual labels:  apollo-client, apollographql
Apollo Angular
A fully-featured, production ready caching GraphQL client for Angular and every GraphQL server 🎁
Stars: ✭ 1,058 (+7457.14%)
Mutual labels:  apollo-client, apollographql
Apollo Client
πŸš€ Β A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
Stars: ✭ 17,070 (+121828.57%)
Mutual labels:  apollo-client, apollographql
React Graphql Github Apollo
πŸš€ A React + Apollo + GraphQL GitHub Client. Your opportunity to learn about these technologies in a real world application.
Stars: ✭ 1,563 (+11064.29%)
Mutual labels:  apollo-client, apollographql
now-course
Proyecto para el curso de Now.sh en Platzi
Stars: ✭ 19 (+35.71%)
Mutual labels:  apollo-client, apollographql
glimmer-apollo
Ember and Glimmer integration for Apollo Client.
Stars: ✭ 32 (+128.57%)
Mutual labels:  apollo-client, apollographql
fullstack-apollo-subscription-example
A minimal Apollo Server 2 with Apollo Client 2 with Subscriptions application.
Stars: ✭ 72 (+414.29%)
Mutual labels:  apollo-client, apollographql
GraphQL-Android
A project for showing use of apollo-android for consuming github graphQL API
Stars: ✭ 51 (+264.29%)
Mutual labels:  apollo-client, apollographql

Apollo Component

npm version

A render component for easy querying and mutating of your GraphQL API.

Install

npm install @department/apollo-component

Examples

Here is an example of fetching and rendering the data of an imaginary Order.

import { gql } from "graphql-tag"
import { Query } from "@department/apollo-component"
import SingleOrder, { LoadingOrder } from "components/SingleOrder"
import { GenericError, NotFound } from "components/Errors"

const ShowOrderQuery = gql`
  query ShowOrder($id: ID!) {
    Order(id: $id) {
      ...SingleOrder
    }
  }
  ${SingleOrder.fragments.SingleOrder}
`;

const Show = ({ id }) => (
  <Query gql={ShowOrderQuery} variables={{id}}>
    {({ data: { Order }, loading, error, refetch }) =>
      loading ? (
        <LoadingOrder />
      ) : error ? (
        <GenericError error={error} />
      ) : (
        <SingleOrder {...Order} update={refetch} />
      )}
  </Query>
));

And another example using <Mutate/> and fail-props to raise any errors to the nearest React 16+ Error Boundary:

import { gql } from "graphql-tag";
import { Mutate, Query } from "@department/apollo-component";
import { Exception } from "components/Exception";

const IncrementMutation = gql`
  mutation IncrementMutation($num: Int!) {
    incr(num: $num)
  }
`;

const ShowCountQuery = gql`
  query ShowCount {
    count
  }
`;

const IncrementView = ({ id }) => (
  <Exception>
    <Query gql={ShowCount} wait fail>
      {({ data: { count } }) => <div>Current count: {count}</div>}
    </Query>
    <Mutate gql={IncrementMutation} refetchQueries={["ShowCount"]} fail>
      {incr => (
        <form onSubmit={e => incr({ num: e.currentTarget.num.valueAsNumber })}>
          <input type="number" name="num" value={1} step={1} />
          <button>+</button>
        </form>
      )}
    </Mutate>
  </Exception>
);

API

<Query />

Available Props:

  • gql
  • wait
  • lazy
  • fail
  • skip
  • variables

Arguments in render callback

  • QueryResults
    • data the loaded data or an empty object
    • loading true while loading (unless the wait-prop was set)
    • skipped true if the request was skipped (using the skip-prop)
    • error Error object if there was any error (unless the fail-props was set)
    • refetch(variables) call this function rerun query with, optionally, new variables
    • fetchMore(opts) call this function to fetch more (read about the opts)
Example:
({ data: { stuff }, loading }) => <div>{loading ? "loading..." : stuff}</div>;

<Mutate />

Available Props:

  • gql
  • refetchQueries
  • optimisticResponse
  • update

Arguments in render callback

  • Mutate(variables) call this function to trigger the mutation
  • QueryResults (like for Query but without skipped)
    • data
    • loading
    • error
    • refetch
    • fetchMore
Example:
(mutate, { data: { stuff }, loading }) => (
  <button onClick={() => mutate()} disabled={loading}>
    {loading ? "loading..." : stuff}
  </button>
);

renderState(client, component, options)

For server side rendering renderState() may be used. It uses a query queue in the React Context which is populated by <Query/> in its componentWillMount() lifecycle method using a naΓ―ve component renderer.

It will by default render the tree repeatedly until all queries, without the lazy-prop has been completed but the maximum "depth" may be adjusted with the maxDepth option.

Arguments

  • client an instance of ApolloClient
  • component the root component, will be wrapped by a <Provider /> with a queue
  • RenderStateOptions
    • maxDepth number of attempts to render the tree, defaults to Infinity (until no more queries are to be found)

Example

An example which could be used to server side render.

Note how the <Provider/> is not needed in renderState(), it's because it wraps the component with a special one.

function renderHTML() {
  return renderState(client, <App />, { maxDepth: 1 })
    .then(() =>
      ReactDOM.renderToString(
        <Provider client={client}>
          <App />
        </Provider>
      )
    )
    .catch(err => {
      // you can let the error throw here
      // or ignore it and let the client side
      // handle it inline
      console.error("failed to render state:", err);
      return renderErrorPage(err);
    });
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].