Skip to content
Open
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
153 changes: 153 additions & 0 deletions packages/batch-delegate/tests/errorPaths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { graphql, GraphQLError } from 'graphql';
import { batchDelegateToSchema } from '@graphql-tools/batch-delegate';
import { delegateToSchema } from '@graphql-tools/delegate';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { stitchSchemas } from '@graphql-tools/stitch';

class NotFoundError extends GraphQLError {
constructor(id: unknown) {
super('Not Found', undefined, undefined, undefined, undefined, undefined, { id });
}
}

describe('preserves error path indices', () => {
const getProperty = jest.fn((id: unknown) => {
return new NotFoundError(id);
});

beforeEach(() => {
getProperty.mockClear();
});

const subschema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Property {
id: ID!
}

type Object {
id: ID!
propertyId: ID!
}

type Query {
objects: [Object!]!
propertyById(id: ID!): Property
propertiesByIds(ids: [ID!]!): [Property]!
}
`,
resolvers: {
Query: {
objects: () => {
return [
{ id: '1', propertyId: '1' },
{ id: '2', propertyId: '1' },
];
},
propertyById: (_, args) => getProperty(args.id),
propertiesByIds: (_, args) => args.ids.map(getProperty),
},
},
});

const subschemas = [subschema];
const typeDefs = /* GraphQL */ `
extend type Object {
property: Property
}
`;

const query = /* GraphQL */ `
query {
objects {
id
property {
id
}
}
}
`;

const expected = {
errors: [
{
message: 'Not Found',
extensions: { id: '1' },
path: ['objects', 0, 'property'],
},
{
message: 'Not Found',
extensions: { id: '1' },
path: ['objects', 1, 'property'],
},
],
data: {
objects: [
{
id: '1',
property: null as null,
},
{
id: '2',
property: null as null,
},
],
},
};

test('using delegateToSchema', async () => {
const schema = stitchSchemas({
subschemas,
typeDefs,
resolvers: {
Object: {
property: {
selectionSet: '{ propertyId }',
resolve: (source, _, context, info) => {
return delegateToSchema({
schema: subschema,
fieldName: 'propertyById',
args: { id: source.propertyId },
context,
info,
});
},
},
},
},
});

const result = await graphql(schema, query);

Check failure on line 120 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Full Check on GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 120 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 18 (ubuntu-latest) and GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 120 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 20 (ubuntu-latest) and GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 120 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 22 (ubuntu-latest) and GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 120 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 23 (ubuntu-latest) and GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 120 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 18 (windows-latest) and GraphQL v16

Expected 1 arguments, but got 2.

expect(getProperty).toBeCalledTimes(2);
expect(result).toMatchObject(expected);
});

test('using batchDelegateToSchema', async () => {
const schema = stitchSchemas({
subschemas,
typeDefs,
resolvers: {
Object: {
property: {
selectionSet: '{ propertyId }',
resolve: (source, _, context, info) => {
return batchDelegateToSchema({
schema: subschema,
fieldName: 'propertiesByIds',
key: source.propertyId,
context,
info,
});
},
},
},
},
});

const result = await graphql(schema, query);

Check failure on line 148 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Full Check on GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 148 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 18 (ubuntu-latest) and GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 148 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 20 (ubuntu-latest) and GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 148 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 22 (ubuntu-latest) and GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 148 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 23 (ubuntu-latest) and GraphQL v16

Expected 1 arguments, but got 2.

Check failure on line 148 in packages/batch-delegate/tests/errorPaths.test.ts

View workflow job for this annotation

GitHub Actions / Unit Test on Node 18 (windows-latest) and GraphQL v16

Expected 1 arguments, but got 2.

expect(getProperty).toBeCalledTimes(1);
expect(result).toMatchObject(expected);
});
});
Loading