|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const http = require('http'); |
| 4 | +const express = require('express'); |
| 5 | +const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); |
| 6 | +require('./helper'); |
| 7 | +const { ParseGraphQLServer } = require('../lib/GraphQL/ParseGraphQLServer'); |
| 8 | + |
| 9 | +describe('graphql query complexity', () => { |
| 10 | + let httpServer; |
| 11 | + let graphQLServer; |
| 12 | + const headers = { |
| 13 | + 'X-Parse-Application-Id': 'test', |
| 14 | + 'X-Parse-Javascript-Key': 'test', |
| 15 | + 'Content-Type': 'application/json', |
| 16 | + }; |
| 17 | + |
| 18 | + async function setupGraphQL(serverOptions = {}) { |
| 19 | + if (httpServer) { |
| 20 | + await new Promise(resolve => httpServer.close(resolve)); |
| 21 | + } |
| 22 | + const server = await reconfigureServer(serverOptions); |
| 23 | + const expressApp = express(); |
| 24 | + httpServer = http.createServer(expressApp); |
| 25 | + expressApp.use('/parse', server.app); |
| 26 | + graphQLServer = new ParseGraphQLServer(server, { |
| 27 | + graphQLPath: '/graphql', |
| 28 | + }); |
| 29 | + graphQLServer.applyGraphQL(expressApp); |
| 30 | + await new Promise(resolve => httpServer.listen({ port: 13378 }, resolve)); |
| 31 | + } |
| 32 | + |
| 33 | + async function graphqlRequest(query, requestHeaders = headers) { |
| 34 | + const response = await fetch('http://localhost:13378/graphql', { |
| 35 | + method: 'POST', |
| 36 | + headers: requestHeaders, |
| 37 | + body: JSON.stringify({ query }), |
| 38 | + }); |
| 39 | + return response.json(); |
| 40 | + } |
| 41 | + |
| 42 | + function buildDeepQuery(depth) { |
| 43 | + let query = '{ users { edges { node {'; |
| 44 | + let closing = ''; |
| 45 | + // Each 'users' nesting adds depth through edges > node |
| 46 | + // Start at depth 4 for the base: users > edges > node > objectId |
| 47 | + // We add more depth by nesting pointer fields won't work easily, |
| 48 | + // so instead we build depth with repeated nested field selections |
| 49 | + for (let i = 0; i < depth; i++) { |
| 50 | + query += ` f${i} {`; |
| 51 | + closing += ' }'; |
| 52 | + } |
| 53 | + query += ' objectId' + closing + ' } } } }'; |
| 54 | + return query; |
| 55 | + } |
| 56 | + |
| 57 | + function buildWideQuery(fieldCount) { |
| 58 | + const fields = Array.from({ length: fieldCount }, (_, i) => `field${i}: objectId`).join('\n '); |
| 59 | + return `{ users { edges { node { ${fields} } } } }`; |
| 60 | + } |
| 61 | + |
| 62 | + afterEach(async () => { |
| 63 | + if (httpServer) { |
| 64 | + await new Promise(resolve => httpServer.close(resolve)); |
| 65 | + httpServer = null; |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + describe('depth limit', () => { |
| 70 | + it('should reject query exceeding depth limit', async () => { |
| 71 | + await setupGraphQL({ |
| 72 | + requestComplexity: { graphQLDepth: 3 }, |
| 73 | + }); |
| 74 | + // Depth: users(1) > edges(2) > node(3) > objectId(4) = depth 4 |
| 75 | + const result = await graphqlRequest('{ users { edges { node { objectId } } } }'); |
| 76 | + expect(result.errors).toBeDefined(); |
| 77 | + expect(result.errors[0].message).toMatch( |
| 78 | + /GraphQL query depth of \d+ exceeds maximum allowed depth of 3/ |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should allow query within depth limit', async () => { |
| 83 | + await setupGraphQL({ |
| 84 | + requestComplexity: { graphQLDepth: 10 }, |
| 85 | + }); |
| 86 | + const result = await graphqlRequest('{ users { edges { node { objectId } } } }'); |
| 87 | + expect(result.errors).toBeUndefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should allow deep query with master key', async () => { |
| 91 | + await setupGraphQL({ |
| 92 | + requestComplexity: { graphQLDepth: 3 }, |
| 93 | + }); |
| 94 | + const result = await graphqlRequest('{ users { edges { node { objectId } } } }', { |
| 95 | + ...headers, |
| 96 | + 'X-Parse-Master-Key': 'test', |
| 97 | + }); |
| 98 | + expect(result.errors).toBeUndefined(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should allow unlimited depth when graphQLDepth is -1', async () => { |
| 102 | + await setupGraphQL({ |
| 103 | + requestComplexity: { graphQLDepth: -1 }, |
| 104 | + }); |
| 105 | + const result = await graphqlRequest('{ users { edges { node { objectId } } } }'); |
| 106 | + expect(result.errors).toBeUndefined(); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('fields limit', () => { |
| 111 | + it('should reject query exceeding fields limit', async () => { |
| 112 | + await setupGraphQL({ |
| 113 | + requestComplexity: { graphQLFields: 5 }, |
| 114 | + }); |
| 115 | + const result = await graphqlRequest(buildWideQuery(10)); |
| 116 | + expect(result.errors).toBeDefined(); |
| 117 | + expect(result.errors[0].message).toMatch( |
| 118 | + /Number of GraphQL fields \(\d+\) exceeds maximum allowed \(5\)/ |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should allow query within fields limit', async () => { |
| 123 | + await setupGraphQL({ |
| 124 | + requestComplexity: { graphQLFields: 200 }, |
| 125 | + }); |
| 126 | + const result = await graphqlRequest('{ users { edges { node { objectId } } } }'); |
| 127 | + expect(result.errors).toBeUndefined(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should allow wide query with master key', async () => { |
| 131 | + await setupGraphQL({ |
| 132 | + requestComplexity: { graphQLFields: 5 }, |
| 133 | + }); |
| 134 | + const result = await graphqlRequest(buildWideQuery(10), { |
| 135 | + ...headers, |
| 136 | + 'X-Parse-Master-Key': 'test', |
| 137 | + }); |
| 138 | + expect(result.errors).toBeUndefined(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should allow unlimited fields when graphQLFields is -1', async () => { |
| 142 | + await setupGraphQL({ |
| 143 | + requestComplexity: { graphQLFields: -1 }, |
| 144 | + }); |
| 145 | + const result = await graphqlRequest(buildWideQuery(50)); |
| 146 | + expect(result.errors).toBeUndefined(); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments