Skip to content

Commit

Permalink
fix(client): support all iso strings
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed May 5, 2020
1 parent faa6756 commit 61044f6
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,63 @@ exports[`select validation Allow deep select query 1`] = `
}"
`;

exports[`select validation Allow different iso strings 1 1`] = `
"mutation {
createOnePost(data: {
title: \\"Some title\\"
content: null
published: false
createdAt: \\"2020-05-05T16:28:33.983Z\\"
}) {
id
createdAt
updatedAt
published
title
content
authorId
}
}"
`;

exports[`select validation Allow different iso strings 2 1`] = `
"mutation {
createOnePost(data: {
title: \\"Some title\\"
content: null
published: false
createdAt: \\"2020-05-05T16:28:33.983+03:00\\"
}) {
id
createdAt
updatedAt
published
title
content
authorId
}
}"
`;

exports[`select validation Allow different iso strings 2 2`] = `
"mutation {
createOnePost(data: {
title: \\"Some title\\"
content: null
published: false
createdAt: \\"2020-05-05T16:28:33.983-02:00\\"
}) {
id
createdAt
updatedAt
published
title
content
authorId
}
}"
`;

exports[`select validation Allow empty input array 1`] = `
"query {
findManyPost(where: {
Expand Down
63 changes: 63 additions & 0 deletions src/packages/client/src/__tests__/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,69 @@ describe('select validation', () => {
expect(() => document.validate(ast)).not.toThrow()
})

test('Allow different iso strings 1', () => {
const ast = {
data: {
title: 'Some title',
content: null,
published: false,
createdAt: '2020-05-05T16:28:33.983Z',
},
}

const document = makeDocument({
dmmf,
select: ast,
rootTypeName: 'mutation',
rootField: 'createOnePost',
})

expect(String(document)).toMatchSnapshot()
expect(() => document.validate(ast)).not.toThrow()
})

test('Allow different iso strings 2', () => {
const ast = {
data: {
title: 'Some title',
content: null,
published: false,
createdAt: '2020-05-05T16:28:33.983+03:00',
},
}

const document = makeDocument({
dmmf,
select: ast,
rootTypeName: 'mutation',
rootField: 'createOnePost',
})

expect(String(document)).toMatchSnapshot()
expect(() => document.validate(ast)).not.toThrow()
})

test('Allow different iso strings 2', () => {
const ast = {
data: {
title: 'Some title',
content: null,
published: false,
createdAt: '2020-05-05T16:28:33.983-02:00',
},
}

const document = 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: {
Expand Down
14 changes: 9 additions & 5 deletions src/packages/client/src/runtime/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ export function getGraphQLType(
if (date.toString() === 'Invalid Date') {
return 'String'
}
if (date.toISOString() === value) {
if (
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/.test(
value,
)
) {
return 'DateTime'
}
}
Expand Down Expand Up @@ -159,7 +163,7 @@ export function getSuggestion(
// heuristic to be not too strict, but allow some big mistakes (<= ~ 5)
distance: Math.min(
Math.floor(str.length) * 1.1,
...possibilities.map(p => p.length * 3),
...possibilities.map((p) => p.length * 3),
),
str: null,
},
Expand All @@ -183,14 +187,14 @@ export function stringifyInputType(
} else {
const body = indent(
(input as DMMF.InputType).fields // TS doesn't discriminate based on existence of fields properly
.map(arg => {
.map((arg) => {
const argInputType = arg.inputType[0]
const key = `${arg.name}`
const str = `${greenKeys ? chalk.green(key) : key}${
argInputType.isRequired ? '' : '?'
}: ${chalk.white(
arg.inputType
.map(argType =>
.map((argType) =>
argIsInputType(argType.type)
? argType.type.name
: wrapWithList(
Expand Down Expand Up @@ -262,7 +266,7 @@ export function inputTypeToJson(
// it's very useful to show to the user, which options they actually have
const showDeepType =
isRequired &&
inputType.fields.every(arg => arg.inputType[0].kind === 'object') &&
inputType.fields.every((arg) => arg.inputType[0].kind === 'object') &&
!inputType.isWhereType &&
!inputType.atLeastOne
if (nameOnly) {
Expand Down

0 comments on commit 61044f6

Please sign in to comment.