Skip to content

Commit

Permalink
fix: wip json support
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed May 6, 2020
1 parent a9e252c commit 1c03aab
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
55 changes: 55 additions & 0 deletions src/packages/client/src/__tests__/json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { DMMFClass } from '../runtime/dmmf'
import { makeDocument } from '../runtime/query'
import { getDMMF } from '../generation/getDMMF'

const datamodel = `\
datasource db {
provider = "postgres"
url = env("DB")
}
model User {
id Int @id @default(autoincrement())
name String
email String @unique
json Json
}`

let dmmf
beforeAll(async () => {
const dmmfDocument = await getDMMF({ datamodel })
dmmf = new DMMFClass(dmmfDocument)
})

describe('json', () => {
test('should be able to create json', async () => {
const document = makeDocument({
dmmf,
select: {
data: {
email: 'a@a.de',
json: {
hello: 'world',
},
name: 'Bob',
},
},
rootTypeName: 'mutation',
rootField: 'createOneUser',
})
expect(String(document)).toMatchInlineSnapshot(`
"mutation {
createOneUser(data: {
email: \\"a@a.de\\"
json: \\"{\\\\\\"hello\\\\\\":\\\\\\"world\\\\\\"}\\"
name: \\"Bob\\"
}) {
id
name
email
json
}
}"
`)
})
})
1 change: 1 addition & 0 deletions src/packages/client/src/fixtures/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ model User {
/// name comment
name String?
posts Post[]
json Json?
}
model Post {
Expand Down
67 changes: 64 additions & 3 deletions src/packages/client/src/runtime/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,16 @@ export class Args {
* @param _
* @param tab
*/
function stringify(obj, _?: any, tabbing?: string | number, isEnum?: boolean) {
function stringify(
obj,
_?: any,
tabbing?: string | number,
isEnum?: boolean,
isJson?: boolean,
) {
if (isJson) {
return JSON.stringify(JSON.stringify(obj))
}
if (obj === undefined) {
return null
}
Expand Down Expand Up @@ -757,7 +766,13 @@ ${indent(value.toString(), 2)}
)}${isScalar ? '' : '\n'}]`
}

return `${key}: ${stringify(value, null, 2, this.isEnum)}`
return `${key}: ${stringify(
value,
null,
2,
this.isEnum,
this.argType === 'Json',
)}`
}
public toString() {
return this._toString(this.value, this.key)
Expand Down Expand Up @@ -1610,7 +1625,8 @@ export function unpack({ document, path, data }: UnpackOptions): any {

const field = getField(document, path)

return mapDates({ field, data: result })
const mappedData = mapDates({ field, data: result })
return mapJson({ field, data: mappedData })
}

export interface MapDatesOptions {
Expand Down Expand Up @@ -1663,6 +1679,51 @@ export function mapDates({ field, data }: MapDatesOptions): any {
return data
}

export function mapJson({ field, data }: MapDatesOptions): any {
if (
!data ||
typeof data !== 'object' ||
!field.children ||
!field.schemaField
) {
return data
}

for (const child of field.children) {
if (child.schemaField && child.schemaField.outputType.type === 'Json') {
if (Array.isArray(data)) {
for (const entry of data) {
// in the very unlikely case, that a field is not there in the result, ignore it
if (typeof entry[child.name] !== 'undefined') {
entry[child.name] = entry[child.name]
? JSON.parse(entry[child.name])
: entry[child.name]
}
}
} else {
// same here, ignore it if it's undefined
if (typeof data[child.name] !== 'undefined') {
data[child.name] = data[child.name]
? JSON.parse(data[child.name])
: data[child.name]
}
}
}

if (child.schemaField && child.schemaField.outputType.kind === 'object') {
if (Array.isArray(data)) {
data.forEach((entry) =>
mapJson({ field: child, data: entry[child.name] }),
)
} else {
mapJson({ field: child, data: data[child.name] })
}
}
}

return data
}

export function getField(document: Document, path: string[]): Field {
const todo = path.slice() // let's create a copy to not fiddle with the input argument
const firstElement = todo.shift()
Expand Down

0 comments on commit 1c03aab

Please sign in to comment.