"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const strip_ansi_1 = __importDefault(require("strip-ansi"));
const blog_1 = require("../fixtures/blog");
const dmmf_1 = require("../runtime/dmmf");
const query_1 = require("../runtime/query");
const getDMMF_1 = require("../runtime/getDMMF");
let dmmf;
beforeAll(async () => {
    const dmmfDocument = await getDMMF_1.getDMMF({ datamodel: blog_1.blog });
    dmmf = new dmmf_1.DMMFClass(dmmfDocument);
});
describe('select validation', () => {
    test('unknown arg, field, incorrect arg type', () => {
        const ast = {
            skip: 200,
            where: {
                name_contains: undefined,
                name_in: ['hans', 'peter', 'schmidt'],
                AND: [
                    {
                        age_gt: 10123123123,
                        this_is_completely_arbitrary: 'veryLongNameGoIntoaNewLineNow@gmail.com',
                    },
                    {
                        age_gt: 10123123123,
                        id_endsWith: 'veryLongNameGoIntoaNewLineNow@gmail.com',
                        name_contains: 'hans',
                        name_gt: 2131203912039123,
                        name_in: ['hans'],
                        AND: [
                            {
                                age_gt: '10123123123',
                                id_endsWith: 'veryLongNameGoIntoaNewLineNow@gmail.com',
                            },
                        ],
                    },
                ],
            },
            select: {
                id: true,
                name: 'asd',
                name2: true,
                posts: {
                    first: 200,
                    select: {
                        id: true,
                        title: false,
                    },
                },
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'query',
            rootField: 'findManyUser',
        });
        expect(String(document)).toMatchSnapshot();
        try {
            document.validate(ast, undefined, undefined, 'minimal');
        }
        catch (e) {
            expect(e.message).toMatchInlineSnapshot(`
        "Unknown arg \`name_in\` in where.name_in for type UserWhereInput. Did you mean \`name\`?
        Unknown arg \`age_gt\` in where.AND.0.age_gt for type UserWhereInput. Did you mean \`name\`?
        Unknown arg \`this_is_completely_arbitrary\` in where.AND.0.this_is_completely_arbitrary for type UserWhereInput.
        Unknown arg \`age_gt\` in where.AND.1.age_gt for type UserWhereInput. Did you mean \`name\`?
        Unknown arg \`id_endsWith\` in where.AND.1.id_endsWith for type UserWhereInput.
        Unknown arg \`name_contains\` in where.AND.1.name_contains for type UserWhereInput.
        Unknown arg \`name_gt\` in where.AND.1.name_gt for type UserWhereInput. Did you mean \`name\`?
        Unknown arg \`name_in\` in where.AND.1.name_in for type UserWhereInput. Did you mean \`name\`?
        Unknown arg \`age_gt\` in where.AND.1.AND.0.age_gt for type UserWhereInput. Did you mean \`name\`?
        Unknown arg \`id_endsWith\` in where.AND.1.AND.0.id_endsWith for type UserWhereInput.
        Invalid value 'asd' of type String for field name on model User. Expected either true or false.
        Unknown field \`name2\` for select statement on model User. Did you mean \`name\`?"
      `);
        }
        try {
            document.validate(ast, undefined, undefined, 'colorless');
        }
        catch (e) {
            expect(e.message).toMatchInlineSnapshot(`
        "
        Invalid \`photon.findManyUser()\` invocation:

        {
          skip: 200,
          where: {
            name_contains: undefined,
            name_in: [
            ~~~~~~~
              'hans',
              'peter',
              'schmidt'
            ],
            AND: [
              {
                age_gt: 10123123123,
                ~~~~~~
                this_is_completely_arbitrary: 'veryLongNameGoIntoaNewLineNow@gmail.com'
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              },
              {
                age_gt: 10123123123,
                ~~~~~~
                id_endsWith: 'veryLongNameGoIntoaNewLineNow@gmail.com',
                ~~~~~~~~~~~
                name_contains: 'hans',
                ~~~~~~~~~~~~~
                name_gt: 2131203912039123,
                ~~~~~~~
                name_in: [
                ~~~~~~~
                  'hans'
                ],
                AND: [
                  {
                    age_gt: '10123123123',
                    ~~~~~~
                    id_endsWith: 'veryLongNameGoIntoaNewLineNow@gmail.com'
                    ~~~~~~~~~~~
                  }
                ]
              }
            ]
          },
          select: {
        ?   id?: true,
        ?   name?: true,
            name2: true,
            ~~~~~
        ?   posts?: true,
        ?   email?: true
          }
        }

        Unknown arg \`name_in\` in where.name_in for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`age_gt\` in where.AND.0.age_gt for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`this_is_completely_arbitrary\` in where.AND.0.this_is_completely_arbitrary for type UserWhereInput. Available args:

        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`age_gt\` in where.AND.1.age_gt for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`id_endsWith\` in where.AND.1.id_endsWith for type UserWhereInput. Available args:

        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`name_contains\` in where.AND.1.name_contains for type UserWhereInput. Available args:

        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`name_gt\` in where.AND.1.name_gt for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`name_in\` in where.AND.1.name_in for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`age_gt\` in where.AND.1.AND.0.age_gt for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`id_endsWith\` in where.AND.1.AND.0.id_endsWith for type UserWhereInput. Available args:

        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Invalid value 'asd' of type String for field name on model User. Expected either true or false.
        Unknown field \`name2\` for select statement on model User. Available options are listed in green. Did you mean \`name\`?
        "
      `);
        }
        try {
            document.validate(ast);
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "
        Invalid \`photon.findManyUser()\` invocation:

        {
          skip: 200,
          where: {
            name_contains: undefined,
            name_in: [
            ~~~~~~~
              'hans',
              'peter',
              'schmidt'
            ],
            AND: [
              {
                age_gt: 10123123123,
                ~~~~~~
                this_is_completely_arbitrary: 'veryLongNameGoIntoaNewLineNow@gmail.com'
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              },
              {
                age_gt: 10123123123,
                ~~~~~~
                id_endsWith: 'veryLongNameGoIntoaNewLineNow@gmail.com',
                ~~~~~~~~~~~
                name_contains: 'hans',
                ~~~~~~~~~~~~~
                name_gt: 2131203912039123,
                ~~~~~~~
                name_in: [
                ~~~~~~~
                  'hans'
                ],
                AND: [
                  {
                    age_gt: '10123123123',
                    ~~~~~~
                    id_endsWith: 'veryLongNameGoIntoaNewLineNow@gmail.com'
                    ~~~~~~~~~~~
                  }
                ]
              }
            ]
          },
          select: {
        ?   id?: true,
        ?   name?: true,
            name2: true,
            ~~~~~
        ?   posts?: true,
        ?   email?: true
          }
        }

        Unknown arg \`name_in\` in where.name_in for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`age_gt\` in where.AND.0.age_gt for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`this_is_completely_arbitrary\` in where.AND.0.this_is_completely_arbitrary for type UserWhereInput. Available args:

        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`age_gt\` in where.AND.1.age_gt for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`id_endsWith\` in where.AND.1.id_endsWith for type UserWhereInput. Available args:

        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`name_contains\` in where.AND.1.name_contains for type UserWhereInput. Available args:

        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`name_gt\` in where.AND.1.name_gt for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`name_in\` in where.AND.1.name_in for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`age_gt\` in where.AND.1.AND.0.age_gt for type UserWhereInput. Did you mean \`name\`? Available args:
        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Unknown arg \`id_endsWith\` in where.AND.1.AND.0.id_endsWith for type UserWhereInput. Available args:

        type UserWhereInput {
          id?: UUID | UUIDFilter
          email?: String | StringFilter
          name?: String | NullableStringFilter | null
          posts?: PostFilter
          AND?: UserWhereInput
          OR?: UserWhereInput
          NOT?: UserWhereInput
        }
        Invalid value 'asd' of type String for field name on model User. Expected either true or false.
        Unknown field \`name2\` for select statement on model User. Available options are listed in green. Did you mean \`name\`?
        "
      `);
        }
    });
    test('missing arg object', () => {
        const ast = {};
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'mutation',
            rootField: 'createOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        try {
            document.validate(ast);
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "
        Invalid \`photon.createOnePost()\` invocation:

        {
        + data: {
        +   id?: String,
        +   createdAt?: DateTime,
        +   updatedAt?: DateTime,
        +   published: Boolean,
        +   title: String,
        +   content?: String,
        +   author?: UserCreateOneWithoutAuthorInput
        + }
        }

        Argument data for data is missing.

        Note: Lines with + are required
        "
      `);
        }
    });
    test('missing arg object', () => {
        const ast = {};
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'mutation',
            rootField: 'createOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        try {
            document.validate(ast);
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "
        Invalid \`photon.createOnePost()\` invocation:

        {
        + data: {
        +   id?: String,
        +   createdAt?: DateTime,
        +   updatedAt?: DateTime,
        +   published: Boolean,
        +   title: String,
        +   content?: String,
        +   author?: UserCreateOneWithoutAuthorInput
        + }
        }

        Argument data for data is missing.

        Note: Lines with + are required
        "
      `);
        }
        try {
            document.validate(ast, undefined, undefined, 'minimal');
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "Argument data for data is missing.
        "
      `);
        }
        try {
            document.validate(ast, undefined, undefined, 'colorless');
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "
        Invalid \`photon.createOnePost()\` invocation:

        {
        + data: {
        +   id?: String,
        +   createdAt?: DateTime,
        +   updatedAt?: DateTime,
        +   published: Boolean,
        +   title: String,
        +   content?: String,
        +   author?: UserCreateOneWithoutAuthorInput
        + }
        }

        Argument data for data is missing.

        Note: Lines with + are required
        "
      `);
        }
    });
    test('missing arg scalar', () => {
        const ast = {
            data: {
                title: 'string',
                author: {
                    connect: { id: '' },
                },
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'mutation',
            rootField: 'createOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        try {
            document.validate(ast);
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "
        Invalid \`photon.createOnePost()\` invocation:

        {
          data: {
            title: 'string',
            author: {
              connect: {
                id: ''
              }
            },
        +   published: Boolean,
        ?   id?: String,
        ?   createdAt?: DateTime,
        ?   updatedAt?: DateTime,
        ?   content?: String
          }
        }

        Argument published for data.published is missing.

        Note: Lines with + are required, lines with ? are optional.
        "
      `);
        }
    });
    test('missing arg scalar && object', () => {
        const ast = {
            data: {
                title: 'string',
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'mutation',
            rootField: 'createOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        try {
            document.validate(ast);
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "
        Invalid \`photon.createOnePost()\` invocation:

        {
          data: {
            title: 'string',
        +   published: Boolean,
        ?   id?: String,
        ?   createdAt?: DateTime,
        ?   updatedAt?: DateTime,
        ?   content?: String,
        ?   author?: {
        ?     create?: UserCreateWithoutPostsInput,
        ?     connect?: UserWhereUniqueInput
        ?   }
          }
        }

        Argument published for data.published is missing.

        Note: Lines with + are required, lines with ? are optional.
        "
      `);
        }
    });
    test('Allow simple create mutation', () => {
        const ast = {
            data: {
                title: 'Some title',
                content: 'Some Content',
                published: false,
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'mutation',
            rootField: 'createOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        expect(() => document.validate(ast)).not.toThrow();
    });
    test('Allow explicit null value', () => {
        const ast = {
            data: {
                title: 'Some title',
                content: null,
                published: false,
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'mutation',
            rootField: 'createOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        expect(() => document.validate(ast)).not.toThrow();
    });
    test('Allow uuid for string input', () => {
        const ast = {
            data: {
                title: 'Some title',
                content: '123e4567-e89b-12d3-a456-426655440000',
                published: false,
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'mutation',
            rootField: 'createOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        expect(() => document.validate(ast)).not.toThrow();
    });
    test('Allow deep select query', () => {
        const ast = {
            select: {
                author: {
                    select: {
                        id: true,
                    },
                },
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'query',
            rootField: 'findManyPost',
        });
        expect(String(document)).toMatchSnapshot();
        expect(() => document.validate(ast)).not.toThrow();
    });
    test('Accept empty where in findMany', () => {
        const ast = {
            select: {
                author: {
                    select: {
                        id: true,
                    },
                },
            },
            where: {},
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'query',
            rootField: 'findManyPost',
        });
        expect(String(document)).toMatchSnapshot();
        expect(() => document.validate(ast)).not.toThrow();
    });
    test('allow where with all undefined in findMany', () => {
        const ast = {
            select: {
                author: {
                    select: {
                        id: true,
                    },
                },
            },
            where: {
                id: undefined,
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'query',
            rootField: 'findManyPost',
        });
        expect(String(document)).toMatchSnapshot();
        expect(() => document.validate(ast)).not.toThrow();
    });
    test('reject empty where for findOne', () => {
        const ast = {
            select: {
                author: {
                    select: {
                        id: true,
                    },
                },
            },
            where: {},
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'query',
            rootField: 'findOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        try {
            document.validate(ast);
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "
        Invalid \`photon.findOnePost()\` invocation:

        {
          select: {
            author: {
              select: {
                id: true
              }
            }
          },
          where: {
        ?   id?: String
          }
        }

        Argument where of type PostWhereUniqueInput needs at least one argument. Available args are listed in green.

        Note: Lines with ? are optional.
        "
      `);
        }
    });
    test('reject all undefined where for findOne', () => {
        const ast = {
            select: {
                author: {
                    select: {
                        id: true,
                    },
                },
            },
            where: {
                id: undefined,
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'query',
            rootField: 'findOnePost',
        });
        expect(String(document)).toMatchSnapshot();
        try {
            document.validate(ast);
        }
        catch (e) {
            expect(strip_ansi_1.default(e.message)).toMatchInlineSnapshot(`
        "
        Invalid \`photon.findOnePost()\` invocation:

        {
          select: {
            author: {
              select: {
                id: true
              }
            }
          },
          where: {
        ?   id?: String
          }
        }

        Argument where of type PostWhereUniqueInput needs at least one argument. Available args are listed in green.

        Note: Lines with ? are optional.
        "
      `);
        }
    });
    test('Allow uuid array for string array', () => {
        const ast = {
            select: {
                author: {
                    select: {
                        id: true,
                    },
                },
            },
            where: {
                id: {
                    in: ['d4082b42-b161-11e9-8754-6542abf52968'],
                },
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'query',
            rootField: 'findManyPost',
        });
        expect(String(document)).toMatchSnapshot();
        expect(() => document.validate(ast)).not.toThrow();
    });
    test('Allow empty input array', () => {
        const ast = {
            select: {
                author: {
                    select: {
                        id: true,
                    },
                },
            },
            where: {
                id: {
                    in: [],
                },
            },
        };
        const document = query_1.makeDocument({
            dmmf,
            select: ast,
            rootTypeName: 'query',
            rootField: 'findManyPost',
        });
        expect(String(document)).toMatchSnapshot();
        expect(() => document.validate(ast)).not.toThrow();
    });
});
//# sourceMappingURL=select.test.js.map