Skip to content

Commit

Permalink
feat: add data-schema client (#12552)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Wire <[email protected]>
  • Loading branch information
iartemiev and svidgen authored Nov 10, 2023
1 parent ff85fa5 commit 922eb86
Show file tree
Hide file tree
Showing 47 changed files with 7,662 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
// Optionally specify a single test file to run/debug:
"GraphQLAPI.test.ts",
"generateClient.test.ts",
"--runInBand",
"--testTimeout",
"600000", // 10 min timeout so jest doesn't error while we're stepping through code
Expand Down
7 changes: 7 additions & 0 deletions packages/adapter-nextjs/data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@aws-amplify/adapter-nextjs/data",
"main": "../dist/cjs/api/index.js",
"browser": "../dist/esm/api/index.mjs",
"module": "../dist/esm/api/index.mjs",
"typings": "../dist/esm/api/index.d.ts"
}
5 changes: 5 additions & 0 deletions packages/adapter-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"import": "./dist/esm/api/index.mjs",
"require": "./dist/cjs/api/index.js"
},
"./data": {
"types": "./dist/esm/api/index.d.ts",
"import": "./dist/esm/api/index.mjs",
"require": "./dist/cjs/api/index.js"
},
"./package.json": "./package.json"
},
"files": [
Expand Down
33 changes: 26 additions & 7 deletions packages/adapter-nextjs/src/api/generateServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
AmplifyServer,
} from '@aws-amplify/core/internals/adapter-core';
import {
V6Client,
V6ClientSSR,
V6ClientSSRRequest,
V6ClientSSRCookies,
GraphQLMethod,
GraphQLMethodSSR,
__amplify,
Expand All @@ -18,6 +18,20 @@ import {
import { NextServer } from '../types';
import { createServerRunnerForAPI } from './createServerRunnerForAPI';
import { getAmplifyConfig } from '../utils';
import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';

type CookiesClientParams = {
cookies: NextServer.ServerComponentContext['cookies'];
config: NextServer.CreateServerRunnerInput['config'];
authMode?: GraphQLAuthMode;
authToken?: string;
};

type ReqClientParams = {
config: NextServer.CreateServerRunnerInput['config'];
authMode?: GraphQLAuthMode;
authToken?: string;
};

/**
* Generates an API client that can be used inside a Next.js Server Component with Dynamic Rendering
Expand All @@ -33,8 +47,9 @@ export function generateServerClientUsingCookies<
>({
config,
cookies,
}: NextServer.ServerComponentContext &
NextServer.CreateServerRunnerInput): V6Client<T> {
authMode,
authToken,
}: CookiesClientParams): V6ClientSSRCookies<T> {
if (typeof cookies !== 'function') {
throw new AmplifyServerContextError({
message:
Expand All @@ -58,9 +73,11 @@ export function generateServerClientUsingCookies<
fn(getAmplifyServerContext(contextSpec).amplify),
});

return generateServerClient<T, V6Client<T>>({
return generateServerClient<T, V6ClientSSRCookies<T>>({
amplify: getAmplify,
config: resourcesConfig,
authMode,
authToken,
});
}

Expand All @@ -82,13 +99,15 @@ export function generateServerClientUsingCookies<
*/
export function generateServerClientUsingReqRes<
T extends Record<any, any> = never
>({ config }: NextServer.CreateServerRunnerInput): V6ClientSSR<T> {
>({ config, authMode, authToken }: ReqClientParams): V6ClientSSRRequest<T> {
const amplifyConfig = getAmplifyConfig(config);
// passing `null` instance because each (future model) method must retrieve a valid instance
// from server context
const client = generateServerClient<T, V6ClientSSR<T>>({
const client = generateServerClient<T, V6ClientSSRRequest<T>>({
amplify: null,
config: amplifyConfig,
authMode,
authToken,
});

// TODO: improve this and the next type
Expand Down
Loading

0 comments on commit 922eb86

Please sign in to comment.