Skip to content

Commit

Permalink
[Add] subscribeToMore sample code #29
Browse files Browse the repository at this point in the history
why
์ƒˆ ํ”ผ๋“œ ์•Œ๋žŒ์„ ์œ„ํ•ด์„œ sample code ์ž‘์„ฑ

how
subscribeToMore ๋กœ ์ƒˆ ๋ฐ์ดํ„ฐ ๋ฐ›์•„์„œ data ์— ๋ฐ˜์˜๋˜๋Š”์ง€ ํ™•์ธ์ฐจ ์ ์šฉ

์ฃผ์˜
- subscribeToMore๋Š” ์•Œ๋žŒ์ด ์™”์„ ๋•Œ ์–ด๋–ค ์ž‘์—…์„ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
- ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ๋ฐ”๋กœ ์ฟผ๋ฆฌ์˜ ๋ฐ์ดํ„ฐ์™€ ๋จธ์ง€ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ- existing queries are automatically notified ๋Œ€์‹  ๋ฐ์ดํ„ฐ ํ˜•ํƒœ๊ฐ€ ๊ฐ™์•„์•ผ๋จ!!!
  • Loading branch information
WooYeonSeo committed Nov 28, 2019
1 parent 4065ddf commit bdab9b4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 31 deletions.
21 changes: 14 additions & 7 deletions client/src/composition/Feed/onMount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import React from 'react';

interface Props {
onEffect: () => void | (() => void);
data: { newUser: string };
}

export const OnMount = ({ onEffect }: Props) => {
useEffect(onEffect, []);
export const OnMount = ({ onEffect, data }: Props) => {
useEffect(() => {
return onEffect();
}, [onEffect]);

return (
<>
<div> aa</div>
</>
);
if (data) {
return <div>{data.newUser}</div>;
} else {
return (
<>
<div>WHAT?</div>
</>
);
}
};
38 changes: 17 additions & 21 deletions client/src/composition/Feed/subscriptionTest.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { useQuery, useMutation } from '@apollo/react-hooks';
import { useQuery, useSubscription } from '@apollo/react-hooks';
import gql from 'graphql-tag';
import { OnMount } from './onMount';

const FEEDS_SUBSCRIPTION = gql`
subscription {
newUser
Expand All @@ -10,18 +11,15 @@ const FEEDS_SUBSCRIPTION = gql`

const COMMENT_QUERY = gql`
query {
testPubsub
newUser
}
`;

const SusbscriptionTest = () => {
/* const { data, loading } = useSubscription<{ newUser: string }, {}>(
FEEDS_SUBSCRIPTION,
{}
); */
const { subscribeToMore, fetchMore, ...result } = useQuery(COMMENT_QUERY, {
const { subscribeToMore, data, ...result } = useQuery(COMMENT_QUERY, {
variables: {}
});

// const [testMutation] = useMutation(COMMENT_QUERY, {});

const subscribeToNewComments = () => {
Expand All @@ -31,26 +29,24 @@ const SusbscriptionTest = () => {
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) return prev;
const newFeedItem = subscriptionData.data;
// console.log('new data! -', newFeedItem);
return newFeedItem;
console.log('subscriptionData loaded -', { ...prev, newFeedItem });
return { ...prev, ...newFeedItem };
}
});
};
const onLoadMore = () =>
fetchMore({
variables: {},
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) return prev;

return fetchMoreResult;
}
});

// return <> lalala </>;
if (data) {
console.log('data load check??', data);
}
return (
<>
New comment: <OnMount onEffect={subscribeToNewComments} />
<div>FETCH</div>
New data: <OnMount data={data} onEffect={subscribeToNewComments} /> !!
<div
onClick={() => {
// onLoadMore();
}}>
FETCH MORE
</div>
</>
);
};
Expand Down
63 changes: 63 additions & 0 deletions client/src/composition/Feed/subscriptionTest2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import { useQuery, useSubscription } from '@apollo/react-hooks';
import gql from 'graphql-tag';
import { OnMount } from './onMount';

const FEEDS_SUBSCRIPTION = gql`
subscription {
newUser
}
`;

const COMMENT_QUERY = gql`
query {
testPubsub
}
`;

const SusbscriptionTest = () => {
const { data, loading } = useSubscription<{ newUser: string }, {}>(
FEEDS_SUBSCRIPTION,
{}
);
/* const { subscribeToMore, fetchMore, ...result } = useQuery(COMMENT_QUERY, {
variables: {}
});
// const [testMutation] = useMutation(COMMENT_QUERY, {});
const subscribeToNewComments = () => {
return subscribeToMore({
document: FEEDS_SUBSCRIPTION,
variables: {},
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) return prev;
const newFeedItem = subscriptionData.data;
// console.log('new data! -', newFeedItem);
return newFeedItem;
}
});
};
const onLoadMore = () =>
fetchMore({
variables: {},
updateQuery: (prev, { fetchMoreResult }) => {
if (!fetchMoreResult) return prev;
return fetchMoreResult;
}
}); */

// return <> lalala </>;
if (!loading) {
console.log(data);
return <> data </>;
}
return (
<>
New comment: {/* <OnMount onEffect={subscribeToNewComments} /> */}
<div>FETCH</div>
</>
);
};

export default SusbscriptionTest;
2 changes: 1 addition & 1 deletion server/src/api/feed/feed.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Query {
feeds(first: Int, cursor: String): [Feed]!
pageInfo: PageInfo
feedItems(first: Int, cursor: String): [IFeed]
testPubsub: String
newUser: String
}

type Mutation {
Expand Down
4 changes: 2 additions & 2 deletions server/src/api/feed/feed.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export default {
});
return ParseResultRecords(result.records);
},
testPubsub: (_, __, { pubsub }) => {
newUser: (_, __, { pubsub }) => {
/* const user = {
id: 1,
username: 'name'
}; */

pubsub.publish(NEW_USER, {
newUser: 'name'
newUser: 'name' + temp + ''
});
return 'value' + temp++;
}
Expand Down

0 comments on commit bdab9b4

Please sign in to comment.