Skip to content

Commit

Permalink
docs: data fetching code examples
Browse files Browse the repository at this point in the history
docs: data fetching code examples #2

docs: data fetching code examples #3
  • Loading branch information
kark committed Jun 9, 2022
1 parent f85880b commit d2989cc
Show file tree
Hide file tree
Showing 17 changed files with 525 additions and 255 deletions.
5 changes: 2 additions & 3 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const micromatch = require('micromatch');

module.exports = {
'*.md': ['prettier --write --parser markdown'],
'*.yaml': ['prettier --write --parser yaml'],
Expand All @@ -21,7 +19,7 @@ module.exports = {
// For that reason, we move the `--onlyChanged` flag next to it.
'yarn lint:js --reporters=jest-silent-reporter --onlyChanged',
],
'!(cypress)/**/*.{ts,tsx}': [
'!(cypress|website)/**/*.{ts,tsx}': [
'prettier --write',
// NOTE: apparently if you pass some argument that is not a flag AFTER the `reporters`
// flag, jest does not seem correctly parse the arguments.
Expand All @@ -47,6 +45,7 @@ module.exports = {
'yarn lint:js --reporters=jest-silent-reporter --onlyChanged',
() => 'yarn typecheck:cypress',
],
'website/**/*.{ts,tsx}': ['prettier --write'],
'*.css': [
'prettier --write --parser css',
// NOTE: apparently if you pass some argument that is not a flag AFTER the `reporters`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addon-components.js
15 changes: 13 additions & 2 deletions website/src/code-examples/data-fetching/channels.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { useQuery } from '@apollo/client/react';
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants';
import type {
TChannelQueryResult,
TFetchChannelsQueryVariables,
} from '../../../types/generated/ctp';

const Channels = (props) => {
const { data } = useQuery(FetchChannelsQuery, {
type TChannelsProps = {
// component props types
};

const Channels = (props: TChannelsProps) => {
const { data } = useQuery<
{ channels: TChannelQueryResult },
TFetchChannelsQueryVariables
>(FetchChannelsQuery, {
context: {
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM,
},
Expand Down
17 changes: 17 additions & 0 deletions website/src/code-examples/data-fetching/connector-hook-usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ContentNotification } from '@commercetools-uikit/notifications';
import Text from '@commercetools-uikit/text';
import { useChannelsFetcher } from '../../hooks/use-channels-connector';

const Channels = (props) => {
const { channels, error } = useChannelsFetcher();

if (error) {
return (
<ContentNotification type="error">
<Text.Body>{getErrorMessage(error)}</Text.Body>
</ContentNotification>
);
}

return <div>{/* Do something with `channels` */}</div>;
};
21 changes: 21 additions & 0 deletions website/src/code-examples/data-fetching/connector-hook-usage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ContentNotification } from '@commercetools-uikit/notifications';
import Text from '@commercetools-uikit/text';
import { useChannelsFetcher } from '../../hooks/use-channels-connector';

type TChannelsProps = {
// component props types
};

const Channels = (props: TChannelsProps) => {
const { channels, error } = useChannelsFetcher();

if (error) {
return (
<ContentNotification type="error">
<Text.Body>{getErrorMessage(error)}</Text.Body>
</ContentNotification>
);
}

return <div>{/* Do something with `channels` */}</div>;
};
16 changes: 16 additions & 0 deletions website/src/code-examples/data-fetching/connector-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useQuery } from '@apollo/client/react';
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants';

export const useChannelsFetcher = () => {
const { data, error, loading } = useQuery(FetchChannelsQuery, {
context: {
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM,
},
});

return {
channels: data?.channels,
error,
loading,
};
};
23 changes: 23 additions & 0 deletions website/src/code-examples/data-fetching/connector-hook.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useQuery } from '@apollo/client/react';
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants';
import type {
TChannelQueryResult,
TFetchChannelsQueryVariables,
} from '../../../types/generated/ctp';

export const useChannelsFetcher = () => {
const { data, error, loading } = useQuery<
{ channels: TChannelQueryResult },
TFetchChannelsQueryVariables
>(FetchChannelsQuery, {
context: {
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM,
},
});

return {
channels: data?.channels,
error,
loading,
};
};
22 changes: 22 additions & 0 deletions website/src/code-examples/data-fetching/error-handling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useQuery } from '@apollo/client/react';
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants';
import { ContentNotification } from '@commercetools-uikit/notifications';
import Text from '@commercetools-uikit/text';

const Channels = (props) => {
const { data, error } = useQuery(FetchChannelsQuery, {
context: {
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM,
},
});

if (error) {
return (
<ContentNotification type="error">
<Text.Body>{getErrorMessage(error)}</Text.Body>
</ContentNotification>
);
}

return <div>{/* Do something with `data` */}</div>;
};
33 changes: 33 additions & 0 deletions website/src/code-examples/data-fetching/error-handling.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useQuery } from '@apollo/client/react';
import { GRAPHQL_TARGETS } from '@commercetools-frontend/constants';
import { ContentNotification } from '@commercetools-uikit/notifications';
import Text from '@commercetools-uikit/text';
import type {
TChannelQueryResult,
TFetchChannelsQueryVariables,
} from '../../../types/generated/ctp';

type TChannelsProps = {
// component props types
};

const Channels = (props: TChannelsProps) => {
const { data, error } = useQuery<
{ channels: TChannelQueryResult },
TFetchChannelsQueryVariables
>(FetchChannelsQuery, {
context: {
target: GRAPHQL_TARGETS.COMMERCETOOLS_PLATFORM,
},
});

if (error) {
return (
<ContentNotification type="error">
<Text.Body>{getErrorMessage(error)}</Text.Body>
</ContentNotification>
);
}

return <div>{/* Do something with `data` */}</div>;
};
2 changes: 2 additions & 0 deletions website/src/code-examples/data-fetching/error-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const getErrorMessage = (error) =>
error.graphQLErrors?.map((e) => e.message).join('\n') || error.message;
4 changes: 4 additions & 0 deletions website/src/code-examples/data-fetching/error-message.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ApolloError } from '@apollo/client';

const getErrorMessage = (error: ApolloError) =>
error.graphQLErrors?.map((e) => e.message).join('\n') || error.message;
18 changes: 18 additions & 0 deletions website/src/code-examples/data-fetching/external-graphql-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
createApolloContextForProxyForwardTo,
useMcQuery,
} from '@commercetools-frontend/application-shell';
import Text from '@commercetools-uikit/text';
import HelloWorldQuery from './hello-world.graphql';

const HelloWorld = () => {
const { loading, data, error } = useMcQuery(HelloWorldQuery, {
context: createApolloContextForProxyForwardTo({
uri: 'https://my-custom-app.com/graphql',
}),
});

if (loading) return 'Loading...';
if (error) return `Error! ${error.message}`;
return <Text.Headline as="h1">{data.title}</Text.Headline>;
};
21 changes: 21 additions & 0 deletions website/src/code-examples/data-fetching/external-graphql-api.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
createApolloContextForProxyForwardTo,
useMcQuery,
} from '@commercetools-frontend/application-shell';
import Text from '@commercetools-uikit/text';
import HelloWorldQuery from './hello-world.graphql';

const HelloWorld = () => {
const { loading, data, error } = useMcQuery<
THelloWorldQueryData,
THelloWorldQueryVariables
>(HelloWorldQuery, {
context: createApolloContextForProxyForwardTo({
uri: 'https://my-custom-app.com/graphql',
}),
});

if (loading) return 'Loading...';
if (error) return `Error! ${error.message}`;
return <Text.Headline as="h1">{data.title}</Text.Headline>;
};
28 changes: 28 additions & 0 deletions website/src/code-examples/data-fetching/rest-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useEffect } from 'react';
import { useAsyncDispatch, actions } from '@commercetools-frontend/sdk';
import { MC_API_PROXY_TARGETS } from '@commercetools-frontend/constants';

