serlina-apollo

1.0.0 • Public • Published

serlina-apollo

npm downloads

Delightful using Apollo in Serlina.

Usage

npm i apollo-boost react-apollo serlina-apollo isomorphic-fetch --save

Create an apollo client for serlina-apollo:

import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from "apollo-cache-inmemory"
import { createHttpLink } from 'apollo-link-http'
import * as fetch from 'isomorphic-fetch'

const client = (initialState = {}) => {
  return new ApolloClient({
    ssrMode: true,
    cache: new InMemoryCache().restore(initialState),
    link: createHttpLink({
      uri: 'YOUR_URL',
      fetch
    })
  })
}

uri must be an absolute URI. See Apollo document for server-side rendering

Use <Query> in your page or anywhere:

import { Query } from 'react-apollo'
import { withApollo } from 'serlina-apollo'
import * as gql from 'graphql-tag'

export default withApollo(() => {
  return (
    <div>
      <Query query={gql`
        query {
          posts {
            id,
            title
          }
        }
      `}>
        {({ loading, data }) => {
          if (loading) {
            return "loading"
          } else {
            const posts = data.posts || []
            return (
              <div>
                {posts.map(post => {
                  return <p>{post.title}</p>
                })}
              </div>
            )
          }
        }}
      </Query>
    </div>
  )
}, client)

License

MIT License

Readme

Keywords

none

Package Sidebar

Install

npm i serlina-apollo

Weekly Downloads

3

Version

1.0.0

License

none

Unpacked Size

3.36 kB

Total Files

5

Last publish

Collaborators

  • djyde