Skip to content
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

Redis APL provided by app-sdk #286

Closed
djkato opened this issue Sep 29, 2023 · 10 comments
Closed

Redis APL provided by app-sdk #286

djkato opened this issue Sep 29, 2023 · 10 comments
Assignees

Comments

@djkato
Copy link

djkato commented Sep 29, 2023

What I'm trying to achieve

Use an APL that I don't have to pay another party for . For simple KV storage it should be enough to just use the Redis db that is inlcuded in saleor-platform.

Describe a proposed solution

Add Redis APL as another possible import, and also add an .env switch for it inside apps.

Screenshots or mockups

As per https://docs.saleor.io/docs/3.x/developer/extending/apps/developing-apps/app-sdk/apl#example-implementation someone already wrote how to make it, so just use that in app-sdk :)
Code snippet from the docs:

import { createClient } from "redis";
import { APL, AuthData } from "@saleor/app-sdk/apl";

const client = createClient();
await client.connect();

/**
 * The APL uses API URL as a key to store and retrieve AuthData.
 * If you intend to use the same Redis instance for multiple Apps,
 * add prefix to the keys to avoid overwriting the data by different apps.
 * Keys will be formatted as below:
 * - `APPID1:http://staging.saleor.io/graphql`
 * - `APPID1:http://demo.saleor.io/graphql`
 * - `APPID2:http://demo.saleor.io/graphql`
 **/
const prepareAuthDataKey = (apiUrl: string) => `${APP_ID}:${apiUrl}`;

const redisAPL: APL = {
  get: async (saleorApiUrl: string) => {
    const response = await client.get(prepareAuthDataKey(saleorApiUrl));
    if (response) {
      return JSON.parse(response);
    }
    return;
  },
  set: async (authData: AuthData) => {
    await client.set(
      prepareAuthDataKey(authData.saleorApiUrl),
      JSON.stringify(authData)
    );
  },
  delete: async (saleorApiUrl: string) => {
    await client.del(prepareAuthDataKey(saleorApiUrl));
  },
  getAll: async () => {
    throw new Exception("Not implemented.");
  },
};
@lkostrowski lkostrowski linked a pull request Oct 2, 2023 that will close this issue
@JannikZed
Copy link
Contributor

@lkostrowski @djkato I create a new PR that is tested: #374
@andrzejewsky
please take a look guys

@JannikZed
Copy link
Contributor

@krzysztofzuraw @lkostrowski when will my recently merged PR #374 be released? :) That will close this old issue and we are patiently waiting for it.

@lkostrowski
Copy link
Member

@JannikZed we did some testing and looks like getAll method seems to had some issue, that's why we haven't released it yet

you can use the candidate version @saleor/app-sdk@0.0.0-pr-20250120101758

@JannikZed
Copy link
Contributor

Oh alright ! Should I try and verify that from my end or are you guys already on it ?

@lkostrowski
Copy link
Member

@JannikZed you would definitely make it faster :)

the getAll method should return a list of all previously saved items

@JannikZed
Copy link
Contributor

@lkostrowski could you tell me under which test you faced the error? I tried to rework the integration tests more: #384
adding more cases for hgetall etc..
but I. can't find any issues. Seems to work completely the same than the vercel kv APL.
When is the getAll used anyways? A regular app would normally not need that, right? Just want to create a test environment on my own to try that out.

@krzysztofzuraw
Copy link
Member

@JannikZed getAll can be used e.g for migration of webhooks (to get all app installations)

To test it - you can use my repo:

  1. Run Redis locally on redis://localhost:6378
  2. Install app from repo provided above
  3. Run pnpm run test-redis

@JannikZed
Copy link
Contributor

@krzysztofzuraw getting this result:

> saleor-app-template@1.0.0 test-redis /Users/jannikzinkl/Documents/git/redis-apl-test
> tsx test-redis-apl.ts

Using Redis APL
[
  {
    appId: 'foobar',
    saleorApiUrl: 'https://delete-test.saleor.cloud/graphql/',
    token: 'token',
    domain: 'domain',
    jwks: '{}'
  },
  {
    token: 'other-token',
    saleorApiUrl: 'https://other.saleor.cloud/graphql/',
    appId: 'other-app-id'
  }
]

I had to change the redis port here:

console.log("Using Redis APL");
    const client = createClient({
      url: "redis://localhost:6379",
      // Add any additional Redis configuration options
    });

from 6378 in your repo to 6379.

I looks all good to me?

@krzysztofzuraw
Copy link
Member

krzysztofzuraw commented Jan 23, 2025

@JannikZed I tested it again and it works 👍🏻 it seems like my error was not loading .env file that pointed to APL

@krzysztofzuraw
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants