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
25 changes: 17 additions & 8 deletions src/graphql-utlities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ import {
export const PlainGraphQLError = z.object({
message: z.string(),
locations: z.array(z.object({ line: z.number(), column: z.number() })),
path: z.array(z.union([z.string(), z.number()])),
path: z.array(z.union([z.string(), z.number()])).optional(),
extensions: z.object({ code: z.string() }),
});
export type PlainGraphQLError = z.infer<typeof PlainGraphQLError>;

export const PlainGraphQLResponse = z.object({
const PlainSuccessfulGraphQLResponse = z.object({
data: z.unknown(),
errors: z.array(PlainGraphQLError).optional(),
});
export type PlainGraphQLResponse = z.infer<typeof PlainGraphQLResponse>;

export function isPlainGraphQLResponse(x: unknown): x is PlainGraphQLResponse {
return PlainGraphQLResponse.safeParse(x).success;
export function isPlainSuccessfulGraphQLResponse(
x: unknown
): x is z.infer<typeof PlainSuccessfulGraphQLResponse> {
return PlainSuccessfulGraphQLResponse.safeParse(x).success;
}

const PlainFailedGraphQLResponse = z.object({
errors: z.array(PlainGraphQLError),
});

export function isPlainFailedGraphQLResponse(
x: unknown
): x is z.infer<typeof PlainFailedGraphQLResponse> {
return PlainFailedGraphQLResponse.safeParse(x).success;
}

const PlainMutationResponse = z.record(
Expand All @@ -42,9 +52,8 @@ const PlainMutationResponse = z.record(
}),
})
);
type PlainMutationResponse = z.infer<typeof PlainMutationResponse>;

function isPlainMutationResponse(x: unknown): x is PlainMutationResponse {
function isPlainMutationResponse(x: unknown): x is z.infer<typeof PlainMutationResponse> {
return PlainMutationResponse.safeParse(x).success;
}

Expand Down
12 changes: 8 additions & 4 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { print } from 'graphql';

import type { Context } from './context';
import type { PlainSDKError } from './error';
import { getMutationErrorFromResponse, isPlainGraphQLResponse } from './graphql-utlities';
import {
getMutationErrorFromResponse,
isPlainFailedGraphQLResponse,
isPlainSuccessfulGraphQLResponse,
} from './graphql-utlities';
import type { Result } from './result';

const defaultUrl = 'https://core-api.uk.plain.com/graphql/v1';
Expand Down Expand Up @@ -45,7 +49,7 @@ export async function request<Query, Variables>(
}
);

if (!isPlainGraphQLResponse(res)) {
if (!isPlainSuccessfulGraphQLResponse(res)) {
throw new Error('Unexpected response received');
}

Expand Down Expand Up @@ -88,11 +92,11 @@ export async function request<Query, Variables>(
};
}

if (err.response.status === 400 && isPlainGraphQLResponse(err.response.data)) {
if (err.response.status === 400 && isPlainFailedGraphQLResponse(err.response.data)) {
return {
error: {
type: 'bad_request',
message: 'Missing or invalid arguments provided.',
message: 'Malformed query, missing or invalid arguments provided.',
graphqlErrors: err.response.data.errors || [],
requestId: getRequestId(err.response.headers),
},
Expand Down
2 changes: 1 addition & 1 deletion src/tests/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('query test - customer by id', () => {

const err: PlainSDKError = {
type: 'bad_request',
message: 'Missing or invalid arguments provided.',
message: 'Malformed query, missing or invalid arguments provided.',
graphqlErrors,
requestId: 'req_1',
};
Expand Down
41 changes: 29 additions & 12 deletions src/tests/raw-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { describe, expect, test } from 'vitest';

import { PlainClient } from '..';
import type { PlainSDKError } from '../error';
import { PlainGraphQLError } from '../graphql-utlities';

describe('raw request', () => {
test('make a request as defined', async () => {
Expand Down Expand Up @@ -60,16 +59,34 @@ describe('raw request', () => {
scope.done();
});

test('handles graphql errors', async () => {
const graphqlErrors: PlainGraphQLError[] = [
test.each([
[
{
message: 'Variable "$customerId" of required type "ID!" was not provided.',
locations: [{ line: 1, column: 20 }],
extensions: { code: 'BAD_USER_INPUT' },
path: ['a', 1],
name: 'with path',
errors: [
{
message: 'Variable "$customerId" of required type "ID!" was not provided.',
locations: [{ line: 1, column: 20 }],
extensions: { code: 'BAD_USER_INPUT' },
path: ['a', 1],
},
],
},
];

],
[
{
name: 'without path',
errors: [
{
message: 'Variable "$customerId" of required type "ID!" was not provided.',
locations: [{ line: 1, column: 20 }],
extensions: { code: 'BAD_USER_INPUT' },
},
],
},
],
])('handles graphql errors ($testCase.name)', async (testCase) => {
const { errors } = testCase;
const query = 'query customer { customer() { id }}';
const variables = {};

Expand All @@ -82,7 +99,7 @@ describe('raw request', () => {
.reply(
400,
{
errors: graphqlErrors,
errors,
},
{
'apigw-requestid': 'req_2',
Expand All @@ -97,8 +114,8 @@ describe('raw request', () => {

const err: PlainSDKError = {
type: 'bad_request',
message: 'Missing or invalid arguments provided.',
graphqlErrors,
message: 'Malformed query, missing or invalid arguments provided.',
graphqlErrors: errors,
requestId: 'req_2',
};

Expand Down