Skip to content

Commit

Permalink
feat(client): result extensions runtime (#16343)
Browse files Browse the repository at this point in the history
* feat(client): result extensions runtime

Core of implemenation consists of 3 functions:

- `getAllComputedFields` is responsible for resolving all depenencies
  and naming conflicts between computed fields and producing a list of
  computed that could potentially be applied to specific model.
- `applyResultExtensions` function applies computed field to single part
  of the query result. Does not traverse relationship.
- `visitQueryResult` traverses the response and calls provided
  callback for each part of the response, corresponding to particular
  model and allows to modify it.

By combining all 3 of them, we are able to resolve and apply all
computed fields to any query response recursively, following all the
relationships.
  • Loading branch information
SevInf authored and jkomyno committed Dec 21, 2022
1 parent ff0ca78 commit 48bf0af
Show file tree
Hide file tree
Showing 50 changed files with 1,772 additions and 38 deletions.
2 changes: 2 additions & 0 deletions packages/client/src/__tests__/atLeastOne.test.ts
Expand Up @@ -21,6 +21,7 @@ describe('at least one validation', () => {
select,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
expect(String(document)).toMatchInlineSnapshot(`
query {
Expand Down Expand Up @@ -85,6 +86,7 @@ describe('at least one validation', () => {
select,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
expect(String(document)).toMatchSnapshot()
expect(() => document.validate(select, false, 'users')).not.toThrow()
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/__tests__/atomicOperationsUpdate.test.ts
Expand Up @@ -14,6 +14,7 @@ function getTransformedDocument(select) {
select,
rootTypeName: 'mutation',
rootField: 'updateOneUser',
extensions: [],
})
return String(transformDocument(document))
}
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('minimal atomic update transformation', () => {
select,
rootTypeName: 'mutation',
rootField: 'updateOneUser',
extensions: [],
})

expect(String(document)).toMatchInlineSnapshot(`
Expand Down
12 changes: 12 additions & 0 deletions packages/client/src/__tests__/batching.test.ts
Expand Up @@ -38,6 +38,7 @@ describe('batching', () => {
},
rootTypeName: 'query',
rootField: 'findUniqueUser',
extensions: [],
}),
isList: false,
rootField: 'query',
Expand All @@ -47,6 +48,7 @@ describe('batching', () => {
id: '1',
},
},
extensions: [],
}),
fetcher.request({
clientMethod: 'findUnique',
Expand All @@ -60,6 +62,7 @@ describe('batching', () => {
},
rootTypeName: 'query',
rootField: 'findUniqueUser',
extensions: [],
}),
isList: false,
rootField: 'query',
Expand All @@ -69,6 +72,7 @@ describe('batching', () => {
id: '2',
},
},
extensions: [],
}),
])

Expand Down Expand Up @@ -160,13 +164,15 @@ describe('batching', () => {
},
rootTypeName: 'query',
rootField: 'findUniquePost',
extensions: [],
}),
isList: false,
rootField: 'query',
typeName: 'User',
args: {
where: { id: '1' },
},
extensions: [],
}),
fetcher.request({
clientMethod: 'findUnique',
Expand All @@ -180,13 +186,15 @@ describe('batching', () => {
},
rootTypeName: 'query',
rootField: 'findUniqueUser',
extensions: [],
}),
isList: false,
rootField: 'query',
typeName: 'User',
args: {
where: { id: '2' },
},
extensions: [],
}),
])

Expand Down Expand Up @@ -278,11 +286,13 @@ describe('batching', () => {
},
rootTypeName: 'query',
rootField: 'findUniqueUser',
extensions: [],
}),
isList: false,
rootField: 'query',
typeName: 'User',
args: { where: { email: 'a@a.de' } },
extensions: [],
}),
fetcher.request({
clientMethod: 'findUnique',
Expand All @@ -296,11 +306,13 @@ describe('batching', () => {
},
rootTypeName: 'query',
rootField: 'findUniqueUser',
extensions: [],
}),
isList: false,
rootField: 'query',
typeName: 'User',
args: { where: { id: '2' } },
extensions: [],
}),
])

