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

[Question] Integration with websocket (partykit) #1302

Open
emroot opened this issue Sep 10, 2024 · 1 comment
Open

[Question] Integration with websocket (partykit) #1302

emroot opened this issue Sep 10, 2024 · 1 comment

Comments

@emroot
Copy link

emroot commented Sep 10, 2024

I'm adding partykit (websocket) to my project, and when a mutation happens, I'm sending a message to my ws server to broadcast my update to my clients (for other users to receive the update).
I'm using nextjs so can't really use the smart subscriptions.
I was wondering if there's some utility function to convert my data (coming from stripe api) onto the my pothos object implementation so i matches my graphql format.
This may involve writing a plugin maybe but curious if there's some function that will do that already.

Here's a snippet of what I'm trying to do:

// Schema definition
export const StripeAccount = builder
  .objectRef<Stripe.Account & { spaceId: string }>('StripeAccount')
  .implement({
    fields: (t) => ({
      id: t.field({
        type: 'ID',
        resolve: (account) => encodeGlobalID('StripeAccount', account.spaceId),
      }),
      accountId: t.exposeID('id'),
      chargesEnabled: t.exposeBoolean('charges_enabled'),
      detailsSubmitted: t.exposeBoolean('details_submitted'),
      payoutsEnabled: t.exposeBoolean('payouts_enabled'),
      requirements: t.field({
        type: ['String'],
        resolve: async (account) => account.requirements?.currently_due as string[] ?? [],
      }),
      name: t.field({
        type: 'String',
        nullable: true,
        resolve: (account) => {
          return account.business_profile?.name ?? account.settings?.dashboard?.display_name ?? 'Unknown';
        },
      }),
      isOnboardingComplete: t.field({
        type: 'Boolean',
        resolve: (account) => {
          return account.details_submitted && account.charges_enabled && account.payouts_enabled;
        },
      }),
    }),
  });

// Mutation
builder.mutationField('updateStripeAccount', (t) =>
  t.field({
    authScopes: {
      admin: true,
      space: 'owner',
    },
    type: StripeAccount,
    nullable: true,
    args: {...},
    async resolve(_root, _args, { decodedSpaceId, spaceId }) {
      // Update the stripe account

      const account = await stripe.accounts.retrieve(id);

      // Websocket call
      fetch(`http://127.0.0.1:1999/parties/spaces/${spaceId}`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          type: 'stripeAccount:update',
          data: account, // this is where I'd wanna convert from stripe schema to StripeAccount schema defined above
        }),
      });

      return {
        ...account,
        spaceId: space.id,
      };
    },
  }),
);
@emroot
Copy link
Author

emroot commented Sep 10, 2024

this is what I have in mind:

StripeAccount.init(account).toGraphql()

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

No branches or pull requests

1 participant