wgc
Version:
The official CLI tool to manage the GraphQL Federation Platform Cosmo
78 lines (71 loc) • 2.26 kB
JavaScript
import { describe, it, expect } from 'vitest';
import { lexicographicSortSchema, printSchema } from 'graphql';
import { buildSchemaWithoutDirectives } from '../schema.js';
describe('buildSchemaWithoutDirectives', () => {
it('should remove all directive definitions and usages from a federation schema', () => {
const schemaWithDirectives = `
directive on OBJECT | INTERFACE
directive on FIELD_DEFINITION
directive on FIELD_DEFINITION
directive on FIELD_DEFINITION
directive on OBJECT | INTERFACE
type Query {
topProducts: [Product]
}
type Product {
id: ID!
name: String!
price: Int
weight: Int
shippingEstimate: Int
}
type User {
id: ID!
username: String
reviews: [Review]
}
type Review {
id: ID!
author: User
product: Product!
rating: Int!
}
`;
const expectedSchema = `type Product {
id: ID!
name: String!
price: Int
shippingEstimate: Int
weight: Int
}
type Query {
topProducts: [Product]
}
type Review {
author: User
id: ID!
product: Product!
rating: Int!
}
type User {
id: ID!
reviews: [Review]
username: String
}`;
const schema = buildSchemaWithoutDirectives(schemaWithDirectives);
// Sort the schema to ensure consistent output
const sortedSchema = lexicographicSortSchema(schema);
const cleanedSchemaString = printSchema(sortedSchema);
// Compare the actual cleaned schema with expected schema
expect(cleanedSchemaString.trim()).toBe(expectedSchema.trim());
});
it('should throw an error for an invalid schema', () => {
const invalidSchema = `
type Query {
invalidField: NonExistentType
}
`;
expect(() => buildSchemaWithoutDirectives(invalidSchema)).toThrowError('Failed to parse schema: Unknown type: "NonExistentType".');
});
});
//# sourceMappingURL=schema.test.js.map