Realtime GraphQL UI Updates in React with Apollo. ― Scotch

Realtime GraphQL UI updates in React with Apollo  #ReactJS #GraphQL

  • // ChannelsListWithData = graphql(channelsListQuery, { options: { pollInterval: 5000 a new channel from another client now updates our list of channels after 5 seconds, not the best way of doing it but a step in making the application realtime.
  • store.writeQuery({ query: channelsListQuery, data }); – } – }) – … – As soon as the mutation completes, we read the current result for the channelsListQuery out of the store, append the new channel to it, and tell Apollo Client to write it back to the store.
  • To add WebSocket support to our existing interface, we will construct a GraphQL Subscription client and merge it with our existing network interface to create a new interface that performs normal GraphQL queries over HTTP and subscription queries over WebSockets.
  • … – We edit the following part to disable const ChannelsListWithData = graphql(channelsListQuery, { options: { pollInterval: 5000 }})(ChannelsList); – … – Edit the above to: const ChannelsListWithData = to add a new channel now in our app does not reflect the changes until we refresh the page and that…
  • jsx – … – import { SubscriptionClient, addGraphQLSubscriptions } from networkInterface = createNetworkInterface({ – uri: wsClient = new { – reconnect: true – }); – – // Extend the network interface with the WebSocket – const = addGraphQLSubscriptions( – networkInterface, – wsClient – ); – To enable subscriptions throughout our…

This is the fourth and final part of a 4-part series of articles explaining the implementation of GraphQL on an Express Server and similarly in a React client using Apollo. In this article we look at how to maintain realtime UI updates after implementing different GraphQL updates that result in either creation, update or deletion of data on the server side. For part 1, 2 and 3, refer to the links:
Continue reading “Realtime GraphQL UI Updates in React with Apollo. ― Scotch”

Mocking API responses with React Apollo – Carlos Martinez – Medium

  • Mocking API responses with React ApolloApollo Client and the GraphQL tools that have been made open source within the last years are pretty awesome, the community that is being builded around them is great.
  • However, as with every new technology, there are a lot of things that need to mature and improve.Documentation on testing is definitely one of the bits that needs a lot of improvements, it isn’t clear what to do, and I personally found myself digging into the source code to understand what to do.One of the problems I faced was that I wanted to use mock responses when testing my app components, and there are at least two ways of doing this:The first way is to use graphql-tools.
  • From their docs:This package allows you to use the GraphQL schema language to build your GraphQL.js schema, and also includes useful schema tools like per-type mocking.You can use this library together with a custom MockNetworkInterface to return mocked responses to your components:const { mockServer } = require(“graphql-tools”);const { print } = MockNetworkInterface { constructor(schema, mocks = {}) { if (!
  • schema) { throw new Error(‘Cannot create Mock Api without specifying a schema’); } this.mockServer = mockServer(schema, mocks); } query(request) { return request.variables); }}This is nice, and very flexible, with graphql-tools you can do pretty much whatever you do in your actual API.What I didn’t like about this approach is that you need your schema in sync with the schema defined in your API (you can do this making an instrospection query to your backend), I also found this approach overcomplicates things unnecessarily.The second (and simpler) approach I found was to use the mockNetworkInterface class, this class is defined in react-apollo/test-utils, you can have a peek on it here.To use it, you first need to define some Mock Requests (these requests need to be exactly the same your components will make):const GraphQLMocks = [ { request: { query: UserProfileQuery, variables: {} }, result: { data: { current_user: { id: 1, first_name: ‘Foo’, last_name: ‘Bar’, email: ‘’ } } } }];The UserProfileQuery is exported from your component:export const UserProfileQuery = gql` query userProfile { current_user { id first_name last_name email } }`;And this is how you use it to setup your tests:import { mockNetworkInterface } from ApolloClient from ‘apollo-client’;import { ApolloProvider } from ‘react-apollo’;import { mount } from ‘enzyme’;import App from ‘.
  • /App.js’;import React from ‘react’;const setupTests = () = { const networkInterface = // or mocks); const client = new ApolloClient({ networkInterface, addTypename: false }); const wrapper = mount( ApolloProvider client={client} App / /ApolloProvider ); return { wrapper };};It is important that you pass addTypename: false to ApolloClient, otherwise you would have to include a __typename field in all your mocks and queries.Now any queries your components make will be handled by your mocked responses, you have full control of what you want to return.And that’s it!

Apollo Client and the GraphQL tools that have been made open source within the last years are pretty awesome, the community that is being builded around them is great. However, as with every new…
Continue reading “Mocking API responses with React Apollo – Carlos Martinez – Medium”

A stronger (typed) React Apollo – Apollo GraphQL

A stronger (typed) React Apollo:  #ReactJS #GraphQL

  • Helping developers understand what they can use in their React components was our first priority when we set up flow types for Apollo.Let’s take for example a simple React component wrapped with the graphql HOC from React Apollo.
  • This component will display a hero from Star Wars and their friends with data returned from a GraphQL endpoint:The above code pulls some data from a GraphQL API using a query and includes lifecycle information, such as loading and error information.
  • We tell Flow what the shape of the result will look like from the server when the graphql enhancer wraps a component.
  • If you already are a user of Flow and have your data typed already, with a couple of imports and a single type addition, you can type any component that is wrapped with graphql!Currently Flow does not support the ES7 decorator syntax so you won’t be able to get all of the benefits of stronger type support if you are using decorators.
  • Let’s make a change to our initial component that converts it to use a prop called episode:We can define the props that our exported component will need (InputProps) and attach them to our enhancer using the second type generic of OperationComponent.

Static typing can be low-hanging fruit to improve your app. As data flows throughout the application, it is easy to forget its shape, and even for what it’s used. Although you could have unit tests…
Continue reading “A stronger (typed) React Apollo – Apollo GraphQL”

[React Native 016.3] Using GraphCool with React Native

Using @graphcool with @reactnative -  #reactNative #programming

  • The second screen is a screen to create a post, by passing the image URL and the post description.
  • The rest of the ListPage component is just a list showing each Post and its image.
  • Let’s use this mutation and create a new Post:

    Once we run this mutation, we can refresh our app and see this new Post we have created in our app.

  • On the inline code, we will put our code, it does a POST request to our slack, and we will pass the post description and image url.
  • Let’s save, that, open our slack and create a new Post in our App.

A serverless solution for GraphQL that works great with React Native
Continue reading “[React Native 016.3] Using GraphCool with React Native”

Simplify your React components with Apollo and Recompose

Simplify your #ReactJS components with Apollo and Recompose:  #JavaScript #frontend

  • Here are some things you can do with Recompose:Optimize your React components by making them pure-renderedSet default propsAdd limited state variablesAnd you can do all of this while using the React functional component syntax, which makes your code much more straightforward and reduces your chances of accidentally introducing some state or complexity into your UI rendering.The React higher order component you use from react-apollo fits this mold.
  • It just does one thing well: It enables you to co-locate your GraphQL query with your React component and get that data in your props, while handling all of the details like fetching and caching the data.
  • That’s where React component composition and Recompose come in.We can attach GraphQL data, loading management, variable state, or pure rendering in just one function call!Without further ado, here is our first end-to-end example of how to use Recompose together with React Apollo to have a nice, concise UI with GraphQL!Manage your variables using ‘withState’The best feature of functional components is that they are stateless.
  • However, sometimes you still want some temporary state, and it’s best to put that outside of the component that handles rendering to keep things clean.Let’s look how we can use the withState container from Recompose to build a React component that has both a search box and a GraphQL query that uses that search term:One nice side effect is that since we are keeping all of the state and data loading outside of the component, we can drop in the pure mix-in everywhere to make sure our components don’t rerender when they don’t need to.In the above code snippet, all of our concerns are very nicely separated through the React component paradigm.
  • We could also use an HoC to flatten this out:Now, we can very easily share a common loading component between all of our UI that uses data from React-Apollo, and the logic is contained in a separate chunk that isn’t obscuring the structure of our pure component!Take-awayThe react-apollo API was designed to be the best way to pull GraphQL data into the props of a React component.

One of the main goals of Apollo Client and the Apollo project in general is to fit seamlessly into the modern React developer’s toolbox. That’s why we’ve invested in simple higher-order components…
Continue reading “Simplify your React components with Apollo and Recompose”

Apollo on React Native Radio – Apollo GraphQL

  • For example, Apollo Client is a caching JavaScript client for GraphQL. React Apollo is a really popular way to use GraphQL data in your React application.
  • It’s really natural to use your Redux data as input into your query results.
  • It also works with some of these other libraries that help manage Redux data.
  • It’s actually designed especially for people who like the insight into their app data that Redux gives you, and people that are already using Redux in their app.
  • I guess before we get too deep into some of this technical stuff, for people that are listening that maybe haven’t used Apollo.

Tom Coleman and I went on the December 5 episode of React Native Radio, and we got asked some really great questions. You can listen to the episode on, but if you don’t want to take an…
Continue reading “Apollo on React Native Radio – Apollo GraphQL”

React Native with Apollo — Part 2, Apollo Client – React Native Development – Medium

  • We return the App component we will create next and wrap it in passing in the client as a prop.
  • create a new query using gql that we will use when we create the higher order component
  • gql will be used to create our query.
  • The graphql method will allow us to create a higher order component for providing Apollo data to a component.
  • create a component named President that we will pass to the higher order component along with the query

Here, we will be using the Apollo GraphQL server we created in part 1 to retrieve data from our database and render it in our app.
Continue reading “React Native with Apollo — Part 2, Apollo Client – React Native Development – Medium”

GraphQL with the Serverless Framework

  • These two libraries form the core of the Web client for this project.
  • The Lambda function retrieves data from DynamoDB and returns it to the client via API Gateway.
  • There are already great libraries for retrieving data from GraphQL endpoints.
  • Letting the client choose what data to fetch is more efficient and flexible.
  • GraphQL allows the client to select ad-hoc data.

Building a dashboard for my garden
Continue reading “GraphQL with the Serverless Framework”