Skip to content

refactor(repository-screen): switch to GraphQL #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions __tests__/tests/components/RepositoryProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@ import { RepositoryProfile } from 'components';

const defaultProps = {
repository: {
fork: true,
parent: true,
language: 'en',
isFork: true,
parent: {
nameWithOwner: 'foo/bar',
},
primaryLanguage: {
name: 'JavaScript',
color: '#FFCC00',
},
viewerHasStarred: false,
viewerSubscription: 'UNSUBSCRIBED',
},
starred: false,
navigation: {
navigate() {},
},
loading: false,
subscribed: false,
locale: 'en',
};

describe('<RepositoryProfile />', () => {
it('should render the Icon component if loading is false and repository language is not null', () => {
const wrapper = shallow(
<RepositoryProfile {...defaultProps} language={false} />
);
const wrapper = shallow(<RepositoryProfile {...defaultProps} />);
const icon = wrapper.find({ name: 'fiber-manual-record' });

expect(icon.length).toBeTruthy();
Expand All @@ -44,7 +47,7 @@ describe('<RepositoryProfile />', () => {
{...defaultProps}
repository={{
...defaultProps.repository,
language: null,
primaryLanguage: null,
}}
/>
);
Expand All @@ -53,7 +56,7 @@ describe('<RepositoryProfile />', () => {
expect(icon.length).toBeFalsy();
});

it('should render repository fork text container if repository.fork is true', () => {
it('should render repository fork text container if repository.isFork is true', () => {
const wrapper = shallow(<RepositoryProfile {...defaultProps} />);
const repositoryContainer = wrapper.find({
nativeId: 'repository-fork-container',
Expand All @@ -77,7 +80,7 @@ describe('<RepositoryProfile />', () => {
.simulate('press');

expect(navigateMock).toHaveBeenCalledWith('Repository', {
repository: true,
repoId: 'foo/bar',
});
});
});
11 changes: 11 additions & 0 deletions src/api/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ export const ACTIVITY_GET_EVENTS_RECEIVED = createPaginationActionSet(
export const ACTIVITY_GET_STARRED_REPOS_FOR_USER = createPaginationActionSet(
'ACTIVITY_GET_STARRED_REPOS_FOR_USER'
);
export const ACTIVITY_STAR_REPO = createActionSet('ACTIVITY_STAR_REPO');
export const ACTIVITY_UNSTAR_REPO = createActionSet('ACTIVITY_UNSTAR_REPO');
export const ACTIVITY_WATCH_REPO = createActionSet('ACTIVITY_WATCH_REPO');
export const ACTIVITY_UNWATCH_REPO = createActionSet('ACTIVITY_UNWATCH_REPO');
export const SEARCH_REPOS = createPaginationActionSet('SEARCH_REPOS');
export const SEARCH_USERS = createPaginationActionSet('SEARCH_USERS');
export const ORGS_GET_MEMBERS = createPaginationActionSet('ORGS_GET_MEMBERS');
export const ORGS_GET_BY_ID = createActionSet('ORGS_GET_BY_ID');

export const GRAPHQL_GET_REPO = createActionSet('GRAPHQL_GET_REPO');

export const REPOS_GET_CONTRIBUTORS = createPaginationActionSet(
'REPOS_GET_CONTRIBUTORS'
);
export const REPOS_FORK = createActionSet('REPOS_FORK');
166 changes: 166 additions & 0 deletions src/api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { version } from 'package.json';
import { Platform } from 'react-native';

import Schemas from './schemas';
import { repoQuery } from './queries';

type SpecialParameters = {
forceRefresh?: boolean,
Expand All @@ -19,6 +20,12 @@ type FetchParameters = {
body?: Object,
};

type UpdateEntity = {
type: string,
id: string,
updater: Function,
};

export type CallParameters = {
endpoint: string,
schema: Schema,
Expand All @@ -27,6 +34,7 @@ export type CallParameters = {
normalizrKey?: string,
paginationArgs?: Array<string | number | boolean>,
entityId?: String | number,
updateEntity?: UpdateEntity,
};

export type CallType = CallParameters & {
Expand Down Expand Up @@ -98,6 +106,19 @@ export class Client {
return fetch(finalUrl, parameters);
};

put = ({ fetchParameters, ...config }: CallParameters): CallType => ({
type: 'put',
params: {},
...config,
fetchParameters: merge(
{
method: this.Method.PUT,
headers: { Accept: this.Accept.JSON, 'Content-Length': 0 },
},
fetchParameters
),
});

get = ({ fetchParameters, ...config }: CallParameters): CallType => ({
type: 'get',
params: {},
Expand All @@ -108,6 +129,49 @@ export class Client {
),
});

delete = ({ fetchParameters, ...config }: CallParameters): CallType => ({
type: 'delete',
params: {},
...config,
fetchParameters: merge(
{ method: this.Method.DELETE, headers: { Accept: this.Accept.JSON } },
fetchParameters
),
});

create = ({ fetchParameters, ...config }: CallParameters): CallType => ({
type: 'create',
params: {},
...config,
fetchParameters: merge(
{ method: this.Method.POST, headers: { Accept: this.Accept.JSON } },
fetchParameters
),
});

query = ({
fetchParameters,
query,
variables,
...config
}: CallParameters): CallType => ({
type: 'query',
endpoint: 'graphql',
params: {},
...config,
fetchParameters: merge(
{
method: this.Method.POST,
headers: { Accept: this.Accept.JSON },
body: {
query,
variables,
},
},
fetchParameters
),
});

list = ({ fetchParameters, ...config }: CallParameters): CallType => ({
type: 'list',
params: {},
Expand Down Expand Up @@ -173,6 +237,19 @@ export class Client {
.slice(1, -1);
};

graphql = {
getRepo: (repoId: string, params: SpecialParameters = {}) => {
const [owner, name] = repoId.split('/');

return this.query({
params,
query: repoQuery,
variables: { owner, name },
schema: Schemas.GQL_REPO,
});
},
};

/**
* The activity endpoint
*/
Expand Down Expand Up @@ -200,6 +277,80 @@ export class Client {
schema: Schemas.REPO_ARRAY,
paginationArgs: [userId],
}),

starRepo: (repoId: string, params: SpecialParameters = {}) =>
this.put({
endpoint: `user/starred/${repoId}`,
params,
schema: Schemas.GQL_REPO,
updateEntity: {
type: 'gqlRepos',
id: repoId,
updater: gqlRepo => ({
viewerHasStarred: true,
stargazers: {
...gqlRepo.stargazers,
totalCount: gqlRepo.stargazers.totalCount + 1,
},
}),
},
}),
unstarRepo: (repoId: string, params: SpecialParameters = {}) =>
this.delete({
endpoint: `user/starred/${repoId}`,
params,
schema: Schemas.GQL_REPO,
updateEntity: {
type: 'gqlRepos',
id: repoId,
updater: gqlRepo => ({
viewerHasStarred: false,
stargazers: {
...gqlRepo.stargazers,
totalCount: gqlRepo.stargazers.totalCount - 1,
},
}),
},
}),
watchRepo: (repoId: string, params: SpecialParameters = {}) =>
this.put({
endpoint: `repos/${repoId}/subscription`,
params,
fetchParameters: {
body: {
subscribed: true,
},
},
schema: Schemas.GQL_REPO,
updateEntity: {
type: 'gqlRepos',
id: repoId,
updater: gqlRepo => ({
viewerSubscription: 'SUBSCRIBED',
watchers: {
...gqlRepo.watchers,
totalCount: gqlRepo.watchers.totalCount + 1,
},
}),
},
}),
unwatchRepo: (repoId: string, params: SpecialParameters = {}) =>
this.delete({
endpoint: `repos/${repoId}/subscription`,
params,
schema: Schemas.GQL_REPO,
updateEntity: {
type: 'gqlRepos',
id: repoId,
updater: gqlRepo => ({
viewerSubscription: 'UNSUBSCRIBED',
watchers: {
...gqlRepo.watchers,
totalCount: gqlRepo.watchers.totalCount - 1,
},
}),
},
}),
};
search = {
repos: (q: string, params: SpecialParameters = {}) =>
Expand All @@ -220,6 +371,21 @@ export class Client {
normalizrKey: 'items',
}),
};
repos = {
getContributors: (repoId: string, params: SpecialParameters = {}) =>
this.list({
endpoint: `repos/${repoId}/contributors`,
params,
schema: Schemas.USER_ARRAY,
paginationArgs: [repoId],
}),
fork: (repoId: string, params: SpecialParameters = {}) =>
this.create({
endpoint: `repos/${repoId}/forks`,
params,
schema: Schemas.REPO,
}),
};
orgs = {
/**
* Get org by id
Expand Down
25 changes: 0 additions & 25 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,9 @@ export const fetchMarkRepoNotificationAsRead = (repoFullName, accessToken) =>
export const fetchMarkAllNotificationsAsRead = accessToken =>
v3.put('/notifications', accessToken);

export const fetchChangeStarStatusRepo = (owner, repo, starred, accessToken) =>
v3[starred ? 'delete' : 'put'](`/user/starred/${owner}/${repo}`, accessToken);

export const fetchForkRepo = (owner, repo, accessToken) =>
v3.post(`/repos/${owner}/${repo}/forks`, accessToken);

export const fetchStarCount = (owner, accessToken) =>
v3.count(`/users/${owner}/starred`, accessToken);

export const isWatchingRepo = (url, accessToken) => v3.head(url, accessToken);

export const watchRepo = (owner, repo, accessToken) =>
v3.put(`/repos/${owner}/${repo}/subscription`, accessToken, {
subscribed: true,
});

export const unWatchRepo = (owner, repo, accessToken) =>
v3.delete(`/repos/${owner}/${repo}/subscription`, accessToken);

export const fetchChangeFollowStatus = (user, isFollowing, accessToken) =>
v3[isFollowing ? 'delete' : 'put'](`/user/following/${user}`, accessToken);

Expand Down Expand Up @@ -383,15 +367,6 @@ export const fetchNotificationsCount = accessToken =>
export const fetchRepoNotificationsCount = (owner, repoName, accessToken) =>
v3.count(`/repos/${owner}/${repoName}/notifications?per_page=1`, accessToken);

export const fetchRepoTopics = async (owner, repoName, accessToken) => {
const response = await v3.call(
`/repos/${owner}/${repoName}/topics`,
v3.parameters(accessToken, METHOD.GET, ACCEPT.MERCY_PREVIEW)
);

return response.json();
};

export const fetchIssueEvents = (
owner: string,
repoName: string,
Expand Down
Loading