-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Don't force async if connection resolvers are sync (#707)
- Loading branch information
Showing
8 changed files
with
191 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`field level configuration does not pay the cost of async unless there is an async call 1`] = ` | ||
Object { | ||
"data": Object { | ||
"users": Object { | ||
"edges": Array [ | ||
Object { | ||
"cursor": "Y3Vyc29yOjA=", | ||
"node": Object { | ||
"id": "User:1", | ||
}, | ||
}, | ||
], | ||
"pageInfo": Object { | ||
"endCursor": "Y3Vyc29yOjA=", | ||
"hasNextPage": true, | ||
"hasPreviousPage": false, | ||
"startCursor": "Y3Vyc29yOjA=", | ||
}, | ||
}, | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { executeSync, parse } from 'graphql' | ||
import { connectionPlugin, makeSchema, objectType } from '../../src' | ||
import { SchemaConfig } from '../../src/core' | ||
import { ConnectionFieldConfig, ConnectionPluginConfig } from '../../src/plugins/connectionPlugin' | ||
|
||
const userNodes: { id: string; name: string }[] = [] | ||
for (let i = 0; i < 10; i++) { | ||
userNodes.push({ id: `User:${i + 1}`, name: `Test ${i + 1}` }) | ||
} | ||
|
||
const User = objectType({ | ||
name: 'User', | ||
definition(t) { | ||
t.id('id') | ||
t.string('name') | ||
}, | ||
}) | ||
|
||
const UsersFieldBody = ` | ||
nodes { id } | ||
edges { | ||
cursor | ||
node { id } | ||
} | ||
pageInfo { | ||
hasNextPage | ||
hasPreviousPage | ||
startCursor | ||
endCursor | ||
} | ||
` | ||
|
||
const UsersFirst = parse(`query UsersFieldFirst($first: Int!) { users(first: $first) { ${UsersFieldBody} } }`) | ||
|
||
const makeTestSchema = ( | ||
pluginConfig: ConnectionPluginConfig = {}, | ||
fieldConfig: Omit<ConnectionFieldConfig<any, any>, 'type'> = {}, | ||
makeSchemaConfig: Omit<SchemaConfig, 'types'> = {} | ||
) => | ||
makeSchema({ | ||
outputs: false, | ||
types: [ | ||
User, | ||
objectType({ | ||
name: 'Query', | ||
definition(t) { | ||
// @ts-ignore | ||
t.connectionField('users', { | ||
type: User, | ||
nodes(root: any, args: any, ctx: any, info: any) { | ||
return userNodes | ||
}, | ||
...fieldConfig, | ||
}) | ||
}, | ||
}), | ||
], | ||
nonNullDefaults: { | ||
input: false, | ||
output: false, | ||
}, | ||
...makeSchemaConfig, | ||
plugins: [connectionPlugin(pluginConfig), ...(makeSchemaConfig.plugins ?? [])], | ||
}) | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('field level configuration', () => { | ||
it('does not pay the cost of async unless there is an async call', () => { | ||
const schema = makeTestSchema() | ||
|
||
const result = executeSync({ | ||
schema, | ||
document: UsersFirst, | ||
variableValues: { | ||
first: 1, | ||
}, | ||
}) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters