Skip to content

Commit

Permalink
Merge pull request #327 from cofacts/rollback-apollo
Browse files Browse the repository at this point in the history
Rollback apollo-client to 2.X
  • Loading branch information
MrOrz authored Sep 3, 2020
2 parents ddf23aa + 40b8618 commit b047152
Show file tree
Hide file tree
Showing 41 changed files with 402 additions and 176 deletions.
3 changes: 2 additions & 1 deletion components/AppLayout/AppHeader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { gql, useQuery } from '@apollo/client';
import gql from 'graphql-tag';
import { c, t } from 'ttag';

import {
Expand Down Expand Up @@ -31,6 +31,7 @@ import { NAVBAR_HEIGHT, TABS_HEIGHT } from 'constants/size';
import { EDITOR_FACEBOOK_GROUP } from 'constants/urls';
import desktopLogo from './images/logo-desktop.svg';
import mobileLogo from './images/logo-mobile.svg';
import { useQuery } from '@apollo/react-hooks';

const MENU_BUTTON_WIDTH = 48;

Expand Down
3 changes: 2 additions & 1 deletion components/AppLayout/AppLayout.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { Fragment, useState, useEffect, useCallback } from 'react';
import { gql, useLazyQuery } from '@apollo/client';
import Container from '@material-ui/core/Container';
import { makeStyles } from '@material-ui/core/styles';
import Router from 'next/router';
import { pushToDataLayer } from 'lib/gtm';
import AppHeader from './AppHeader';
import AppSidebar from './AppSidebar';
import AppFooter from './AppFooter';
import gql from 'graphql-tag';
import { useLazyQuery } from '@apollo/react-hooks';
import LoginModal from './LoginModal';
import fetchAPI from 'lib/fetchAPI';

Expand Down
3 changes: 2 additions & 1 deletion components/AppLayout/UserName.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @todo: remove this. this components is deprecated and will be replaced by new UI components
import React, { PureComponent, useState, useCallback, useEffect } from 'react';
import { t } from 'ttag';
import { gql, useLazyQuery, useMutation } from '@apollo/client';
import gql from 'graphql-tag';
import { useLazyQuery, useMutation } from '@apollo/react-hooks';
import Link from 'next/link';
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
Expand Down
2 changes: 1 addition & 1 deletion components/AppLayout/Widgets/Avatar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import cx from 'clsx';
import { makeStyles, withStyles } from '@material-ui/core/styles';
import { Badge } from '@material-ui/core';
Expand Down
2 changes: 1 addition & 1 deletion components/ArticleCategories/AddCategoryDialog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { t } from 'ttag';
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import { Dialog, DialogTitle, DialogContent } from '@material-ui/core';
import useCurrentUser from 'lib/useCurrentUser';

Expand Down
5 changes: 3 additions & 2 deletions components/ArticleCategories/ArticleCategories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState } from 'react';
import { gql, useQuery } from '@apollo/client';
import gql from 'graphql-tag';
import getConfig from 'next/config';
import { t } from 'ttag';
import { useQuery } from '@apollo/react-hooks';
import { Box, Chip } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import AddIcon from '@material-ui/icons/Add';
Expand Down Expand Up @@ -54,7 +55,7 @@ function ArticleCategories({ articleId, articleCategories }) {
{}
);

const allCategories = (data?.ListCategories?.edges || []).map(
const allCategories = (data.ListCategories?.edges || []).map(
({ node }) => node
);
const hasOtherCategories = allCategories.some(({ id }) => !isInArticle[id]);
Expand Down
2 changes: 1 addition & 1 deletion components/ArticleCategories/ArticleCategory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import Chip from '@material-ui/core/Chip';

import { makeStyles } from '@material-ui/core/styles';
Expand Down
3 changes: 2 additions & 1 deletion components/ArticleCategories/CategoryOption.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState } from 'react';
import { t } from 'ttag';
import { gql, useMutation } from '@apollo/client';
import gql from 'graphql-tag';
import Link from 'next/link';
import { useMutation } from '@apollo/react-hooks';
import {
Box,
Chip,
Expand Down
3 changes: 2 additions & 1 deletion components/ArticleCategories/DownVoteDialog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { t } from 'ttag';
import { gql, useQuery } from '@apollo/client';
import gql from 'graphql-tag';
import { useQuery } from '@apollo/react-hooks';

import DialogTitle from '@material-ui/core/DialogTitle';
import Dialog from '@material-ui/core/Dialog';
Expand Down
2 changes: 1 addition & 1 deletion components/ArticleInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import { t, ngettext, msgid } from 'ttag';
import { makeStyles } from '@material-ui/core/styles';
import { TYPE_ICON } from 'constants/replyType';
Expand Down
3 changes: 2 additions & 1 deletion components/ArticlePageLayout.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gql, useQuery } from '@apollo/client';
import gql from 'graphql-tag';
import { t, jt } from 'ttag';
import { useRouter } from 'next/router';
import { useQuery } from '@apollo/react-hooks';

