Skip to content

Commit

Permalink
fix(client): type coercion
Browse files Browse the repository at this point in the history
Close #2784
  • Loading branch information
timsuchanek committed Jun 22, 2020
1 parent 43b704f commit 30c1ea4
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/packages/client/fixtures/scalarList/.gitignore
@@ -0,0 +1,3 @@
@prisma
migrations
db
23 changes: 23 additions & 0 deletions src/packages/client/fixtures/scalarList/main.ts
@@ -0,0 +1,23 @@
import { PrismaClient } from './@prisma/client'

async function main() {
const prisma = new PrismaClient()

await prisma.user.create({
data: {
hobbies: {
set: [
'sample 1 string',
'7fb1aef9-5250-4cf6-92c7-b01f53862822',
'sample 3 string',
'575e0b28-81fa-43e0-8f05-708a98d55c14',
'sample 5 string',
],
},
name: 'name',
},
})
prisma.disconnect()
}

main().catch(console.error)
10 changes: 10 additions & 0 deletions src/packages/client/fixtures/scalarList/prisma/schema.prisma
@@ -0,0 +1,10 @@
datasource my_db {
provider = "postgres"
url = env("POSTGRES_URL")
}

model User {
id String @default(cuid()) @id
name String
hobbies String[]
}
10 changes: 10 additions & 0 deletions src/packages/client/fixtures/scalarList/tsconfig.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"target": "es2018",
"module": "commonjs",
"esModuleInterop": true,
"noImplicitAny": false,
"lib": ["esnext"]
}
}
66 changes: 66 additions & 0 deletions src/packages/client/src/__tests__/uuid.test.ts
@@ -0,0 +1,66 @@
import { DMMFClass, makeDocument } from '../runtime'
import { getDMMF } from '../generation/getDMMF'

const datamodel = `datasource my_db {
provider = "postgres"
url = env("POSTGRES_URL")
}
model User {
id String @default(cuid()) @id
name String
hobbies String[]
}
`

describe('at least one validation', () => {
let dmmf
beforeAll(async () => {
dmmf = new DMMFClass(await getDMMF({ datamodel }))
})
test('string first', () => {
const select = {
data: {
hobbies: {
set: [
'sample 1 string',
'7fb1aef9-5250-4cf6-92c7-b01f53862822',
'sample 3 string',
'575e0b28-81fa-43e0-8f05-708a98d55c14',
'sample 5 string',
],
},
name: 'name',
},
}
const document = makeDocument({
dmmf,
select,
rootTypeName: 'mutation',
rootField: 'createOneUser',
})
expect(() => document.validate(select, false)).not.toThrow()
})
test('uuid first', () => {
const select = {
data: {
hobbies: {
set: [
'7fb1aef9-5250-4cf6-92c7-b01f53862822',
'sample 3 string',
'575e0b28-81fa-43e0-8f05-708a98d55c14',
'sample 5 string',
],
},
name: 'name',
},
}
const document = makeDocument({
dmmf,
select,
rootTypeName: 'mutation',
rootField: 'createOneUser',
})
expect(() => document.validate(select, false)).not.toThrow()
})
})
7 changes: 7 additions & 0 deletions src/packages/client/src/runtime/query.ts
Expand Up @@ -1310,6 +1310,13 @@ function hasCorrectScalarType(
if (graphQLType === 'List<String>' && expectedType === 'List<ID>') {
return true
}
if (
expectedType === 'List<String>' &&
(graphQLType === 'List<String | UUID>' ||
graphQLType === 'List<UUID | String>')
) {
return true
}

// Int is a subset of Float
if (graphQLType === 'Int' && expectedType === 'Float') {
Expand Down

0 comments on commit 30c1ea4

Please sign in to comment.