awslabs / aws-mobile-appsync-sdk-js

JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Apache License 2.0
921 stars 266 forks source link

Type 'CredentialsGetter | undefined' is not assignable to type 'CredentialsGetter' with createAppSyncLink #742

Open OperationalFallacy opened 1 year ago

OperationalFallacy commented 1 year ago

Do you want to request a feature or report a bug?

I am unable to use the complexObjectsCredentials configuration option in the AWSAppSyncClient without encountering a TypeScript error.

What is the current behavior?

When attempting to use the complexObjectsCredentials configuration option in the AWSAppSyncClient, the following TypeScript error is generated:

Argument of type '{ resultsFetcherLink: ApolloLink; url: string; region: string; auth: AuthOptions; conflictResolver?: ConflictResolver | undefined; complexObjectsCredentials?: CredentialsGetter | undefined; cacheOptions?: ApolloReducerConfig | undefined; disableOffline?: boolean | undefined; offlineConfig?: OfflineConfig | undefined...' is not assignable to parameter of type '{ url: string; region: string; auth: AuthOptions; complexObjectsCredentials: CredentialsGetter; resultsFetcherLink?: ApolloLink | undefined; conflictResolver?: ConflictResolver | undefined; }'.
  Types of property 'complexObjectsCredentials' are incompatible.
    Type 'CredentialsGetter | undefined' is not assignable to type 'CredentialsGetter'.
      Type 'undefined' is not assignable to type 'CredentialsGetter'.ts(2345)

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.

The issue occurs when attempting to use the AWSAppSyncClient with the complexObjectsCredentials configuration option in a TypeScript project intended for use in an AWS Lambda function. The following code snippet demonstrates the setup:

The IAM temporary credentials generated by external process.

const credsReady = creds as NoUndefinedField<AwsCredentialIdentity>;
// Initialize the STS client
const sts = new STSClient({
  credentials: {
    accessKeyId: credsReady.accessKeyId,
    secretAccessKey: credsReady.secretAccessKey,
    sessionToken: credsReady.sessionToken,
  },
  region,
});

// Configure the AppSync client
const config: AWSAppSyncClientOptions = {
  url: apiUrl,
  region: region,
  auth: {
    type: AUTH_TYPE.AWS_IAM,
    credentials: async () => sts.config.credentials,
  },
  complexObjectsCredentials: () => Auth.currentCredentials(),
  disableOffline: true,
};

const client1 = new AWSAppSyncClient(config, {
  link: createAppSyncLink({
    ...config,
    resultsFetcherLink: ApolloLink.from([
      setContext((request, previousContext) => ({
        headers: {
          ...previousContext.headers,
          token: "1234"
        }
      })),
      createHttpLink({
        uri: config.url
      })
    ])
  })
});

What is the expected behavior?

The complexObjectsCredentials configuration option should be properly assigned

Which versions and which environment (browser, react-native, nodejs) / OS are affected by this issue? Did this work in previous versions?

Environment: Node.js (a TypeScript project intended to run on a host, no browser interaction)