Skip to content

Commit ee3781d

Browse files
authored
improve: prefix data term in return mode (#966)
1 parent f72ceb0 commit ee3781d

File tree

9 files changed

+172
-177
lines changed

9 files changed

+172
-177
lines changed

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@
112112
"@types/express": "^4.17.21",
113113
"@types/json-bigint": "^1.0.4",
114114
"@types/node": "^20.14.9",
115-
"@typescript-eslint/eslint-plugin": "^7.14.1",
116-
"@typescript-eslint/parser": "^7.14.1",
115+
"@typescript-eslint/eslint-plugin": "^7.15.0",
116+
"@typescript-eslint/parser": "^7.15.0",
117117
"apollo-server-express": "^3.13.0",
118118
"body-parser": "^1.20.2",
119119
"doctoc": "^2.2.1",
@@ -123,7 +123,7 @@
123123
"eslint-plugin-deprecation": "^3.0.0",
124124
"eslint-plugin-only-warn": "^1.1.0",
125125
"eslint-plugin-prefer-arrow": "^1.2.3",
126-
"eslint-plugin-simple-import-sort": "^12.1.0",
126+
"eslint-plugin-simple-import-sort": "^12.1.1",
127127
"eslint-plugin-tsdoc": "^0.3.0",
128128
"eslint-typescript": "^1.1.0",
129129
"express": "^4.19.2",
@@ -132,14 +132,14 @@
132132
"graphql-scalars": "^1.23.0",
133133
"graphql-tag": "^2.12.6",
134134
"graphql-upload-minimal": "^1.6.1",
135-
"graphql-yoga": "^5.5.0",
135+
"graphql-yoga": "^5.6.0",
136136
"jsdom": "^24.1.0",
137137
"json-bigint": "^1.0.0",
138138
"publint": "^0.2.8",
139-
"tsx": "^4.16.0",
140-
"type-fest": "^4.20.1",
141-
"typescript": "^5.5.2",
142-
"typescript-eslint": "^7.14.1",
139+
"tsx": "^4.16.2",
140+
"type-fest": "^4.21.0",
141+
"typescript": "^5.5.3",
142+
"typescript-eslint": "^7.15.0",
143143
"vitest": "^1.6.0"
144144
}
145145
}

pnpm-lock.yaml

