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

How to Modify the Result of a PgSelectStep #2161

Closed
iamameme opened this issue Aug 19, 2024 · 5 comments
Closed

How to Modify the Result of a PgSelectStep #2161

iamameme opened this issue Aug 19, 2024 · 5 comments

Comments

@iamameme
Copy link

iamameme commented Aug 19, 2024

I know that PgSelectStep is opaque and cannot be fed into another step. I'm thinking this isn't possible but, is there a way to modify the result of this step?

Example:
The GraphQL schema we have has some legacy fields. One field, for example, returns the same values as the database, but the key names are slightly different. Such as is_primary and is instead isPrimary.

Right now, we are using a withPgClient step to make the same query we would make with a PgSelectStep in order to have the values of the rows so we can modify the names of the keys.

Is there a better way to do this? Thank you :)

In code:

What I want to do:

const $allocations = team_allocation.find();
const frag = $allocations.placeholder($workdayId, TYPES.text);
$allocations.where(sql`${$allocations.alias}."teamId" = ${frag}`);

return loadMany(
              list([$allocations, $warLegacyId]),
              ['allocations', 'warLegacyId'],
              ([allocations, warLegacyId]: any) => {
                return allocations.map((allocation: any) => ({
                  _created: allocation.createdAt.toISOString(),
                  _updated: allocation.updatedAt.toISOString(),
                  _version: 123456789, // DEPRECATED
                  allocation_group_id: warLegacyId,
                  is_primary: allocation.isPrimary,
                  lastSync: allocation.updatedAt.toISOString(),
                  person_rid: allocation.personRid,
                }));
              },
            );

Then take the result of this and modify it slightly

What we are doing instead:

const $teamAllocations = withPgClient(
              teamExecutor,
              list([$workdayId, $warLegacyId]),
              async (pgClient, [workdayId, warLegacyId]) => {
                if (!workdayId || !warLegacyId) return null;

                const teamAllocations = await pgClient.query<{ id: number }>({
                  text: `select * from team_allocation where "teamId" = $1`,
                  values: [workdayId],
                });

                return teamAllocations.rows.map((allocation: any) => ({
                  _created: allocation.createdAt.toISOString(),
                  _updated: allocation.updatedAt.toISOString(),
                  _version: 123456789, // DEPRECATED
                  allocation_group_id: warLegacyId,
                  is_primary: allocation.isPrimary,
                  lastSync: allocation.updatedAt.toISOString(),
                  person_rid: allocation.personRid,
                }));
              },
            );
@github-project-automation github-project-automation bot moved this to 🌳 Triage in V5.0.0 Aug 19, 2024
@benjie
Copy link
Member

benjie commented Aug 21, 2024

Instead of list([$allocations, $warLegacyId]) in your first example, you could use list([$resolvedAllocations, $warLegacyId]) where:

const $resolvedAllocations = applyTransforms(each($allocations, $a => object({
  createdAt: $a.get('createdAt'),
  updatedAt: $a.get('updatedAt'),
  isPrimary: $a.get('isPrimary'),
  personRid: $a.get('personRid'),
})));

Not sure if this is what you're looking for?

@iamameme
Copy link
Author

iamameme commented Aug 21, 2024

This is exactly what I was looking for! Thank you so much. I couldn't find this anywhere in the documentation or code! applyTransformers was exactly what I needed.

Think it would be worth it for me to open a PR and add some documentation around this? I think people would find it very useful.

@benjie
Copy link
Member

benjie commented Aug 22, 2024

Yes please; see this issue and the linked discussion for some hints on documenting it:

graphile/crystal-pre-merge#416

Also if you get into the documentation mood... there's 27 other issues that could do with some attention - have a look in the "Docs Improvements" column:

https://github.com/orgs/graphile/projects/3/views/1

@benjie
Copy link
Member

benjie commented Aug 22, 2024

Closing this as I think it's covered by graphile/crystal-pre-merge#416

@iamameme
Copy link
Author

I am in a documentation mood! I took my first stab at writing documentation around this, and if all goes well, will move onto more on that list :)

#2163

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

No branches or pull requests

2 participants