import Box from '@material-ui/core/Box';
import Typography from '@material-ui/core/Typography';
Expand Down
2 changes: 1 addition & 1 deletion components/ArticleReply/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback } from 'react';
import { t, jt } from 'ttag';
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import { Box, Divider } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from 'react';
import { t } from 'ttag';
import { gql, useMutation } from '@apollo/client';
import gql from 'graphql-tag';
import { useMutation } from '@apollo/react-hooks';
import { makeStyles, withStyles } from '@material-ui/core/styles';
import { Tabs, Tab, Box, Popover, Typography } from '@material-ui/core';
import Snackbar from '@material-ui/core/Snackbar';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { MockedProvider } from '@apollo/client/testing';
import { MockedProvider } from '@apollo/react-testing';
import ArticleReplyFeedbackControl from './';
import { CREATE_REPLY_FEEDBACK } from './ArticleReplyFeedbackControl';
import { USER_QUERY } from 'lib/useCurrentUser';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { t } from 'ttag';
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import { makeStyles } from '@material-ui/core/styles';
import { ButtonGroup, Button } from '@material-ui/core';
import useCurrentUser from 'lib/useCurrentUser';
Expand Down
3 changes: 2 additions & 1 deletion components/CreateReplyRequestForm/CreateReplyRequestForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useRef } from 'react';
import { t } from 'ttag';
import { gql, useMutation } from '@apollo/client';
import { useMutation } from '@apollo/react-hooks';
import {
ButtonGroup,
Button,
Expand All @@ -10,6 +10,7 @@ import {
MenuItem,
} from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import gql from 'graphql-tag';
import Avatar from 'components/AppLayout/Widgets/Avatar';
import Hint from 'components/NewReplySection/ReplyForm/Hint';
import useCurrentUser from 'lib/useCurrentUser';
Expand Down
3 changes: 2 additions & 1 deletion components/CurrentReplies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useCallback } from 'react';
import { gql, useMutation } from '@apollo/client';
import gql from 'graphql-tag';
import { t, jt, ngettext, msgid } from 'ttag';
import { useMutation } from '@apollo/react-hooks';

import DialogTitle from '@material-ui/core/DialogTitle';
import Dialog from '@material-ui/core/Dialog';
Expand Down
2 changes: 1 addition & 1 deletion components/Feedback.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import { makeStyles } from '@material-ui/core/styles';
import Avatar from 'components/AppLayout/Widgets/Avatar';
import { Box } from '@material-ui/core';
Expand Down
3 changes: 2 additions & 1 deletion components/Hyperlinks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gql, useQuery } from '@apollo/client';
import gql from 'graphql-tag';
import { Box, SvgIcon, CircularProgress } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import { useQuery } from '@apollo/react-hooks';