Lines changed: 139 additions & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/layers/3_SelectionSet/encode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export const resolveObjectLikeFieldValue = (
200200
* Inject __typename field for result fields that are missing it.
201201
*/
202202
// dprint-ignore
203-
if (rootTypeName && context.config.returnMode === `successData` && context.schemaIndex.error.rootResultFields[rootTypeName][fieldName.actual]) {
203+
if (rootTypeName && context.config.returnMode === `dataSuccess` && context.schemaIndex.error.rootResultFields[rootTypeName][fieldName.actual]) {
204204
(ss as Record<string, boolean>)[`__typename`] = true
205205
}
206206
return `${toGraphQLFieldName(fieldName)} ${resolveFieldValue(context, schemaField, ss)}`

src/layers/5_client/Config.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { SelectionSet } from '../3_SelectionSet/__.js'
88
export type ReturnModeType =
99
| ReturnModeTypeGraphQL
1010
| ReturnModeTypeGraphQLSuccess
11-
| ReturnModeTypeSuccessData
11+
| ReturnModeTypeDataSuccess
1212
| ReturnModeTypeData
1313
| ReturnModeTypeDataAndErrors
1414

@@ -26,8 +26,7 @@ export type ReturnModeTypeData = 'data'
2626

2727
export type ReturnModeTypeDataAndErrors = 'dataAndErrors'
2828

29-
// todo rename to dataSuccess
30-
export type ReturnModeTypeSuccessData = 'successData'
29+
export type ReturnModeTypeDataSuccess = 'dataSuccess'
3130

3231
export type OptionsInput = {
3332
returnMode: ReturnModeType | undefined
@@ -50,14 +49,14 @@ export type ApplyInputDefaults<Input extends OptionsInput> = {
5049
export type ReturnModeRootType<$Config extends Config, $Index extends Schema.Index, $Data extends object> =
5150
$Config['returnMode'] extends 'graphql' ? ExecutionResult<$Data> :
5251
$Config['returnMode'] extends 'data' ? $Data :
53-
$Config['returnMode'] extends 'successData' ? { [$Key in keyof $Data]: ExcludeSchemaErrors<$Index, $Data[$Key]> } :
52+
$Config['returnMode'] extends 'dataSuccess' ? { [$Key in keyof $Data]: ExcludeSchemaErrors<$Index, $Data[$Key]> } :
5453
$Data | GraphQLExecutionResultError
5554

5655
// dprint-ignore
5756
export type ReturnModeRootField<$Config extends Config, $Index extends Schema.Index, $Data, $DataRaw = undefined> =
5857
$Config['returnMode'] extends 'graphql' ? ExecutionResult<$DataRaw extends undefined ? $Data : $DataRaw> :
5958
$Config['returnMode'] extends 'data' ? $Data :
60-
$Config['returnMode'] extends 'successData' ? ExcludeSchemaErrors<$Index, $Data> :
59+
$Config['returnMode'] extends 'dataSuccess' ? ExcludeSchemaErrors<$Index, $Data> :
6160
$Data | GraphQLExecutionResultError
6261

6362
export type ExcludeSchemaErrors<$Index extends Schema.Index, $Data> = Exclude<
@@ -66,12 +65,12 @@ export type ExcludeSchemaErrors<$Index extends Schema.Index, $Data> = Exclude<
6665
>
6766

6867
export type OrThrowifyConfig<$Config extends Config> = $Config['returnMode'] extends 'graphql' ? $Config
69-
: SetProperty<$Config, 'returnMode', 'successData'>
68+
: SetProperty<$Config, 'returnMode', 'dataSuccess'>
7069

7170
/**
7271
* We inject __typename select when:
7372
* 1. using schema errors
74-
* 2. using return mode successData
73+
* 2. using return mode dataSuccess
7574
*/
7675

7776
type TypenameSelection = { __typename: true }
@@ -82,7 +81,7 @@ export type CreateSelectionTypename<$Config extends Config, $Index extends Schem
8281

8382
// dprint-ignore
8483
export type IsNeedSelectionTypename<$Config extends Config, $Index extends Schema.Index> =
85-
$Config['returnMode'] extends 'successData' ? GlobalRegistry.HasSchemaErrorsViaName<$Index['name']> extends true ? true :
84+
$Config['returnMode'] extends 'dataSuccess' ? GlobalRegistry.HasSchemaErrorsViaName<$Index['name']> extends true ? true :
8685
false :
8786
false
8887

src/layers/5_client/client.extend.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@ const headers = { 'x-foo': 'bar' }
1010

1111
// todo each extension added should copy, not mutate the client
1212

13-
describe(`entrypoint request`, () => {
14-
test(`can add header to request`, async ({ fetch }) => {
13+
describe(`entrypoint pack`, () => {
14+
test(`can add header`, async ({ fetch }) => {
1515
fetch.mockImplementationOnce(async (input: Request) => {
1616
expect(input.headers.get('x-foo')).toEqual(headers['x-foo'])
1717
return createResponse({ data: { id: db.id } })
1818
})
1919
const client2 = client.use(async ({ pack }) => {
20-
// todo should be raw input types but rather resolved
21-
// todo should be URL instance?
22-
// todo these input type tests should be moved down to Anyware
23-
// expectTypeOf(exchange).toEqualTypeOf<NetworkRequestHook>()
24-
// expect(exchange.input).toEqual({ url: 'https://foo', document: `query { id \n }` })
2520
return await pack({ input: { ...pack.input, headers } })
2621
})
2722
expect(await client2.query.id()).toEqual(db.id)

src/layers/5_client/client.input.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ test(`works`, () => {
77
Graffle.create({ schema, returnMode: `graphql` })
88
Graffle.create({ schema, returnMode: `data` })
99
Graffle.create({ schema, returnMode: `dataAndErrors` })
10-
Graffle.create({ schema, returnMode: `successData` })
10+
Graffle.create({ schema, returnMode: `dataSuccess` })
1111

1212
QueryOnly.create({ schema, returnMode: `graphql` })
1313
QueryOnly.create({ schema, returnMode: `data` })
1414
QueryOnly.create({ schema, returnMode: `dataAndErrors` })
1515
// @ts-expect-error bad returnMode
16-
QueryOnly.create({ schema, name: `QueryOnly`, returnMode: `successData` })
16+
QueryOnly.create({ schema, name: `QueryOnly`, returnMode: `dataSuccess` })
1717
})

src/layers/5_client/client.returnMode.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe('data', () => {
5151
})
5252

5353
// dprint-ignore
54-
describe('successData', () => {
55-
const graffle = Graffle.create({ schema, returnMode: 'successData' })
54+
describe('dataSuccess', () => {
55+
const graffle = Graffle.create({ schema, returnMode: 'dataSuccess' })
5656
test(`document.run`, async () => {
5757
expectTypeOf(graffle.document({ x: { query: { id: true } } }).run()).resolves.toEqualTypeOf<{ id: string | null }>()
5858
})

src/layers/5_client/client.returnMode.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ describe('dataAndErrors', () => {
8282
})
8383

8484
// dprint-ignore
85-
describe('successData', () => {
86-
const graffle = Graffle.create({ schema, returnMode: 'successData' })
85+
describe('dataSuccess', () => {
86+
const graffle = Graffle.create({ schema, returnMode: 'dataSuccess' })
8787
test(`document.run`, async () => {
8888
expect(graffle.document({ x: { query: { id: true } } }).run()).resolves.toEqual({ id: db.id })
8989
})

src/layers/5_client/client.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
Config,
1919
ReturnModeType,
2020
ReturnModeTypeBase,
21-
ReturnModeTypeSuccessData,
21+
ReturnModeTypeDataSuccess,
2222
} from './Config.js'
2323
import type { DocumentFn } from './document.js'
2424
import type { GetRootTypeMethods } from './RootTypeMethods.js'
@@ -89,7 +89,7 @@ export type InputRaw = {
8989
export type InputPrefilled<$Schema extends GlobalRegistry.SchemaList> = $Schema extends any ? {
9090
returnMode?:
9191
| ReturnModeTypeBase
92-
| (GlobalRegistry.HasSchemaErrors<$Schema> extends true ? ReturnModeTypeSuccessData : never)
92+
| (GlobalRegistry.HasSchemaErrors<$Schema> extends true ? ReturnModeTypeDataSuccess : never)
9393
} & InputRaw
9494
: never
9595

@@ -217,7 +217,7 @@ export const createInternal = (
217217
const isSelectedTypeScalarOrTypeName = selectedNamedType.kind === `Scalar` || selectedNamedType.kind === `typename` // todo fix type here, its valid
218218
const isFieldHasArgs = Boolean(context.schemaIndex.Root[rootTypeName]?.fields[rootTypeFieldName]?.args)
219219
// We should only need to add __typename for result type fields, but the return handler doesn't yet know how to look beyond a plain object type so we have to add all those cases here.
220-
const needsTypenameAdded = context.config.returnMode === `successData`
220+
const needsTypenameAdded = context.config.returnMode === `dataSuccess`
221221
&& (selectedNamedType.kind === `Object` || selectedNamedType.kind === `Interface`
222222
|| selectedNamedType.kind === `Union`)
223223
const rootTypeFieldSelectionSet = isSelectedTypeScalarOrTypeName
@@ -231,7 +231,7 @@ export const createInternal = (
231231
} as GraphQLObjectSelection)
232232
if (result instanceof Error) return result
233233
return context.config.returnMode === `data` || context.config.returnMode === `dataAndErrors`
234-
|| context.config.returnMode === `successData`
234+
|| context.config.returnMode === `dataSuccess`
235235
// @ts-expect-error
236236
? result[rootTypeFieldName]
237237
: result
@@ -382,7 +382,7 @@ const handleReturn = (
382382
switch (context.config.returnMode) {
383383
case `graphqlSuccess`:
384384
case `dataAndErrors`:
385-
case `successData`:
385+
case `dataSuccess`:
386386
case `data`: {
387387
if (result instanceof Error || (result.errors && result.errors.length > 0)) {
388388
const error = result instanceof Error ? result : (new Errors.ContextualAggregateError(
@@ -391,14 +391,14 @@ const handleReturn = (
391391
result.errors!,
392392
))
393393
if (
394-
context.config.returnMode === `data` || context.config.returnMode === `successData`
394+
context.config.returnMode === `data` || context.config.returnMode === `dataSuccess`
395395
|| context.config.returnMode === `graphqlSuccess`
396396
) throw error
397397
return error
398398
}
399399

400400
if (isTypedContext(context)) {
401-
if (context.config.returnMode === `successData`) {
401+
if (context.config.returnMode === `dataSuccess`) {
402402
if (!isPlainObject(result.data)) throw new Error(`Expected data to be an object.`)
403403
const schemaErrors = Object.entries(result.data).map(([rootFieldName, rootFieldValue]) => {
404404
// todo this check would be nice but it doesn't account for aliases right now. To achieve this we would
@@ -444,10 +444,10 @@ const handleReturn = (
444444
}
445445

446446
const applyOrThrowToContext = <$Context extends Context>(context: $Context): $Context => {
447-
if (context.config.returnMode === `successData` || context.config.returnMode === `graphqlSuccess`) {
447+
if (context.config.returnMode === `dataSuccess` || context.config.returnMode === `graphqlSuccess`) {
448448
return context
449449
}
450-
const newMode = context.config.returnMode === `graphql` ? `graphqlSuccess` : `successData`
450+
const newMode = context.config.returnMode === `graphql` ? `graphqlSuccess` : `dataSuccess`
451451
return updateContextConfig(context, { returnMode: newMode })
452452
}
453453

0 commit comments

Comments
 (0)