Skip to content

Commit a29b61e

Browse files
committed
Add React provider for Apollo, so context hooks work. Add simple example component.
1 parent e95910b commit a29b61e

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

reactium_modules/@reactium/graphql/reactium-hooks-graphql-client.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApolloClient, InMemoryCache } from '@apollo/client';
1+
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';
22
import Reactium, { Hook, Enums } from 'reactium-core/sdk';
33

44
Hook.register(
@@ -43,3 +43,10 @@ Hook.register(
4343
Enums.highest,
4444
'REACTIUM-CORE-SDK-GRAPHQL',
4545
);
46+
47+
Hook.register('app-context-provider', async () => {
48+
Reactium.AppContext.register('ApolloProvider', {
49+
provider: ApolloProvider,
50+
client: Reactium.GraphQL,
51+
});
52+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { gql, useQuery } from '@apollo/client';
2+
import React from 'react';
3+
4+
const GET_USERS = gql`
5+
query GetUsers {
6+
users {
7+
id
8+
name
9+
email
10+
}
11+
}
12+
`;
13+
14+
/**
15+
* -----------------------------------------------------------------------------
16+
* Component: ApolloTest
17+
* -----------------------------------------------------------------------------
18+
*/
19+
export const ApolloTest = () => {
20+
const { loading, error, data } = useQuery(GET_USERS);
21+
if (loading) return <div>Loading...</div>;
22+
else if (error) return <div>Error :(</div>;
23+
const { users } = data;
24+
25+
return (
26+
<ul>
27+
{users.map(({ id, name, email }) => (
28+
<li key={id}>
29+
{name}: {email}
30+
</li>
31+
))}
32+
</ul>
33+
);
34+
};
35+
36+
export default ApolloTest;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* -----------------------------------------------------------------------------
3+
* DDD Domain ApolloTest - Change name to place domain artifacts in this directory
4+
* in a different domain.
5+
* -----------------------------------------------------------------------------
6+
*/
7+
module.exports = {
8+
name: 'ApolloTest',
9+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* -----------------------------------------------------------------------------
3+
* Reactium Plugin ApolloTest
4+
* -----------------------------------------------------------------------------
5+
*/
6+
(async () => {
7+
const { Hook, Enums, Component } = await import('@atomic-reactor/reactium-core/sdk');
8+
9+
Hook.register('plugin-init', async () => {
10+
const { ApolloTest } = await import('./ApolloTest');
11+
Component.register('ApolloTest', ApolloTest);
12+
}, Enums.priority.normal, 'plugin-init-ApolloTest');
13+
})();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ApolloTest as component } from './ApolloTest';
2+
3+
export default [
4+
{
5+
id: 'route-ApolloTest-1',
6+
exact: true,
7+
component,
8+
path: ['/apollo-test'],
9+
},
10+
];

0 commit comments

Comments
 (0)