const Channels = (props) => {
const dispatch = useAsyncDispatch();
useEffect(() => {
async function execute() {
try {
const result = await dispatch(
actions.get({
mcApiProxyTarget: MC_API_PROXY_TARGETS.COMMERCETOOLS_PLATFORM,
service: 'channels',
options: {
// query options
},
})
);
// Update state with `result`
} catch (error) {
// Update state with `error`
}
}
execute();
}, [dispatch]);

return <div>{/* Do something with the state */}</div>;
};
32 changes: 32 additions & 0 deletions website/src/code-examples/data-fetching/rest-api.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect } from 'react';
import { useAsyncDispatch, actions } from '@commercetools-frontend/sdk';
import { MC_API_PROXY_TARGETS } from '@commercetools-frontend/constants';

type TChannelsProps = {
// component props types
};

const Channels = (props: TChannelsProps) => {
const dispatch = useAsyncDispatch();
useEffect(() => {
async function execute() {
try {
const result = await dispatch(
actions.get({
mcApiProxyTarget: MC_API_PROXY_TARGETS.COMMERCETOOLS_PLATFORM,
service: 'channels',
options: {
// query options
},
})
);
// Update state with `result`
} catch (error) {
// Update state with `error`
}
}
execute();
}, [dispatch]);

return <div>{/* Do something with the state */}</div>;
};
Loading

0 comments on commit d2989cc

Please sign in to comment.