Skip to content

feat(api-graphql): add libraryOptions to generateServerClientUsingCookies #12979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/adapter-nextjs/src/api/createServerRunnerForAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { NextServer } from '../types';

export const createServerRunnerForAPI = ({
config,
libraryOptions,
}: NextServer.CreateServerRunnerInput): NextServer.CreateServerRunnerOutput & {
resourcesConfig: ResourcesConfig;
} => {
Expand All @@ -15,6 +16,7 @@ export const createServerRunnerForAPI = ({
return {
runWithAmplifyServerContext: createRunWithAmplifyServerContext({
config: amplifyConfig,
libraryOptions,
}),
resourcesConfig: amplifyConfig,
};
Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-nextjs/src/api/generateServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
type CookiesClientParams = {
cookies: NextServer.ServerComponentContext['cookies'];
config: NextServer.CreateServerRunnerInput['config'];
libraryOptions?: NextServer.CreateServerRunnerInput['libraryOptions'];
authMode?: GraphQLAuthMode;
authToken?: string;
};
Expand All @@ -44,6 +45,7 @@ export function generateServerClientUsingCookies<
>({
config,
cookies,
libraryOptions,
authMode,
authToken,
}: CookiesClientParams): V6ClientSSRCookies<T> {
Expand All @@ -58,7 +60,7 @@ export function generateServerClientUsingCookies<
}

const { runWithAmplifyServerContext, resourcesConfig } =
createServerRunnerForAPI({ config });
createServerRunnerForAPI({ config, libraryOptions });

// This function reference gets passed down to InternalGraphQLAPI.ts.graphql
// where this._graphql is passed in as the `fn` argument
Expand Down
5 changes: 4 additions & 1 deletion packages/adapter-nextjs/src/createServerRunner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { ResourcesConfig } from 'aws-amplify';
import { LibraryOptions, ResourcesConfig } from 'aws-amplify';

import { createRunWithAmplifyServerContext, getAmplifyConfig } from './utils';
import { NextServer } from './types';
Expand All @@ -16,6 +16,7 @@ import { NextServer } from './types';
* @param input The input used to create the `runWithAmplifyServerContext` function.
* @param input.config The {@link ResourcesConfig} imported from the `amplifyconfiguration.json` file or manually
* created.
* @param input.libraryOptions The {@link LibraryOptions} additional options for the library.
* @returns An object that contains the `runWithAmplifyServerContext` function.
*
* @example
Expand All @@ -27,12 +28,14 @@ import { NextServer } from './types';
*/
export const createServerRunner: NextServer.CreateServerRunner = ({
config,
libraryOptions,
}) => {
const amplifyConfig = getAmplifyConfig(config);

return {
runWithAmplifyServerContext: createRunWithAmplifyServerContext({
config: amplifyConfig,
libraryOptions,
}),
};
};
3 changes: 2 additions & 1 deletion packages/adapter-nextjs/src/types/NextServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NextRequest, NextResponse } from 'next/server.js';
import { cookies } from 'next/headers.js';
import { LegacyConfig } from 'aws-amplify/adapter-core';
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
import { ResourcesConfig } from '@aws-amplify/core';
import { LibraryOptions, ResourcesConfig } from '@aws-amplify/core';

export declare namespace NextServer {
/**
Expand Down Expand Up @@ -75,6 +75,7 @@ export declare namespace NextServer {

export interface CreateServerRunnerInput {
config: ResourcesConfig | LegacyConfig;
libraryOptions: LibraryOptions;
}

export interface CreateServerRunnerOutput {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { ResourcesConfig, sharedInMemoryStorage } from '@aws-amplify/core';
import { LibraryOptions, ResourcesConfig, sharedInMemoryStorage } from '@aws-amplify/core';
import {
createAWSCredentialsAndIdentityIdProvider,
createKeyValueStorageFromCookieStorageAdapter,
Expand All @@ -15,8 +15,10 @@ import { createCookieStorageAdapterFromNextServerContext } from './createCookieS

export const createRunWithAmplifyServerContext = ({
config: resourcesConfig,
libraryOptions,
}: {
config: ResourcesConfig;
libraryOptions?: LibraryOptions
}) => {
const runWithAmplifyServerContext: NextServer.RunOperationWithContext =
async ({ nextServerContext, operation }) => {
Expand Down Expand Up @@ -48,14 +50,15 @@ export const createRunWithAmplifyServerContext = ({
resourcesConfig,
{
Auth: { credentialsProvider, tokenProvider },
...libraryOptions,
},
operation,
);
}

// Otherwise it may be the case that auth is not used, e.g. API key.
// Omitting the `Auth` in the second parameter.
return runWithAmplifyServerContextCore(resourcesConfig, {}, operation);
return runWithAmplifyServerContextCore(resourcesConfig, libraryOptions, operation);
};

return runWithAmplifyServerContext;
Expand Down