test: improve connection plugin tests
This deals with spec ambiguity raised in #490
This commit is contained in:
parent
2edfcfa629
commit
76e6eff6a0
|
@ -1,6 +1,6 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`connectionPlugin basic behavior can define custom resolve 1`] = `
|
||||
exports[`basic behavior can define custom resolve 1`] = `
|
||||
Object {
|
||||
"data": Object {
|
||||
"users": Object {
|
||||
|
@ -29,7 +29,7 @@ Object {
|
|||
}
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin basic behavior can define custom resolve, supplying nodes directly 1`] = `
|
||||
exports[`basic behavior can define custom resolve, supplying nodes directly 1`] = `
|
||||
Object {
|
||||
"data": Object {
|
||||
"users": Object {
|
||||
|
@ -66,7 +66,7 @@ Object {
|
|||
}
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin basic behavior can define custom resolve, which will derive nodes if includeNodesField is true 1`] = `
|
||||
exports[`basic behavior can define custom resolve, which will derive nodes if includeNodesField is true 1`] = `
|
||||
Object {
|
||||
"data": Object {
|
||||
"users": Object {
|
||||
|
@ -103,7 +103,7 @@ Object {
|
|||
}
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin basic behavior resolves any promises in nodes 1`] = `
|
||||
exports[`basic behavior resolves any promises in nodes 1`] = `
|
||||
Object {
|
||||
"data": Object {
|
||||
"users": Object {
|
||||
|
@ -180,7 +180,7 @@ Object {
|
|||
}
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin basic behavior should adhere to the Relay spec 1`] = `
|
||||
exports[`basic behavior should adhere to the Relay spec 1`] = `
|
||||
"type UserConnection {
|
||||
\\"\\"\\"
|
||||
https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types
|
||||
|
@ -194,7 +194,7 @@ exports[`connectionPlugin basic behavior should adhere to the Relay spec 1`] = `
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin basic behavior should adhere to the Relay spec 2`] = `
|
||||
exports[`basic behavior should adhere to the Relay spec 2`] = `
|
||||
"type UserEdge {
|
||||
\\"\\"\\"https://facebook.github.io/relay/graphql/connections.htm#sec-Cursor\\"\\"\\"
|
||||
cursor: String!
|
||||
|
@ -204,7 +204,7 @@ exports[`connectionPlugin basic behavior should adhere to the Relay spec 2`] = `
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin basic behavior should adhere to the Relay spec 3`] = `
|
||||
exports[`basic behavior should adhere to the Relay spec 3`] = `
|
||||
"\\"\\"\\"
|
||||
PageInfo cursor, as defined in https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo
|
||||
\\"\\"\\"
|
||||
|
@ -231,7 +231,7 @@ type PageInfo {
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin field level configuration can configure the connection per-instance 1`] = `
|
||||
exports[`field level configuration can configure the connection per-instance 1`] = `
|
||||
"type QueryUsers_Connection {
|
||||
\\"\\"\\"
|
||||
https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types
|
||||
|
@ -246,7 +246,7 @@ exports[`connectionPlugin field level configuration can configure the connection
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin field level configuration can configure the edge per-instance 1`] = `
|
||||
exports[`field level configuration can configure the edge per-instance 1`] = `
|
||||
"type QueryUsers_Connection {
|
||||
\\"\\"\\"
|
||||
https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types
|
||||
|
@ -260,7 +260,7 @@ exports[`connectionPlugin field level configuration can configure the edge per-i
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin field level configuration can configure the edge per-instance 2`] = `
|
||||
exports[`field level configuration can configure the edge per-instance 2`] = `
|
||||
"type QueryUsers_Edge {
|
||||
\\"\\"\\"https://facebook.github.io/relay/graphql/connections.htm#sec-Cursor\\"\\"\\"
|
||||
cursor: String!
|
||||
|
@ -271,7 +271,7 @@ exports[`connectionPlugin field level configuration can configure the edge per-i
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin field level configuration can define a schema with multiple plugins, and separate them by typePrefix 1`] = `
|
||||
exports[`field level configuration can define a schema with multiple plugins, and separate them by typePrefix 1`] = `
|
||||
"type Query {
|
||||
users(
|
||||
\\"\\"\\"Returns the first n elements from the list.\\"\\"\\"
|
||||
|
@ -375,7 +375,7 @@ type User {
|
|||
"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin field level configuration can define additional args for the connection 1`] = `
|
||||
exports[`field level configuration can define additional args for the connection 1`] = `
|
||||
"type Query {
|
||||
users(
|
||||
\\"\\"\\"This should be included\\"\\"\\"
|
||||
|
@ -396,7 +396,7 @@ exports[`connectionPlugin field level configuration can define additional args f
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin field level configuration can inherit the additional args from the main config 1`] = `
|
||||
exports[`field level configuration can inherit the additional args from the main config 1`] = `
|
||||
"type Query {
|
||||
users(
|
||||
\\"\\"\\"This should be included\\"\\"\\"
|
||||
|
@ -420,7 +420,7 @@ exports[`connectionPlugin field level configuration can inherit the additional a
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration allows disabling backward pagination 1`] = `
|
||||
exports[`global plugin configuration allows disabling backward pagination 1`] = `
|
||||
"type Query {
|
||||
users(
|
||||
\\"\\"\\"Returns the first n elements from the list.\\"\\"\\"
|
||||
|
@ -432,7 +432,7 @@ exports[`connectionPlugin global plugin configuration allows disabling backward
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration allows disabling backward pagination w/ strictArgs: false to make \`first\` nullable 1`] = `
|
||||
exports[`global plugin configuration allows disabling backward pagination w/ strictArgs: false to make \`first\` nullable 1`] = `
|
||||
"type Query {
|
||||
users(
|
||||
\\"\\"\\"Returns the first n elements from the list.\\"\\"\\"
|
||||
|
@ -444,7 +444,7 @@ exports[`connectionPlugin global plugin configuration allows disabling backward
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration allows disabling forward pagination 1`] = `
|
||||
exports[`global plugin configuration allows disabling forward pagination 1`] = `
|
||||
"type Query {
|
||||
users(
|
||||
\\"\\"\\"Returns the last n elements from the list.\\"\\"\\"
|
||||
|
@ -456,7 +456,7 @@ exports[`connectionPlugin global plugin configuration allows disabling forward p
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration allows disabling forward pagination w/ strictArgs:false to make \`last\` nullable 1`] = `
|
||||
exports[`global plugin configuration allows disabling forward pagination w/ strictArgs:false to make \`last\` nullable 1`] = `
|
||||
"type Query {
|
||||
users(
|
||||
\\"\\"\\"Returns the last n elements from the list.\\"\\"\\"
|
||||
|
@ -468,7 +468,7 @@ exports[`connectionPlugin global plugin configuration allows disabling forward p
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration can configure additional fields for the connection globally 1`] = `
|
||||
exports[`global plugin configuration can configure additional fields for the connection globally 1`] = `
|
||||
"type UserConnection {
|
||||
\\"\\"\\"
|
||||
https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types
|
||||
|
@ -483,7 +483,7 @@ exports[`connectionPlugin global plugin configuration can configure additional f
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration can configure additional fields for the edge globally 1`] = `
|
||||
exports[`global plugin configuration can configure additional fields for the edge globally 1`] = `
|
||||
"type UserEdge {
|
||||
\\"\\"\\"https://facebook.github.io/relay/graphql/connections.htm#sec-Cursor\\"\\"\\"
|
||||
cursor: String!
|
||||
|
@ -494,7 +494,7 @@ exports[`connectionPlugin global plugin configuration can configure additional f
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration can define additional args for all connections 1`] = `
|
||||
exports[`global plugin configuration can define additional args for all connections 1`] = `
|
||||
"type Query {
|
||||
users(
|
||||
\\"\\"\\"This should be included\\"\\"\\"
|
||||
|
@ -515,15 +515,15 @@ exports[`connectionPlugin global plugin configuration can define additional args
|
|||
}"
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration can include a "nodes" field, with an array of nodes 1`] = `"UserConnection"`;
|
||||
exports[`global plugin configuration can include a "nodes" field, with an array of nodes 1`] = `"UserConnection"`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration logs error if the extendConnection resolver is not specified 1`] = `
|
||||
exports[`global plugin configuration logs error if the extendConnection resolver is not specified 1`] = `
|
||||
Array [
|
||||
[Error: Nexus Connection Plugin: Missing totalCount resolver property for Query.users],
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`connectionPlugin global plugin configuration logs error if the extendEdge resolver is not specified 1`] = `
|
||||
exports[`global plugin configuration logs error if the extendEdge resolver is not specified 1`] = `
|
||||
Array [
|
||||
[Error: Nexus Connection Plugin: Missing edgeFields.totalCount resolver property for Query.users],
|
||||
]
|
||||
|
|
|
@ -38,14 +38,12 @@ const UsersFieldBody = `
|
|||
}
|
||||
`
|
||||
|
||||
const UsersFieldLast = parse(`query UsersFieldLast($last: Int!) { users(last: $last) { ${UsersFieldBody} } }`)
|
||||
const UsersFieldLastBefore = parse(
|
||||
const UsersLast = parse(`query UsersFieldLast($last: Int!) { users(last: $last) { ${UsersFieldBody} } }`)
|
||||
const UsersLastBefore = parse(
|
||||
`query UsersFieldLastBefore($last: Int!, $before: String!) { users(last: $last, before: $before) { ${UsersFieldBody} } }`
|
||||
)
|
||||
const UsersFieldFirst = parse(
|
||||
`query UsersFieldFirst($first: Int!) { users(first: $first) { ${UsersFieldBody} } }`
|
||||
)
|
||||
const UsersFieldFirstAfter = parse(
|
||||
const UsersFirst = parse(`query UsersFieldFirst($first: Int!) { users(first: $first) { ${UsersFieldBody} } }`)
|
||||
const UsersFirstAfter = parse(
|
||||
`query UsersFieldFirstAfter($first: Int!, $after: String!) { users(first: $first, after: $after) { ${UsersFieldBody} } }`
|
||||
)
|
||||
|
||||
|
@ -59,9 +57,9 @@ const customResolveFn: GraphQLFieldResolver<any, any> = (root: any, args: any) =
|
|||
return connectionFromArray(userNodes, args)
|
||||
}
|
||||
|
||||
const testConnectionSchema = (
|
||||
pluginConfig: ConnectionPluginConfig,
|
||||
connectionFieldProps: Omit<ConnectionFieldConfig<any, any>, 'type'> = {}
|
||||
const makeTestSchema = (
|
||||
pluginConfig: ConnectionPluginConfig = {},
|
||||
fieldConfig: Omit<ConnectionFieldConfig<any, any>, 'type'> = {}
|
||||
) =>
|
||||
makeSchema({
|
||||
outputs: false,
|
||||
|
@ -76,7 +74,7 @@ const testConnectionSchema = (
|
|||
nodes(root: any, args: any, ctx: any, info: any) {
|
||||
return userNodes
|
||||
},
|
||||
...connectionFieldProps,
|
||||
...fieldConfig,
|
||||
})
|
||||
},
|
||||
}),
|
||||
|
@ -88,21 +86,60 @@ const testConnectionSchema = (
|
|||
},
|
||||
})
|
||||
|
||||
describe('connectionPlugin', () => {
|
||||
beforeEach(() => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks()
|
||||
})
|
||||
})
|
||||
|
||||
describe('basic behavior', () => {
|
||||
function toBase64(x: string): string {
|
||||
return new Buffer(x).toString('base64')
|
||||
}
|
||||
|
||||
function dump(x: any) {
|
||||
console.log(require('util').inspect(x, { depth: null }))
|
||||
}
|
||||
|
||||
describe('defaults', () => {
|
||||
it('hasPreviousPage when paginating backwards assumes that node count equal to page size means there is another page to visit backward', async () => {
|
||||
const schema = makeTestSchema(
|
||||
{},
|
||||
{
|
||||
nodes(root, args, ctx) {
|
||||
return userNodes.slice(0, 5)
|
||||
},
|
||||
}
|
||||
)
|
||||
const result = await executeOk({
|
||||
schema,
|
||||
document: UsersLastBefore,
|
||||
variableValues: {
|
||||
last: 5,
|
||||
before: 'whatever',
|
||||
},
|
||||
})
|
||||
expect(result.data?.users.pageInfo.hasPreviousPage).toEqual(true)
|
||||
})
|
||||
it('should provide forward pagination defaults', async () => {
|
||||
const schema = makeTestSchema({})
|
||||
const nodes = await execute({
|
||||
schema,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 1 },
|
||||
})
|
||||
expect(nodes.data?.users.edges).toEqual([{ cursor: 'Y3Vyc29yOjA=', node: { id: 'User:1' } }])
|
||||
expect(Buffer.from(nodes.data?.users.edges[0].cursor, 'base64').toString('utf8')).toEqual('cursor:0')
|
||||
})
|
||||
})
|
||||
|
||||
describe('basic behavior', () => {
|
||||
it('should adhere to the Relay spec', () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const schema = makeTestSchema({})
|
||||
expect(printType(schema.getType('UserConnection')!)).toMatchSnapshot()
|
||||
expect(printType(schema.getType('UserEdge')!)).toMatchSnapshot()
|
||||
expect(printType(schema.getType('PageInfo')!)).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('resolves string value', () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{},
|
||||
{
|
||||
// @ts-ignore
|
||||
|
@ -114,19 +151,8 @@ describe('connectionPlugin', () => {
|
|||
expect(schema.getType('PageInfo')).not.toBeUndefined()
|
||||
})
|
||||
|
||||
it('should provide forward pagination defaults', async () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const nodes = await execute({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
variableValues: { first: 1 },
|
||||
})
|
||||
expect(nodes.data?.users.edges).toEqual([{ cursor: 'Y3Vyc29yOjA=', node: { id: 'User:1' } }])
|
||||
expect(Buffer.from(nodes.data?.users.edges[0].cursor, 'base64').toString('utf8')).toEqual('cursor:0')
|
||||
})
|
||||
|
||||
it('should continue forward pagination from the after index', async () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{},
|
||||
{
|
||||
nodes(root: any, args: any) {
|
||||
|
@ -137,20 +163,20 @@ describe('connectionPlugin', () => {
|
|||
)
|
||||
const nodes = await executeOk({
|
||||
schema,
|
||||
document: UsersFieldFirstAfter,
|
||||
document: UsersFirstAfter,
|
||||
variableValues: { first: 1, after: 'Y3Vyc29yOjA=' },
|
||||
})
|
||||
expect(Buffer.from(nodes.data?.users.edges[0].cursor, 'base64').toString('utf8')).toEqual('cursor:1')
|
||||
})
|
||||
|
||||
it('can paginate backward from a before cursor', async () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
encodeCursor: (str) => str,
|
||||
decodeCursor: (str) => str,
|
||||
})
|
||||
const first = await executeOk({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 9 },
|
||||
})
|
||||
expect(first.data?.users.pageInfo).toEqual({
|
||||
|
@ -161,7 +187,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
const lastNodes = await executeOk({
|
||||
schema,
|
||||
document: UsersFieldLastBefore,
|
||||
document: UsersLastBefore,
|
||||
variableValues: {
|
||||
last: 3,
|
||||
before: first.data?.users.pageInfo.endCursor,
|
||||
|
@ -177,7 +203,7 @@ describe('connectionPlugin', () => {
|
|||
|
||||
it('can paginate backward without a before with a custom cursorFromNodes', async () => {
|
||||
const getTotalCount = async () => Promise.resolve(100)
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
encodeCursor: (str) => str,
|
||||
decodeCursor: (str) => str,
|
||||
cursorFromNode: async (node, args, ctx, info, { index, nodes }) => {
|
||||
|
@ -193,7 +219,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
const lastNodes = await executeOk({
|
||||
schema,
|
||||
document: UsersFieldLast,
|
||||
document: UsersLast,
|
||||
variableValues: {
|
||||
last: 3,
|
||||
},
|
||||
|
@ -207,13 +233,13 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('cannot paginate backward without a before cursor or a custom cursorFromNodes', async () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
encodeCursor: (str) => str,
|
||||
decodeCursor: (str) => str,
|
||||
})
|
||||
const lastNodes = await execute({
|
||||
schema,
|
||||
document: UsersFieldLast,
|
||||
document: UsersLast,
|
||||
variableValues: {
|
||||
last: 3,
|
||||
},
|
||||
|
@ -224,10 +250,10 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('should resolve pageInfo with basics', async () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const schema = makeTestSchema({})
|
||||
const lastNodes = await executeOk({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 10 },
|
||||
})
|
||||
expect(lastNodes.data?.users.pageInfo).toEqual({
|
||||
|
@ -239,19 +265,19 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('should resolve nodes & edges at the same time', async () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
includeNodesField: true,
|
||||
})
|
||||
const lastNodes = await executeOk({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 10 },
|
||||
})
|
||||
expect(lastNodes.data?.users.nodes).toEqual(lastNodes.data?.users.edges.map((e: any) => e.node))
|
||||
})
|
||||
|
||||
it('can define custom resolve', async () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{},
|
||||
{
|
||||
nodes: undefined,
|
||||
|
@ -260,14 +286,14 @@ describe('connectionPlugin', () => {
|
|||
)
|
||||
const lastNodes = await execute({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 2 },
|
||||
})
|
||||
expect(lastNodes).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('can define custom resolve, which will derive nodes if includeNodesField is true', async () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
includeNodesField: true,
|
||||
},
|
||||
|
@ -278,14 +304,14 @@ describe('connectionPlugin', () => {
|
|||
)
|
||||
const lastNodes = await execute({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 2 },
|
||||
})
|
||||
expect(lastNodes).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('can define custom resolve, supplying nodes directly', async () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
includeNodesField: true,
|
||||
},
|
||||
|
@ -302,14 +328,14 @@ describe('connectionPlugin', () => {
|
|||
)
|
||||
const lastNodes = await executeOk({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 2 },
|
||||
})
|
||||
expect(lastNodes).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('default arg validation: throws if no connection are provided', async () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const schema = makeTestSchema({})
|
||||
const result = await execute({
|
||||
schema,
|
||||
document: parse(`{ users { edges { cursor } } }`),
|
||||
|
@ -322,10 +348,10 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('default arg validation: allows first to be zero', async () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const schema = makeTestSchema({})
|
||||
const result = await execute({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 0 },
|
||||
})
|
||||
expect(result).toEqual({
|
||||
|
@ -344,10 +370,10 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('default arg validation: allows last to be zero', async () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const schema = makeTestSchema({})
|
||||
const result = await execute({
|
||||
schema,
|
||||
document: UsersFieldLast,
|
||||
document: UsersLast,
|
||||
variableValues: { last: 0 },
|
||||
})
|
||||
expect(result).toEqual({
|
||||
|
@ -366,7 +392,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('default arg validation: throws if both first & last are provided', async () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const schema = makeTestSchema({})
|
||||
const result = await execute({
|
||||
schema,
|
||||
document: parse(`{ users(first: 2, last: 1) { edges { cursor } } }`),
|
||||
|
@ -375,15 +401,13 @@ describe('connectionPlugin', () => {
|
|||
expect(result).toEqual({
|
||||
data: { users: null },
|
||||
errors: [
|
||||
new GraphQLError(
|
||||
'The Query.users connection field requires a "first" or "last" argument, not both'
|
||||
),
|
||||
new GraphQLError('The Query.users connection field requires a "first" or "last" argument, not both'),
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it('default arg validation: throws if first & before are mixed', async () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const schema = makeTestSchema({})
|
||||
const result = await execute({
|
||||
schema,
|
||||
document: parse(`{ users(first: 1, before: "FAKE") { edges { cursor } } }`),
|
||||
|
@ -392,15 +416,13 @@ describe('connectionPlugin', () => {
|
|||
expect(result).toEqual({
|
||||
data: { users: null },
|
||||
errors: [
|
||||
new GraphQLError(
|
||||
'The Query.users connection field does not allow a "before" argument with "first"'
|
||||
),
|
||||
new GraphQLError('The Query.users connection field does not allow a "before" argument with "first"'),
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it('default arg validation: throws if last & after are mixed', async () => {
|
||||
const schema = testConnectionSchema({})
|
||||
const schema = makeTestSchema({})
|
||||
const result = await execute({
|
||||
schema,
|
||||
document: parse(`{ users(last: 2, after: "FAKE") { edges { cursor } } }`),
|
||||
|
@ -416,7 +438,7 @@ describe('connectionPlugin', () => {
|
|||
|
||||
it('returns null and logs an error if the nodes resolve is missing', async () => {
|
||||
const consoleError = jest.spyOn(console, 'error').mockImplementation()
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
includeNodesField: true,
|
||||
},
|
||||
|
@ -426,7 +448,7 @@ describe('connectionPlugin', () => {
|
|||
)
|
||||
const lastNodes = await execute({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 2 },
|
||||
})
|
||||
expect(lastNodes.data?.users).toEqual(null)
|
||||
|
@ -438,7 +460,7 @@ describe('connectionPlugin', () => {
|
|||
|
||||
it('returns empty arrays, but warns if the nodes returns null', async () => {
|
||||
const consoleWarn = jest.spyOn(console, 'warn').mockImplementation()
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
includeNodesField: true,
|
||||
},
|
||||
|
@ -450,7 +472,7 @@ describe('connectionPlugin', () => {
|
|||
)
|
||||
const lastNodes = await execute({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 2 },
|
||||
})
|
||||
expect(lastNodes.data?.users).toEqual({
|
||||
|
@ -470,7 +492,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('resolves any promises in nodes', async () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{},
|
||||
{
|
||||
nodes() {
|
||||
|
@ -480,14 +502,14 @@ describe('connectionPlugin', () => {
|
|||
)
|
||||
const result = await execute({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 10 },
|
||||
})
|
||||
expect(result).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('returns list as length of nodes if result is smaller than requested', async () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
includeNodesField: true,
|
||||
},
|
||||
|
@ -499,30 +521,30 @@ describe('connectionPlugin', () => {
|
|||
)
|
||||
const result = await executeOk({
|
||||
schema,
|
||||
document: UsersFieldFirst,
|
||||
document: UsersFirst,
|
||||
variableValues: { first: 1000 },
|
||||
})
|
||||
expect(result.data?.users.nodes.length).toEqual(10)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('global plugin configuration', () => {
|
||||
describe('global plugin configuration', () => {
|
||||
it('allows disabling forward pagination', () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
disableForwardPagination: true,
|
||||
})
|
||||
expect(printType(schema.getQueryType()!)).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('allows disabling backward pagination', () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
disableBackwardPagination: true,
|
||||
})
|
||||
expect(printType(schema.getQueryType()!)).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('allows disabling forward pagination w/ strictArgs:false to make `last` nullable', () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
disableForwardPagination: true,
|
||||
strictArgs: false,
|
||||
})
|
||||
|
@ -530,7 +552,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('allows disabling backward pagination w/ strictArgs: false to make `first` nullable', () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
disableBackwardPagination: true,
|
||||
strictArgs: false,
|
||||
})
|
||||
|
@ -538,7 +560,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('can configure additional fields for the connection globally', () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
extendConnection: {
|
||||
totalCount: {
|
||||
|
@ -556,7 +578,7 @@ describe('connectionPlugin', () => {
|
|||
|
||||
it('logs error if the extendConnection resolver is not specified', () => {
|
||||
const spy = jest.spyOn(console, 'error').mockImplementation()
|
||||
testConnectionSchema({
|
||||
makeTestSchema({
|
||||
extendConnection: {
|
||||
totalCount: {
|
||||
type: 'Int',
|
||||
|
@ -569,7 +591,7 @@ describe('connectionPlugin', () => {
|
|||
|
||||
it('logs error if the extendEdge resolver is not specified', () => {
|
||||
const spy = jest.spyOn(console, 'error').mockImplementation()
|
||||
testConnectionSchema({
|
||||
makeTestSchema({
|
||||
extendEdge: {
|
||||
totalCount: {
|
||||
type: 'Int',
|
||||
|
@ -581,7 +603,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('can configure additional fields for the edge globally', () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
extendEdge: {
|
||||
createdAt: {
|
||||
|
@ -600,14 +622,14 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('can include a "nodes" field, with an array of nodes', () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
includeNodesField: true,
|
||||
})
|
||||
expect(schema.getType('UserConnection')!).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it('can define additional args for all connections', () => {
|
||||
const schema = testConnectionSchema({
|
||||
const schema = makeTestSchema({
|
||||
additionalArgs: {
|
||||
order: arg({
|
||||
type: 'String',
|
||||
|
@ -618,11 +640,11 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
expect(printType(schema.getQueryType()!)).toMatchSnapshot()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('field level configuration', () => {
|
||||
describe('field level configuration', () => {
|
||||
it('can configure the connection per-instance', () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{},
|
||||
{
|
||||
extendConnection(t) {
|
||||
|
@ -635,7 +657,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('can configure the edge per-instance', () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{},
|
||||
{
|
||||
extendEdge(t) {
|
||||
|
@ -650,7 +672,7 @@ describe('connectionPlugin', () => {
|
|||
it('can modify the behavior of cursorFromNode ', () => {})
|
||||
|
||||
it('can define additional args for the connection', () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
additionalArgs: {
|
||||
order: arg({
|
||||
|
@ -673,7 +695,7 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
|
||||
it('can inherit the additional args from the main config', () => {
|
||||
const schema = testConnectionSchema(
|
||||
const schema = makeTestSchema(
|
||||
{
|
||||
additionalArgs: {
|
||||
order: arg({
|
||||
|
@ -738,5 +760,4 @@ describe('connectionPlugin', () => {
|
|||
})
|
||||
expect(printSchema(schema)).toMatchSnapshot()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue