Skip to content

Commit

Permalink
gql sample
Browse files Browse the repository at this point in the history
  • Loading branch information
revmischa committed Jul 2, 2024
1 parent 274af59 commit fcf29ff
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 41 deletions.
42 changes: 21 additions & 21 deletions stacks/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import * as sst from 'sst/constructs';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { AppSyncApi } from './appSyncApi';
import { Auth } from './auth';
import { BastionHost } from './bastionHost';
import { Database, GrantDBAccess } from './database';
import { DatabaseMigrations } from './databaseMigrations';
import { Dns } from './dns';
import { Layers } from './layers';
import { Network } from './network';
import { RestApi } from './restApi';
import { Web } from './web';
import { Aspects } from 'aws-cdk-lib';
import { Secrets } from './secrets';
import * as sst from 'sst/constructs'
import { Runtime } from 'aws-cdk-lib/aws-lambda'
import { AppSyncApi } from './appSyncApi'
import { Auth } from './auth'
import { BastionHost } from './bastionHost'
import { Database, GrantDBAccess } from './database'
import { DatabaseMigrations } from './databaseMigrations'
import { Dns } from './dns'
import { Layers } from './layers'
import { Network } from './network'
import { RestApi } from './restApi'
import { Web } from './web'
import { Aspects } from 'aws-cdk-lib'
import { Secrets } from './secrets'

// deal with dynamic imports of node built-ins (e.g. "crypto")
// from https://github.com/evanw/esbuild/pull/2067#issuecomment-1073039746
// and hardcode __dirname for https://github.com/prisma/prisma/issues/14484
export const ESM_REQUIRE_SHIM = `const require = (await import("node:module")).createRequire(import.meta.url);const __filename = (await import("node:url")).fileURLToPath(import.meta.url);globalThis.__dirname='/var/task';`;
export const ESM_REQUIRE_SHIM = `const require = (await import("node:module")).createRequire(import.meta.url);const __filename = (await import("node:url")).fileURLToPath(import.meta.url);globalThis.__dirname='/var/task';`

export const RUNTIME = Runtime.NODEJS_20_X;
export const RUNTIME = Runtime.NODEJS_20_X

export default function main(app: sst.App) {
app.setDefaultFunctionProps({
Expand All @@ -40,7 +40,7 @@ export default function main(app: sst.App) {
},

// N.B. bundle settings are defined in Layers
});
})

app
.stack(Network)
Expand All @@ -49,13 +49,13 @@ export default function main(app: sst.App) {
.stack(Layers)
.stack(Database)
.stack(BastionHost)
// .stack(DatabaseMigrations)
.stack(DatabaseMigrations)
.stack(Auth)
.stack(RestApi)
.stack(AppSyncApi)
.stack(Web);
.stack(Web)

// DB access
const { db, dbAccessSecurityGroup } = sst.use(Database);
if (db && dbAccessSecurityGroup) Aspects.of(app).add(new GrantDBAccess(db, dbAccessSecurityGroup));
const { db, dbAccessSecurityGroup } = sst.use(Database)
if (db && dbAccessSecurityGroup) Aspects.of(app).add(new GrantDBAccess(db, dbAccessSecurityGroup))
}
13 changes: 13 additions & 0 deletions web/app/sample/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use client'

import { TestGql } from '../ui/TestGql'

// graphql demo

export default () => {
return (
<div>
<TestGql />
</div>
)
}
33 changes: 19 additions & 14 deletions web/app/ui/TestGql.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
'use client';
'use client'

import { useMutation } from '@apollo/client';
import { graphql } from '@common/generated/graphql';
import { useMutation } from '@apollo/client'
import { graphql } from '@common/generated/graphql'

const greetMutation = graphql(`mutation Greet($name: String!) {\n greet(name: $name) {\n greeting\n }\n}`);
const greetMutation = graphql('mutation Greet($name: String!) {\n greet(name: $name) {\n greeting\n }\n}')

export const TestGql = () => {
const [greet] = useMutation(greetMutation);
const [greet, { data: greetResult, loading }] = useMutation(greetMutation)

return (
<button
onClick={() => {
greet({ variables: { name: 'world' } });
}}
>
greet
</button>
);
};
<div className="flex flex-col items-center justify-center h-screen">
<button
onClick={() => {
greet({ variables: { name: 'world' } })
}}
>
greet
</button>

{loading && <progress className="m-4 w-48" />}
{greetResult && <div>{greetResult.greet?.greeting}</div>}
</div>
)
}
13 changes: 7 additions & 6 deletions web/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import NextAuth from 'next-auth';
import { authConfig } from './auth.config';
import NextAuth from 'next-auth'
import { authConfig } from './auth.config'

export default NextAuth(authConfig).auth;
export default NextAuth(authConfig).auth

export const config = {
// https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
};
// require login for these paths
// https://nextjs.org/docs/app/building-your-application/routing/middleware
matcher: ['/sample/:path'],
}

0 comments on commit fcf29ff

Please sign in to comment.