|
| 1 | +const { strictEqual, deepStrictEqual } = require('assert') |
| 2 | +const { GraphQLError } = require('graphql') |
| 3 | +const { isStatusCodeError, IMPL_TYPE_SERVER_VALIDATOR_APOLLO4 } = require('./testutils') |
| 4 | + |
| 5 | +module.exports.test = function (setup, implType) { |
| 6 | + describe('Custom format', () => { |
| 7 | + let typeDefs |
| 8 | + let request |
| 9 | + const fooFormat = (value) => { |
| 10 | + if (typeof value === 'string' && value.toLowerCase().includes('foo')) { |
| 11 | + return true |
| 12 | + } |
| 13 | + throw new GraphQLError('No foos', { |
| 14 | + extensions: { |
| 15 | + code: 'BAD_USER_INPUT', |
| 16 | + http: { |
| 17 | + status: 400 |
| 18 | + } |
| 19 | + } |
| 20 | + }) |
| 21 | + } |
| 22 | + const createBookMutation = ` |
| 23 | + mutation CreateBook($input: BookInput!) { |
| 24 | + createBook(input: $input) { |
| 25 | + title |
| 26 | + authors |
| 27 | + } |
| 28 | + } |
| 29 | + ` |
| 30 | + |
| 31 | + describe('Array input', () => { |
| 32 | + before(async () => { |
| 33 | + typeDefs = ` |
| 34 | + type Query { |
| 35 | + books: [Book!] |
| 36 | + } |
| 37 | + type Mutation { |
| 38 | + createBook(input: BookInput!): Book |
| 39 | + } |
| 40 | + type Book { |
| 41 | + title: String |
| 42 | + authors: [String!] |
| 43 | + } |
| 44 | + input BookInput { |
| 45 | + title: String! |
| 46 | + authors: [String!]! @constraint(format: "foo") |
| 47 | + } |
| 48 | + ` |
| 49 | + request = await setup({ typeDefs, pluginOptions: { formats: { foo: fooFormat } } }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should pass', async () => { |
| 53 | + const variables = { |
| 54 | + input: { |
| 55 | + title: '1984', |
| 56 | + authors: ['Foo Orwell', 'Another Foo'] |
| 57 | + } |
| 58 | + } |
| 59 | + const { body, statusCode } = await request |
| 60 | + .post('/graphql') |
| 61 | + .set('Accept', 'application/json') |
| 62 | + .send({ query: createBookMutation, variables }) |
| 63 | + |
| 64 | + strictEqual(statusCode, 200) |
| 65 | + deepStrictEqual(body, { data: { createBook: null } }) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should fail', async () => { |
| 69 | + const variables = { |
| 70 | + input: { |
| 71 | + title: 'The Metamorphosis', |
| 72 | + authors: ['Franz Kafka', 'Stanley Corngold'] |
| 73 | + } |
| 74 | + } |
| 75 | + const { body, statusCode } = await request |
| 76 | + .post('/graphql') |
| 77 | + .set('Accept', 'application/json') |
| 78 | + .send({ query: createBookMutation, variables }) |
| 79 | + |
| 80 | + isStatusCodeError(statusCode, implType) |
| 81 | + if (implType === IMPL_TYPE_SERVER_VALIDATOR_APOLLO4) { |
| 82 | + strictEqual(body.errors.length, 1) |
| 83 | + strictEqual( |
| 84 | + body.errors[0].extensions.validationErrors[0].message, |
| 85 | + 'Variable "$input" got invalid value "Franz Kafka" at "input.authors[0]". No foos' |
| 86 | + ) |
| 87 | + strictEqual( |
| 88 | + body.errors[0].extensions.validationErrors[1].message, |
| 89 | + 'Variable "$input" got invalid value "Stanley Corngold" at "input.authors[1]". No foos' |
| 90 | + ) |
| 91 | + } else { |
| 92 | + strictEqual(body.errors.length, 2) |
| 93 | + strictEqual( |
| 94 | + body.errors[0].message, |
| 95 | + 'Variable "$input" got invalid value "Franz Kafka" at "input.authors[0]". No foos' |
| 96 | + ) |
| 97 | + strictEqual( |
| 98 | + body.errors[1].message, |
| 99 | + 'Variable "$input" got invalid value "Stanley Corngold" at "input.authors[1]". No foos' |
| 100 | + ) |
| 101 | + } |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe('Scalar input', () => { |
| 106 | + before(async () => { |
| 107 | + typeDefs = ` |
| 108 | + type Query { |
| 109 | + books: [Book!] |
| 110 | + } |
| 111 | + type Mutation { |
| 112 | + createBook(input: BookInput!): Book |
| 113 | + } |
| 114 | + type Book { |
| 115 | + title: String |
| 116 | + authors: [String!] |
| 117 | + } |
| 118 | + input BookInput { |
| 119 | + title: String! @constraint(format: "foo") |
| 120 | + authors: [String!]! |
| 121 | + } |
| 122 | + ` |
| 123 | + request = await setup({ typeDefs, pluginOptions: { formats: { foo: fooFormat } } }) |
| 124 | + }) |
| 125 | + |
| 126 | + it('should pass', async () => { |
| 127 | + const variables = { |
| 128 | + input: { |
| 129 | + title: 'Foos and Bars', |
| 130 | + authors: ['Tacitus Kilgore'] |
| 131 | + } |
| 132 | + } |
| 133 | + const { body, statusCode } = await request |
| 134 | + .post('/graphql') |
| 135 | + .set('Accept', 'application/json') |
| 136 | + .send({ query: createBookMutation, variables }) |
| 137 | + |
| 138 | + strictEqual(statusCode, 200) |
| 139 | + deepStrictEqual(body, { data: { createBook: null } }) |
| 140 | + }) |
| 141 | + |
| 142 | + it('should fail', async () => { |
| 143 | + const variables = { |
| 144 | + input: { |
| 145 | + title: '1984', |
| 146 | + authors: ['George Orwell'] |
| 147 | + } |
| 148 | + } |
| 149 | + const { body, statusCode } = await request |
| 150 | + .post('/graphql') |
| 151 | + .set('Accept', 'application/json') |
| 152 | + .send({ query: createBookMutation, variables }) |
| 153 | + |
| 154 | + isStatusCodeError(statusCode, implType) |
| 155 | + strictEqual(body.errors.length, 1) |
| 156 | + strictEqual( |
| 157 | + body.errors[0].message, |
| 158 | + 'Variable "$input" got invalid value "1984" at "input.title". No foos' |
| 159 | + ) |
| 160 | + }) |
| 161 | + }) |
| 162 | + }) |
| 163 | +} |
0 commit comments