const useStyles = makeStyles(theme => ({
root: {
Expand Down
3 changes: 2 additions & 1 deletion components/ListPageControls/CategoryFilter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { memo } from 'react';
import { useRouter } from 'next/router';
import { t } from 'ttag';
import { gql, useQuery } from '@apollo/client';
import gql from 'graphql-tag';
import { useQuery } from '@apollo/react-hooks';

import { goToUrlQueryAndResetPagination } from 'lib/listPage';
import BaseFilter from './BaseFilter';
Expand Down
2 changes: 1 addition & 1 deletion components/ListPageDisplays/ArticleItem.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import Link from 'next/link';
import { c, t } from 'ttag';
import { makeStyles } from '@material-ui/core/styles';
Expand Down
2 changes: 1 addition & 1 deletion components/ListPageDisplays/ReplyItem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeStyles } from '@material-ui/core/styles';
import { Box } from '@material-ui/core';
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import { t } from 'ttag';
import { TYPE_NAME } from 'constants/replyType';
import Avatar from 'components/AppLayout/Widgets/Avatar';
Expand Down
2 changes: 1 addition & 1 deletion components/ListPageDisplays/ReplySearchItem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react';
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import { t, msgid, ngettext } from 'ttag';
import { makeStyles } from '@material-ui/core/styles';
import {
Expand Down
3 changes: 2 additions & 1 deletion components/NewReplySection/ReplySearch/ReplySearch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useContext } from 'react';
import { gql, useLazyQuery } from '@apollo/client';
import { useLazyQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';
import { t } from 'ttag';

import getDedupedArticleReplies from 'lib/getDedupedArticleReplies';
Expand Down
3 changes: 2 additions & 1 deletion components/NewReplySection/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useContext } from 'react';
import { gql, useMutation } from '@apollo/client';
import { useMutation } from '@apollo/react-hooks';
import gql from 'graphql-tag';
import { t } from 'ttag';

import { Box } from '@material-ui/core';
Expand Down
6 changes: 3 additions & 3 deletions components/RelatedReplies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import { t } from 'ttag';
import { Box, SvgIcon, Button } from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import { gql } from '@apollo/client';
import Link from 'next/link';
import gql from 'graphql-tag';

import { TYPE_NAME, TYPE_DESC } from 'constants/replyType';
import ExpandableText from './ExpandableText';
import { linkify, nl2br } from 'lib/text';
import Link from 'next/link';
import PlainList from 'components/PlainList';
import ExpandableText from './ExpandableText';

const useStyles = makeStyles(theme => ({
root: {
Expand Down
2 changes: 1 addition & 1 deletion components/ReplyInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import { t, ngettext, msgid } from 'ttag';
import Link from 'next/link';
import Infos, { TimeInfo } from './Infos';
Expand Down
3 changes: 2 additions & 1 deletion components/ReplyRequestReason/ReplyRequestReason.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { gql, useMutation } from '@apollo/client';
import { useMutation } from '@apollo/react-hooks';
import { makeStyles } from '@material-ui/core/styles';
import { Box, SvgIcon, Divider } from '@material-ui/core';
import gql from 'graphql-tag';
import PropTypes from 'prop-types';
import cx from 'clsx';

Expand Down
3 changes: 2 additions & 1 deletion components/ReplySearchPageLayout.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gql, useQuery } from '@apollo/client';
import gql from 'graphql-tag';
import { t } from 'ttag';
import { useRouter } from 'next/router';
import { useQuery } from '@apollo/react-hooks';

import Box from '@material-ui/core/Box';
import { makeStyles } from '@material-ui/core/styles';
Expand Down
2 changes: 1 addition & 1 deletion components/UsedArticleItem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { t, jt } from 'ttag';
import Link from 'next/link';
import { gql } from '@apollo/client';
import gql from 'graphql-tag';
import isValid from 'date-fns/isValid';
import { format, formatDistanceToNow } from 'lib/dateWithLocale';
import EditorName from 'components/EditorName';
Expand Down
72 changes: 29 additions & 43 deletions lib/apollo.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {
InMemoryCache,
defaultDataIdFromObject,
ApolloLink,
ApolloClient,
} from '@apollo/client';
import { withApollo } from 'next-apollo';
import { BatchHttpLink } from '@apollo/client/link/batch-http';
import { onError } from '@apollo/client/link/error';
import { withData } from 'next-apollo';
import { ApolloLink } from 'apollo-link';
import { BatchHttpLink } from 'apollo-link-batch-http';
import { onError } from 'apollo-link-error';
import { InMemoryCache, defaultDataIdFromObject } from 'apollo-cache-inmemory';
import getConfig from 'next/config';
import { t } from 'ttag';
import { headers } from './fetchAPI';
Expand All @@ -32,38 +28,28 @@ export function dataIdFromObject(object) {
}
}

/**
* ApolloClient factory function
* @param {object} options - additional options when creating ApolloClient instance
* @returns {ApolloClient} a brand new instance of ApolloClient
*/
export function createApolloClient(options = {}) {
return new ApolloClient({
link: ApolloLink.from([
onError(errors => {
console.error('[apollo-link-error]', errors);
if (
typeof alert !== 'undefined' &&
errors.graphQLErrors &&
errors.graphQLErrors.length === 1 &&
errors.graphQLErrors[0].message === AUTH_ERROR_MSG
) {
alert(t`Please login first.`);
}
}),
new BatchHttpLink({
uri: `${PUBLIC_API_URL}/graphql`, // Server URL (must be absolute)
headers,
credentials: 'include', // Additional fetch() options like `credentials` or `headers`
}),
]),
cache: new InMemoryCache({ dataIdFromObject }),
...options,
});
}
export const config = {
link: ApolloLink.from([
onError(errors => {
console.error('[apollo-link-error]', errors);
if (
typeof alert !== 'undefined' &&
errors.graphQLErrors &&
errors.graphQLErrors.length === 1 &&
errors.graphQLErrors[0].message === AUTH_ERROR_MSG
) {
alert(t`Please login first.`);
}
}),
new BatchHttpLink({
uri: `${PUBLIC_API_URL}/graphql`, // Server URL (must be absolute)
headers,
credentials: 'include', // Additional fetch() options like `credentials` or `headers`
}),
]),
createCache() {
return new InMemoryCache({ dataIdFromObject });
},
};

export default withApollo(
createApolloClient({
name: 'rumors-site',
})
);
export default withData(config);
3 changes: 2 additions & 1 deletion lib/useCurrentUser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect } from 'react';
import { gql, useLazyQuery } from '@apollo/client';
import { useLazyQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';
import { pushToDataLayer } from './gtm';

export const CurrentUser = gql`
Expand Down
Loading

0 comments on commit b047152

Please sign in to comment.