Skip to content

Commit

Permalink
Improve app context in GraphQL template
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade committed Jun 11, 2024
1 parent a70f378 commit f5981c1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-guests-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@quilted/create': patch
---

Improve app context in GraphQL template
5 changes: 4 additions & 1 deletion packages/create/templates/app-graphql/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ function AppContext({children, context}: RenderableProps<AppProps>) {

return (
<AppContextReact.Provider value={context}>
<GraphQLContext fetch={context.fetchGraphQL} cache={context.graphQLCache}>
<GraphQLContext
fetch={context.graphql.fetch}
cache={context.graphql.cache}
>
<Localization locale={locale}>
<Routing>{children}</Routing>
</Localization>
Expand Down
10 changes: 6 additions & 4 deletions packages/create/templates/app-graphql/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import {App} from './App.tsx';
const element = document.querySelector('#app')!;
const browser = new Browser();

const fetchGraphQL = createGraphQLFetch({url: '/api/graphql'});
const graphQLCache = new GraphQLCache({fetch: fetchGraphQL});
const graphQLFetch = createGraphQLFetch({url: '/api/graphql'});
const graphQLCache = new GraphQLCache({fetch: graphQLFetch});

const context = {
fetchGraphQL,
graphQLCache,
graphql: {
fetch: graphQLFetch,
cache: graphQLCache,
},
} satisfies AppContext;

// Makes key parts of the app available in the browser console
Expand Down
23 changes: 12 additions & 11 deletions packages/create/templates/app-graphql/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import '@quilted/quilt/globals';
import {RequestRouter, JSONResponse} from '@quilted/quilt/request-router';
import {BrowserAssets} from 'quilt:module/assets';

import type {AppContext} from '~/shared/context.ts';

const router = new RequestRouter();
const assets = new BrowserAssets();

Expand All @@ -29,18 +31,17 @@ router.get(async (request) => {
import('@quilted/quilt/server'),
]);

const response = await renderToResponse(
<App
context={{
fetchGraphQL: performGraphQLOperation,
graphQLCache: new GraphQLCache(),
}}
/>,
{
request,
assets,
const context = {
graphql: {
fetch: performGraphQLOperation,
cache: new GraphQLCache(),
},
);
} satisfies AppContext;

const response = await renderToResponse(<App context={context} />, {
request,
assets,
});

return response;
});
Expand Down
6 changes: 4 additions & 2 deletions packages/create/templates/app-graphql/shared/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import type {GraphQLFetch, GraphQLCache} from '@quilted/quilt/graphql';

declare module '~/shared/context.ts' {
interface AppContext {
readonly fetchGraphQL: GraphQLFetch;
readonly graphQLCache: GraphQLCache;
readonly graphql: {
readonly fetch: GraphQLFetch;
readonly cache: GraphQLCache;
};
}
}

Expand Down

0 comments on commit f5981c1

Please sign in to comment.