[email protected]ã¸ç§»è¡ãã
ç¾å¨ã®ã¹ãã¼ã¿ã¹ã¯betaã§ãã®ã§ãæ¥ãå¿ è¦ã¯ãªãã§ãã
$ npm i react-apollo@beta
大ããªæ³¨ç®ç¹ã¨ãã¦ã¯ãhooksã®è¿½å ã¨@apollo/react-testing
ã¸ã®åé¢ã§ããããã
ãã®è¨äºã¯ãèªåãã¶ã¤ãã£ãé¨åã®ã¾ã¨ããªã®ã§æãã¯ããã¾ãã注æãã¦ãã ããã
Entry Point
å é¨çãªè©±ã§ããããã¹ã¦åå空éã«ããã±ã¼ã¸ã移ãããreact-apolloèªä½ã¯å¾æ¹äºæã®ããã®ãããã·ã¨ãªãã¾ãã
react-apolloã®entryã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
// index.js export { getApolloContext, resetApolloContext, ApolloProvider, ApolloConsumer } from '@apollo/react-common'; export { Query, Mutation, Subscription } from '@apollo/react-components'; export { graphql, withQuery, withMutation, withSubscription, withApollo } from '@apollo/react-hoc'; export { useQuery, useMutation, useSubscription, useApolloClient, getMarkupFromTree, getDataFromTree, renderToStringWithData } from '@apollo/react-hooks'; //# sourceMappingURL=index.js.map
Hooks
react-hooksã®ç»å ´ã«ä¼´ã£ã¦ãapolloãä»å¾ã¯hooksã主æµã«ãªã£ã¦ããã¾ãã
Queryã¨Mutationã¯ä»¥ä¸ã®ãããªæ¸ãæ¹ã«å¤æ´ã§ãã¾ãã
// ã¹ãã¼ã const GET_ORGS = gql` query { organizations { name uid } } `; const ADD_ORG = gql` mutation addOrganization($name: String!) { addOrganization(name: $name) { name uri uid } } `;
以åã®ã¯ã©ã¹
// ããã§dataã®åãä»ã¾ã§ã¯å ¥ããªãã¨æ¨è«ãã§ããªãã£ã class GetOrgsQuery extends Query<{ organizations: Organizations }> {} export class OrganizationsBox extends React.PureComponent<unknown, State> { state = { currentValue: '' }; onChange = (e: React.FormEvent<HTMLInputElement>) => { this.setState({ currentValue: e.currentTarget.value }); }; onSubmit = (addOrganization: MutationFunc) => { addOrganization({ variables: { name: this.state.currentValue } }); this.setState({ currentValue: '' }); }; render() { return ( <GetOrgsQuery query={GET_ORGS}> {({ loading, error, data }) => ( <Ul> {error || loading ? <p>{error ? `Error! ${error.message}` : 'loading...'}</p> : null} {data && data.organizations && data.organizations.map(({ name, uid }) => ( <Li key={uid}> <A to={`/orgs/${name}`}>{name}</A> </Li> ))} <Mutation mutation={ADD_ORG} refetchQueries={[{ query: GET_ORGS }]}> {(addOrganization, { error }) => ( <React.Fragment> {error && <p>{`Error! ${error.message}`}</p>} <InputBox> <input onChange={this.onChange} value={this.state.currentValue} /> <button onClick={() => this.onSubmit(addOrganization)}>Add</button> </InputBox> </React.Fragment> )} </Mutation> </Ul> )} </GetOrgsQuery> ); } }
Hooks
import { useQuery, useMutation } from 'react-apollo'; export const OrganizationsBox: React.FC = () => { const [currentValue, updateCurrentValue] = useState(''); const { loading, error, data } = useQuery<{ organizations: Organizations }>(GET_ORGS); const [addOrganization, { error, data }] = useMutation(ADD_ORG, { refetchQueries: [{ query: GET_ORGS }] }); const onChange = (e: React.FormEvent<HTMLInputElement>) => { updateCurrentValue(e.currentTarget.value); }; const onSubmit = (addOrganization: any) => { addOrganization({ variables: { name: currentValue } }); updateCurrentValue(''); }; if (error || loading) { return <p>{error ? `Error! ${error.message}` : 'loading...'}</p>; } return ( <Ul> {data && data.organizations.map(({ name, uid }) => ( <Li key={uid}> <A to={`/orgs/${name}`}>{name}</A> </Li> ))} <InputBox> <input onChange={onChange} value={currentValue} /> <button onClick={() => onSubmit(addOrganization)}>Add</button> </InputBox> </Ul> ); };
ä¸è¨ã®éããjsxå ã§é¢æ°ãæ¸ãå¿ è¦ããªããªããå¯èªæ§ãå¢ãã¾ããã
const { loading, error, data } = useQuery(GET_ORGS); const [addOrganization, { error, data }] = useMutation(ADD_ORG); const { loading, data } = useSubscription(LATEST_ORGS);
ã¨å®ç¾©ããã³ã¼ã«ããã°çµããã§ãã
注æç¹ã¨ãã¦ãMutationã ãã¦ã¼ã¶ã¼å®ç¾©ãå
¥ãããé
åã§ãã
react-testing
ãããã¹ããstorybookã§ä½¿ããã MockedProvider
ã¯react-apollo
ããã®ã¤ã³ãã¼ãã§ã¯ãªãã@apollo-react-testing
ã¨ããã©ã¤ãã©ãªããã®ã¤ã³ãã¼ãã«å¤æ´ããã¾ãã
- import { MockedProvider } from 'react-apollo/test-utils'; + import { MockedProvider } from '@apollo/react-testing';
MockLink
, MockSubscriptionLink
ãåæ§ã«ç§»åãã¾ããã
åå®ç¾©
ãªãã¼ã
MutationFn
ã MutationFunction
ã«ä¸èº«ã¯åæ§ã§ãªãã¼ã ããã¾ããã
åé¤
èªåãææ¡ãã¦ããéãã§ä»¥ä¸ã®åããªããªãã¾ããã
GraphqlQueryControls
ãMutationFunc
ãChildDataProps
ç¬èªã§å®ç¾©ããå ´åã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
import { OperationVariables, QueryControls, MutationFunction, DataProps } from 'react-apollo'; export interface GraphqlQueryControls<TGraphQLVariables = OperationVariables> extends QueryControls<any, TGraphQLVariables> {} export type MutationFunc<TData = any, TVariables = OperationVariables> = MutationFunction< TData, TVariables >; export type ChildDataProps< TProps = {}, TData = {}, TGraphQLVariables = OperationVariables > = TProps & DataProps<TData, TGraphQLVariables>;
ãã®ä»
walkTree
ã®ãµãã¼ããç¡ããªã- preactã®ãµãã¼ãããªããªã
compose
ããªããªã- ä¸èº«ãlodashã®
flowRight
ã¨åããªã®ã§åèªã§å®ç¾©ãã¦ãã¨ã®ãã¨
- ä¸èº«ãlodashã®
æããæã£ã¦ããä¸æºãã©ãã©ããªããªã£ã¦ãã¦ãã¦ãæé«ã«ãªã£ã¦ãã¦ããã®ã§ä»å¾ã楽ãã¿ã