Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/sixty-timers-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@team-plain/typescript-sdk': minor
---

Updates the customer fragment so that it also fetches customer groups
61 changes: 56 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,32 @@ import {
import { request } from './request';
import type { Result } from './result';

type SDKResult<T> = Promise<Result<T, PlainSDKError>>;
type SDKResult<T> = Promise<Result<Transform<T>, PlainSDKError>>;

// Transform takes a GraphQL Fragment and transforms it into a type in which
// all of its connection-like fields (e.g. fields which have an `edges`) property
// are flattened into an array. For example:
//
// Given the type:
//
// type Fragment = {
// customerGroupMemberships: {
// edges: Array<{ node: CustomerGroupMembershipPartsFragment }>
// }
// }
//
// When we apply Transform to it, we get:
//
// type Transform<Fragment> = {
// {
// customerGroupMemberships: Array<CustomerGroupMembershipPartsFragment>
// }
//
type Transform<T> = T extends { edges: Array<{ node: infer E }> }
? Array<Transform<E>>
: T extends object
? { [K in keyof T]: Transform<T[K]> }
: T;

function nonNullable<T>(x: T | null | undefined): T {
if (x === null || x === undefined) {
Expand Down Expand Up @@ -159,7 +184,10 @@ export class PlainClient {

return unwrapData(res, (q) => ({
pageInfo: q.customers.pageInfo,
customers: q.customers.edges.map((edge) => edge.node),
customers: q.customers.edges.map((edge) => ({
...edge.node,
customerGroupMemberships: edge.node.customerGroupMemberships.edges.map((e) => e.node),
})),
totalCount: q.customers.totalCount,
}));
}
Expand All @@ -175,7 +203,15 @@ export class PlainClient {
variables,
});

return unwrapData(res, (q) => q.customer);
return unwrapData(res, (q) => {
if (!q.customer) {
return null;
}
return {
...q.customer,
customerGroupMemberships: q.customer.customerGroupMemberships.edges.map((e) => e.node),
};
});
}

/**
Expand All @@ -189,7 +225,18 @@ export class PlainClient {
variables,
});

return unwrapData(res, (q) => q.customerByEmail);
return unwrapData(res, (q) => {
if (!q.customerByEmail) {
return null;
}

return {
...q.customerByEmail,
customerGroupMemberships: q.customerByEmail.customerGroupMemberships.edges.map(
(e) => e.node
),
};
});
}

/**
Expand All @@ -207,9 +254,13 @@ export class PlainClient {
});

return unwrapData(res, (q) => {
const customer = nonNullable(q.upsertCustomer.customer);
return {
result: nonNullable(q.upsertCustomer.result),
customer: nonNullable(q.upsertCustomer.customer),
customer: {
...customer,
customerGroupMemberships: customer.customerGroupMemberships.edges.map((e) => e.node),
},
};
});
}
Expand Down
1 change: 1 addition & 0 deletions src/graphql/fragments/customerCardConfigParts.gql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fragment CustomerCardConfigParts on CustomerCardConfig {
__typename
id
title
key
Expand Down
1 change: 1 addition & 0 deletions src/graphql/fragments/customerEventParts.gql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fragment CustomerEventParts on CustomerEvent {
__typename
id
customerId
title
Expand Down
1 change: 1 addition & 0 deletions src/graphql/fragments/customerGroupMembershipParts.gql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fragment CustomerGroupMembershipParts on CustomerGroupMembership {
__typename
customerId
createdAt {
...DateTimeParts
Expand Down
1 change: 1 addition & 0 deletions src/graphql/fragments/customerGroupParts.gql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fragment CustomerGroupParts on CustomerGroup {
__typename
id
name
key
Expand Down
7 changes: 7 additions & 0 deletions src/graphql/fragments/customerParts.gql
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ fragment CustomerParts on Customer {
markedAsSpamAt {
...DateTimeParts
}
customerGroupMemberships {
edges {
node {
...CustomerGroupMembershipParts
}
}
}
}
64 changes: 33 additions & 31 deletions src/graphql/types.ts

Large diffs are not rendered by default.

35 changes: 24 additions & 11 deletions src/tests/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { describe, expect, test } from 'vitest';

import { PlainClient } from '..';
import type { PlainSDKError } from '../error';
import { CustomerByIdDocument } from '../graphql/types';
import { CustomerByIdDocument, type CustomerPartsFragment } from '../graphql/types';
import { PlainGraphQLError } from '../graphql-utlities';
import { testHelpers } from './test-helpers';

describe('query test - customer by id', () => {
test('should return a valid customer', async () => {
const customerId = 'c_123';

const response = {
const response: { data: { customer: CustomerPartsFragment } } = {
data: {
customer: {
__typename: 'Customer',
Expand All @@ -22,17 +22,27 @@ describe('query test - customer by id', () => {
email: {
email: 'test@gmail.com',
isVerified: true,
verifiedAt: { __typename: 'DateTime', iso8601: '2023-03-20T13:06:37.918Z' },
verifiedAt: {
__typename: 'DateTime',
iso8601: '2023-03-20T13:06:37.918Z',
unixTimestamp: '1699890305',
},
},
updatedAt: {
__typename: 'DateTime',
iso8601: '2023-05-01T09:54:51.715Z',
unixTimestamp: '1699890305',
},
createdAt: {
__typename: 'DateTime',
iso8601: '2023-03-20T13:06:37.961Z',
unixTimestamp: '1699890305',
},
status: 'ACTIVE',
statusChangedAt: { __typename: 'DateTime', iso8601: '2023-05-01T09:54:51.715Z' },
assignedToUser: { __typename: 'UserActor', userId: 'u_123' },
assignedAt: { __typename: 'DateTime', iso8601: '2023-04-10T15:01:54.499Z' },
updatedAt: { __typename: 'DateTime', iso8601: '2023-05-01T09:54:51.715Z' },
lastIdleAt: { __typename: 'DateTime', iso8601: '2023-03-20T13:06:47.492Z' },
createdAt: { __typename: 'DateTime', iso8601: '2023-03-20T13:06:37.961Z' },
createdBy: {},
markedAsSpamAt: null,
customerGroupMemberships: {
edges: [],
},
},
},
};
Expand All @@ -56,7 +66,10 @@ describe('query test - customer by id', () => {
});

expect(result.error).toBeUndefined();
expect(result.data).toEqual(response.data.customer);
expect(result.data).toEqual({
...response.data.customer,
customerGroupMemberships: [],
});
});

test('should accept a null customer when not found', async () => {
Expand Down