| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- import { buildSchema, printType } from 'graphql';
- import { CustomFields } from '../../../../shared/shared-types';
- import { generateListOptions } from './generate-list-options';
- // tslint:disable:no-non-null-assertion
- describe('generateListOptions()', () => {
- const COMMON_TYPES = `
- scalar JSON
- scalar DateTime
- interface PaginatedList {
- items: [Node!]!
- totalItems: Int!
- }
- interface Node {
- id: ID!
- }
- enum SortOrder {
- ASC
- DESC
- }
- input StringOperators { dummy: String }
- input BooleanOperators { dummy: String }
- input NumberRange { dummy: String }
- input NumberOperators { dummy: String }
- input DateRange { dummy: String }
- input DateOperators { dummy: String }
- type PersonList implements PaginatedList {
- items: [Person!]!
- totalItems: Int!
- }
- `;
- const removeLeadingWhitespace = s => {
- const indent = s.match(/^\s+/m)[0].replace(/\n/, '');
- return s.replace(new RegExp(`^${indent}`, 'gm'), '').trim();
- };
- it('creates the required input types', () => {
- const input = `
- ${COMMON_TYPES}
- type Query {
- people(options: PersonListOptions): PersonList
- }
- type Person {
- name: String!
- age: Int!
- }
- # Generated at runtime
- input PersonListOptions
- `;
- const result = generateListOptions(buildSchema(input));
- expect(printType(result.getType('PersonListOptions')!)).toBe(
- removeLeadingWhitespace(`
- input PersonListOptions {
- skip: Int
- take: Int
- sort: PersonSortParameter
- filter: PersonFilterParameter
- }`),
- );
- expect(printType(result.getType('PersonSortParameter')!)).toBe(
- removeLeadingWhitespace(`
- input PersonSortParameter {
- name: SortOrder
- age: SortOrder
- }`),
- );
- expect(printType(result.getType('PersonFilterParameter')!)).toBe(
- removeLeadingWhitespace(`
- input PersonFilterParameter {
- name: StringOperators
- age: NumberOperators
- }`),
- );
- });
- it('works with a non-nullabel list type', () => {
- const input = `
- ${COMMON_TYPES}
- type Query {
- people: PersonList!
- }
- type Person {
- name: String!
- age: Int!
- }
- `;
- const result = generateListOptions(buildSchema(input));
- expect(result.getType('PersonListOptions')).toBeTruthy();
- });
- it('uses the correct filter operators', () => {
- const input = `
- ${COMMON_TYPES}
- type Query {
- people(options: PersonListOptions): PersonList
- }
- type Person {
- name: String!
- age: Int!
- updatedAt: DateTime!
- admin: Boolean!
- score: Float
- personType: PersonType!
- }
- enum PersonType {
- TABS
- SPACES
- }
- # Generated at runtime
- input PersonListOptions
- `;
- const result = generateListOptions(buildSchema(input));
- expect(printType(result.getType('PersonFilterParameter')!)).toBe(
- removeLeadingWhitespace(`
- input PersonFilterParameter {
- name: StringOperators
- age: NumberOperators
- updatedAt: DateOperators
- admin: BooleanOperators
- score: NumberOperators
- personType: StringOperators
- }`),
- );
- });
- it('creates the ListOptions interface and argument if not defined', () => {
- const input = `
- ${COMMON_TYPES}
- type Query {
- people: PersonList
- }
- type Person {
- name: String!
- }
- `;
- const result = generateListOptions(buildSchema(input));
- expect(printType(result.getType('PersonListOptions')!)).toBe(
- removeLeadingWhitespace(`
- input PersonListOptions {
- skip: Int
- take: Int
- sort: PersonSortParameter
- filter: PersonFilterParameter
- }`),
- );
- const args = result.getQueryType()!.getFields().people.args;
- expect(args.length).toBe(1);
- expect(args[0].name).toBe('options');
- expect(args[0].type.toString()).toBe('PersonListOptions');
- });
- it('extends the ListOptions interface if already defined', () => {
- const input = `
- ${COMMON_TYPES}
- type Query {
- people(options: PersonListOptions): PersonList
- }
- type Person {
- name: String!
- }
- input PersonListOptions {
- categoryId: ID
- }
- `;
- const result = generateListOptions(buildSchema(input));
- expect(printType(result.getType('PersonListOptions')!)).toBe(
- removeLeadingWhitespace(`
- input PersonListOptions {
- skip: Int
- take: Int
- sort: PersonSortParameter
- filter: PersonFilterParameter
- categoryId: ID
- }`),
- );
- const args = result.getQueryType()!.getFields().people.args;
- expect(args.length).toBe(1);
- expect(args[0].name).toBe('options');
- expect(args[0].type.toString()).toBe('PersonListOptions');
- });
- it('ignores properties with types which cannot be sorted or filtered', () => {
- const input = `
- ${COMMON_TYPES}
- type Query {
- people: PersonList
- }
- type Person {
- id: ID!
- name: String!
- vitals: [Int]
- meta: JSON
- user: User!
- }
- type User {
- identifier: String!
- }
- `;
- const result = generateListOptions(buildSchema(input));
- expect(printType(result.getType('PersonSortParameter')!)).toBe(
- removeLeadingWhitespace(`
- input PersonSortParameter {
- id: SortOrder
- name: SortOrder
- }`),
- );
- expect(printType(result.getType('PersonFilterParameter')!)).toBe(
- removeLeadingWhitespace(`
- input PersonFilterParameter {
- name: StringOperators
- }`),
- );
- });
- it('generates ListOptions for nested list queries', () => {
- const input = `
- ${COMMON_TYPES}
- type Query {
- people: PersonList
- }
- type Person {
- id: ID!
- orders(options: OrderListOptions): OrderList
- }
- type OrderList implements PaginatedList {
- items: [Order!]!
- totalItems: Int!
- }
- type Order {
- id: ID!
- code: String!
- }
- # Generated at runtime
- input OrderListOptions
- `;
- const result = generateListOptions(buildSchema(input));
- expect(printType(result.getType('OrderListOptions')!)).toBe(
- removeLeadingWhitespace(`
- input OrderListOptions {
- skip: Int
- take: Int
- sort: OrderSortParameter
- filter: OrderFilterParameter
- }`),
- );
- expect(printType(result.getType('OrderSortParameter')!)).toBe(
- removeLeadingWhitespace(`
- input OrderSortParameter {
- id: SortOrder
- code: SortOrder
- }`),
- );
- expect(printType(result.getType('OrderFilterParameter')!)).toBe(
- removeLeadingWhitespace(`
- input OrderFilterParameter {
- code: StringOperators
- }`),
- );
- });
- });
|