question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Switch from gql-sketch to react-apollo

See original GitHub issue

Tackling more ambitious graphql usecases requires standardizing on a client.

import React from 'react';
import { render, Text, View } from 'react-sketchapp';
import { ApolloClient, ApolloProvider, createNetworkInterface, gql, graphql } from 'react-apollo';
import type { User } from './types';
import { fonts, spacing } from './designSystem';
import Profile from './components/Profile';
import Space from './components/Space';

global.setTimeout = (fn, time) => {
  NSThread.sleepForTimeInterval(time / 1000);
  fn();
}

const UserList = ({ users }) =>
  <View style={{ flexDirection: 'row', flexWrap: 'wrap', width: users.length * 300 }}>
    {users.map(user => (
      <Space key={user.screen_name} h={spacing} v={spacing}>
        <Profile user={user} />
      </Space>
    ))}
  </View>

const Page = ({ data }) => (
  <View>
    <Text style={fonts['Title 1']}>Profile Cards w/ GraphQL</Text>
    { data.allProfiles && <UserList users={data.allProfiles} /> }
  </View>
);

const ProfilesQuery = gql`query allProfiles {
  allProfiles {
    screenname,
    name,
    description,
    location,
    url,
    profileImageUrl,
  }
}`;

const PageWithData = graphql(ProfilesQuery, {
  options: {
    fetchPolicy: 'network-only',
  },
})(Page);

const client = new ApolloClient({
  networkInterface: createNetworkInterface({
    uri: 'https://api.graph.cool/simple/v1/cj09zm1k4jcpc0115ecsoc1k4',
  }),
});

export default (context) => {
  render(
    <ApolloProvider client={client}>
      <PageWithData />
    </ApolloProvider>
  , context.document.currentPage())
}

Unfortunately, this doesn’t work — we only render once, and we can’t force react-apollo to render synchronously.

A way around this could be to have separate entry points for Sketch & non-Sketch to components -

/* baseComponent.js */
const Component = ({ data }) =>
  <div>{ data.message }</div>

export const query = `query message { }`;
export default Component;

/* Component.js */
import { graphql, gql } from 'react-apollo';
import Component, { query } from './baseComponent';

const query = gql`${query}`;
export default graphql(query)(Component)

/* Component.sketch.js */
import Component, { query } from './baseComponent';
export const query = query;
export default Component

/* index.js */
import Component from './Component';

render(<ApolloProvider client={client}><Component></ApolloProvider>, $('#root'));

/* index.sketch.js */
import Component, { query } from './Component';
import Client from 'gql-sketch';

Client(endpoint).query(query).then(data =>
  <Component data={data} />
) 

just typing this this seems like a mess so mayyybe not 🗡

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mathieudutourcommented, May 15, 2017

How about:

import { ApolloClient, ApolloProvider, createNetworkInterface, gql, graphql, getDataFromTree } from 'react-apollo';

const client = new ApolloClient({
  ssrMode: true,
  networkInterface: createNetworkInterface({
    uri: 'https://api.graph.cool/simple/v1/cj09zm1k4jcpc0115ecsoc1k4',
  }),
});

const app = (
    <ApolloProvider client={client}>
      <PageWithData />
    </ApolloProvider>
)

export default (context) => {
  return getDataFromTree(app).then(() => {
    render(app, context.document.currentPage())
  })
}

Basically, doing a server side rendering.

0reactions
jbaxleyiiicommented, Jun 21, 2017

@jongold @mathieudutour I’m working on this now 👍 I can’t get my local plugin to not break whenever I import something though 👎

Will keep you posted!

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL On The Front-End (React And Apollo)
In this article, we're going to learn how to handle real-time updates on the client-side using GraphQL. We'll be learning how to do...
Read more >
Migrating to Apollo Client 3.0 - Apollo GraphQL Docs
This article walks you through migrating your application to Apollo Client 3.0 from previous versions of Apollo Client. To illustrate the migration process, ......
Read more >
apollo react: proper way to switch a query's params
For completeness here's my VIDEO_SEARCH_QUERY : export const VIDEO_SEARCH_QUERY = gql` query searchVideos($query: String) { searchVideos(query: ...
Read more >
React Hooks support · Issue #2539 · apollographql/react-apollo
I'd like to start a discussion about the possible next-gen API based on react hooks for react-apollo. I think this API brings a...
Read more >
Using Apollo Client with a REST API | by November Five
For the actual basic setup of the Apollo Client in your React ... Now that we have 2 queries in users.gql , we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found