Skip to content

Commit

Permalink
feat: adds separate components for query and subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshnirmalya committed Jul 20, 2020
1 parent 50c73dd commit 252675e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 55 deletions.
12 changes: 0 additions & 12 deletions packages/frontend/configs/graphql-query.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/frontend/configs/graphql-subscription.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/frontend/lib/graphql-subscription-client.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/frontend/lib/with-graphql-query.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createClient, Provider } from "urql";
import fetch from "isomorphic-unfetch";

const client = createClient({
url: process.env.API_URL || "http://localhost:8080/v1/graphql",
fetch,
fetchOptions: {
headers: { "X-Hasura-Admin-Secret": "secret" },
},
requestPolicy: "cache-and-network",
});

const WithGraphQLQuery = ({ children }: any) => (
<Provider value={client}>{children}</Provider>
);

export default WithGraphQLQuery;
32 changes: 32 additions & 0 deletions packages/frontend/lib/with-graphql-subscription.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Client, defaultExchanges, subscriptionExchange, Provider } from "urql";
import { SubscriptionClient } from "subscriptions-transport-ws";
import ws from "isomorphic-ws";

const subscriptionClient = new SubscriptionClient(
process.env.WS_URL || "ws://localhost:8080/v1/graphql",
{
reconnect: true,
connectionParams: {
headers: { "X-Hasura-Admin-Secret": "secret" },
},
},
ws
);

const client = new Client({
url: process.env.WS_URL || "ws://localhost:8080/v1/graphql",
exchanges: [
...defaultExchanges,
subscriptionExchange({
forwardSubscription(operation) {
return subscriptionClient.request(operation);
},
}),
],
});

const WithGraphQLSubscription = ({ children }: any) => (
<Provider value={client}>{children}</Provider>
);

export default WithGraphQLSubscription;
9 changes: 4 additions & 5 deletions packages/frontend/pages/query.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from "react";
import Head from "next/head";
import Page from "components/pages/query";
import { withUrqlClient } from "next-urql";
import { NextPage } from "next";
import Loader from "components/loader";
import AccessDeniedIndicator from "components/access-denied-indicator";
import { useSession } from "next-auth/client";
import graphqlQueryConfig from "configs/graphql-query";
import WithGraphQLQuery from "lib/with-graphql-query";

const QueryPage: NextPage = () => {
const [session, loading] = useSession();
Expand All @@ -20,13 +19,13 @@ const QueryPage: NextPage = () => {
}

return (
<>
<WithGraphQLQuery>
<Head>
<title>People Page</title>
</Head>
<Page />
</>
</WithGraphQLQuery>
);
};

export default withUrqlClient(graphqlQueryConfig)(QueryPage);
export default QueryPage;
9 changes: 4 additions & 5 deletions packages/frontend/pages/subscription.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from "react";
import Head from "next/head";
import Page from "components/pages/subscription";
import { withUrqlClient } from "next-urql";
import { NextPage } from "next";
import Loader from "components/loader";
import AccessDeniedIndicator from "components/access-denied-indicator";
import { useSession } from "next-auth/client";
import graphqlSubscriptionConfig from "configs/graphql-subscription";
import WithGraphQLSubscription from "lib/with-graphql-subscription";

const SubscriptionPage: NextPage = () => {
const [session, loading] = useSession();
Expand All @@ -20,13 +19,13 @@ const SubscriptionPage: NextPage = () => {
}

return (
<>
<WithGraphQLSubscription>
<Head>
<title>Users Page</title>
</Head>
<Page />
</>
</WithGraphQLSubscription>
);
};

export default withUrqlClient(graphqlSubscriptionConfig)(SubscriptionPage);
export default SubscriptionPage;

0 comments on commit 252675e

Please sign in to comment.