Skip to content

Commit

Permalink
fix(client): only arrays in or
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jun 5, 2020
1 parent c637e11 commit 6d8f5ca
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/packages/client/src/__tests__/__snapshots__/or.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`at least one validation valid or query 1`] = `
"query {
findManyUser(where: {
OR: [
{
email: \\"\\"
}
]
}) {
id
name
email
status
nicknames
permissions
favoriteTree
locationId
someFloats
}
}"
`;
103 changes: 103 additions & 0 deletions src/packages/client/src/__tests__/or.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import stripAnsi from 'strip-ansi'
import { enums } from '../fixtures/enums'
import { DMMFClass, makeDocument } from '../runtime'
import { getDMMF } from '../generation/getDMMF'

describe('at least one validation', () => {
let dmmf
beforeAll(async () => {
dmmf = new DMMFClass(await getDMMF({ datamodel: enums }))
})

test('invalid or query', () => {
const select = {
where: {
OR: {
email: {},
},
},
}
const document = makeDocument({
dmmf,
select,
rootTypeName: 'query',
rootField: 'findManyUser',
})
expect(String(document)).toMatchInlineSnapshot(`
"query {
findManyUser(where: {
OR: {
\\"email\\": {}
}
}) {
id
name
email
status
nicknames
permissions
favoriteTree
locationId
someFloats
}
}"
`)
try {
document.validate(select, false, 'users')
} catch (e) {
expect(stripAnsi(e.message)).toMatchInlineSnapshot(`
"
Invalid \`prisma.users()\` invocation:
{
where: {
OR: {
email: {}
}
~~~~~~~~~~~
}
}
Argument OR: Got invalid value
{
email: {}
}
on prisma.findManyUser. Provided Json, expected List<UserWhereInput>:
type UserWhereInput {
id?: String | StringFilter
name?: String | StringFilter
email?: String | StringFilter
status?: String | StringFilter
favoriteTree?: Tree | TreeFilter
locationId?: Int | IntFilter
posts?: PostFilter
AND?: UserWhereInput
OR?: UserWhereInput
NOT?: UserWhereInput
location?: LocationWhereInput
}
"
`)
}
})
test('valid or query', () => {
const select = {
where: {
OR: [
{
email: '',
},
],
},
}
const document = makeDocument({
dmmf,
select,
rootTypeName: 'query',
rootField: 'findManyUser',
})
expect(String(document)).toMatchSnapshot()
expect(() => document.validate(select, false, 'users')).not.toThrow()
})
})
6 changes: 5 additions & 1 deletion src/packages/client/src/generation/TSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,11 @@ export class InputField implements Generatable {
const fieldInputType = field.inputType[0]
const optionalStr = fieldInputType.isRequired ? '' : '?'
if (fieldInputType.isList) {
fieldType = `Enumerable<${fieldType}>`
if (field.name === 'OR') {
fieldType = `Array<${fieldType}>`
} else {
fieldType = `Enumerable<${fieldType}>`
}
}
const nullableStr =
!fieldInputType.isRequired && !hasNull && fieldInputType.isNullable
Expand Down
4 changes: 4 additions & 0 deletions src/packages/client/src/runtime/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,11 @@ function valueToArg(key: string, value: any, arg: DMMF.SchemaArg): Arg | null {
// that's fine for us as we can just turn this into a list with a single item
// and GraphQL even allows this. We're going the conservative route though
// and actually generate the [] around the value

if (!Array.isArray(value)) {
if (key === 'OR' && arg.name === 'OR' && arg.isRelationFilter) {
return scalarToArg(key, value, arg, argInputType)
}
value = [value]
}

Expand Down

0 comments on commit 6d8f5ca

Please sign in to comment.