Expand Down
1 change: 1 addition & 0 deletions packages/client/src/__tests__/deepAndOr.test.ts
Expand Up @@ -101,6 +101,7 @@ function getTransformedDocument(select) {
select,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
return String(transformDocument(document))
}
1 change: 1 addition & 0 deletions packages/client/src/__tests__/deepQuery.test.ts
Expand Up @@ -88,6 +88,7 @@ function getTransformedDocument(select) {
select,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
return String(transformDocument(document))
}
1 change: 1 addition & 0 deletions packages/client/src/__tests__/deepSome.test.ts
Expand Up @@ -81,6 +81,7 @@ function getTransformedDocument(select) {
select,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
return String(transformDocument(document))
}
12 changes: 12 additions & 0 deletions packages/client/src/__tests__/include.test.ts
Expand Up @@ -28,6 +28,7 @@ describe('include validation', () => {
select: ast,
rootTypeName: 'query',
rootField: 'findManyPost',
extensions: [],
})

expect(String(document)).toMatchSnapshot()
Expand All @@ -44,6 +45,7 @@ describe('include validation', () => {
select: ast,
rootTypeName: 'query',
rootField: 'findManyPost',
extensions: [],
})

expect(String(document)).toMatchSnapshot()
Expand Down Expand Up @@ -77,6 +79,7 @@ The \`include\` statement for type Post must not be empty. Available options are
select: ast,
rootTypeName: 'query',
rootField: 'findManyNoRelations',
extensions: [],
})

expect(String(document)).toMatchSnapshot()
Expand All @@ -92,6 +95,7 @@ The \`include\` statement for type Post must not be empty. Available options are
dmmf,
select: ast,
rootTypeName: 'query',
extensions: [],
rootField: 'findManyNoRelations',
})

Expand Down Expand Up @@ -126,6 +130,7 @@ The \`include\` statement for type Post must not be empty. Available options are
select: ast,
rootTypeName: 'query',
rootField: 'findManyNoRelations',
extensions: [],
})

expect(String(document)).toMatchSnapshot()
Expand Down Expand Up @@ -167,6 +172,7 @@ The \`include\` statement for type Post must not be empty. Available options are
select: ast,
rootTypeName: 'query',
rootField: 'findManyPost',
extensions: [],
})

expect(String(document)).toMatchSnapshot()
Expand All @@ -185,6 +191,7 @@ The \`include\` statement for type Post must not be empty. Available options are
select: ast,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})

expect(String(document)).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -231,6 +238,7 @@ The \`include\` statement for type Post must not be empty. Available options are
select: ast,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})

expect(String(document)).toMatchSnapshot()
Expand All @@ -249,6 +257,7 @@ The \`include\` statement for type Post must not be empty. Available options are
select: ast,
rootTypeName: 'query',
rootField: 'findManyPost',
extensions: [],
})

expect(String(document)).toMatchSnapshot()
Expand Down Expand Up @@ -289,6 +298,7 @@ Note, that include statements only accept relation fields.
select: ast,
rootTypeName: 'query',
rootField: 'findManyPost',
extensions: [],
})

expect(String(document)).toMatchSnapshot()
Expand Down Expand Up @@ -333,6 +343,7 @@ Unknown field \`mauthor\` for include statement on model Post. Available options
select: ast,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})

expect(String(document)).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -380,6 +391,7 @@ Unknown field \`mauthor\` for include statement on model Post. Available options
select: ast,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})

expect(String(document)).toMatchInlineSnapshot(`
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Expand Up @@ -47,11 +47,11 @@ describe('modify-client', () => {
expect(Object.keys(client).filter((k) => !k.startsWith('_'))).toMatchInlineSnapshot(`
Array [
$extends,
prop,
prop2,
user,
profile,
post,
prop,
prop2,
]
`)

Expand Down
7 changes: 7 additions & 0 deletions packages/client/src/__tests__/json.test.ts
Expand Up @@ -64,6 +64,7 @@ describeIf(process.platform !== 'win32')('json', () => {
},
rootTypeName: 'mutation',
rootField: 'createOneUser',
extensions: [],
})
document.validate(undefined, false, 'user', 'colorless')
expect(String(document)).toMatchSnapshot()
Expand All @@ -83,6 +84,7 @@ describeIf(process.platform !== 'win32')('json', () => {
},
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
document.validate(undefined, false, 'user', 'colorless')
expect(String(document)).toMatchSnapshot()
Expand All @@ -100,6 +102,7 @@ describeIf(process.platform !== 'win32')('json', () => {
},
rootTypeName: 'query',
rootField: 'findManyOptionalUser',
extensions: [],
})
document.validate(undefined, false, 'user', 'colorless')
expect(String(document)).toMatchSnapshot()
Expand All @@ -117,6 +120,7 @@ describeIf(process.platform !== 'win32')('json', () => {
},
rootTypeName: 'query',
rootField: 'findManyOptionalUser',
extensions: [],
})
document.validate(undefined, false, 'user', 'colorless')
expect(String(document)).toMatchSnapshot()
Expand All @@ -134,6 +138,7 @@ describeIf(process.platform !== 'win32')('json', () => {
},
rootTypeName: 'query',
rootField: 'findManyOptionalUser',
extensions: [],
})
document.validate(undefined, false, 'user', 'colorless')
expect(String(document)).toMatchSnapshot()
Expand All @@ -151,6 +156,7 @@ describeIf(process.platform !== 'win32')('json', () => {
},
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
expect(() => document.validate(undefined, false, 'user', 'colorless')).toThrowErrorMatchingSnapshot()
})
Expand All @@ -162,6 +168,7 @@ describeIf(process.platform !== 'win32')('json', () => {
select,
rootTypeName: 'mutation',
rootField: 'updateOneUser',
extensions: [],
})
return String(transformDocument(document))
}
Expand Down
Expand Up @@ -372,6 +372,7 @@ function getTransformedDocument(select) {
select,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
return String(transformDocument(document))
}
9 changes: 9 additions & 0 deletions packages/client/src/__tests__/noArgs.test.ts
Expand Up @@ -14,6 +14,7 @@ describe('no args', () => {
select: undefined,
rootTypeName: 'query',
rootField: 'findUniqueUser',
extensions: [],
})
expect(() => document.validate(undefined, false, 'user', 'colorless')).toThrowErrorMatchingSnapshot()
})
Expand All @@ -24,6 +25,7 @@ describe('no args', () => {
select: undefined,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
document.validate(undefined, false, 'user', 'colorless')
expect(String(document)).toMatchInlineSnapshot(`
Expand All @@ -48,6 +50,7 @@ describe('no args', () => {
select,
rootTypeName: 'query',
rootField: 'findManyUser',
extensions: [],
})
expect(() => document.validate(select, false, 'user', 'colorless')).toThrowErrorMatchingSnapshot()
expect(String(document)).toMatchInlineSnapshot(`
Expand All @@ -69,6 +72,7 @@ describe('no args', () => {
select: undefined,
rootTypeName: 'mutation',
rootField: 'createOneUser',
extensions: [],
})
expect(() => document.validate(undefined, false, 'user', 'colorless')).toThrowErrorMatchingSnapshot()
})
Expand All @@ -78,6 +82,7 @@ describe('no args', () => {
select: undefined,
rootTypeName: 'mutation',
rootField: 'deleteManyUser',
extensions: [],
})
document.validate(undefined, false, 'user', 'colorless')
expect(String(document)).toMatchInlineSnapshot(`
Expand All @@ -94,6 +99,7 @@ describe('no args', () => {
select: undefined,
rootTypeName: 'mutation',
rootField: 'deleteOneUser',
extensions: [],
})
expect(() => document.validate(undefined, false, 'user', 'colorless')).toThrowErrorMatchingSnapshot()
})
Expand All @@ -103,6 +109,7 @@ describe('no args', () => {
select: undefined,
rootTypeName: 'mutation',
rootField: 'updateManyUser',
extensions: [],
})
expect(() => document.validate(undefined, false, 'user', 'colorless')).toThrowErrorMatchingSnapshot()
})
Expand All @@ -112,6 +119,7 @@ describe('no args', () => {
select: undefined,
rootTypeName: 'mutation',
rootField: 'upsertOneUser',
extensions: [],
})
expect(() => document.validate(undefined, false, 'user', 'colorless')).toThrowErrorMatchingSnapshot()
})
Expand All @@ -136,6 +144,7 @@ describe('no args', () => {
},
rootTypeName: 'mutation',
rootField: 'createOneUser',
extensions: [],
})
expect(() => document.validate(undefined, false, 'user', 'colorless')).toThrowErrorMatchingSnapshot()
})
Expand Down

0 comments on commit 48bf0af

Please sign in to comment.