|  | 
|  | 1 | +import { expect } from 'chai'; | 
|  | 2 | +import { describe, it } from 'mocha'; | 
|  | 3 | + | 
|  | 4 | +import { invariant } from '../../jsutils/invariant.js'; | 
|  | 5 | + | 
|  | 6 | +import { Kind } from '../../language/kinds.js'; | 
|  | 7 | +import { parse } from '../../language/parser.js'; | 
|  | 8 | + | 
|  | 9 | +import { | 
|  | 10 | +  GraphQLInterfaceType, | 
|  | 11 | +  GraphQLObjectType, | 
|  | 12 | +} from '../../type/definition.js'; | 
|  | 13 | +import { GraphQLString } from '../../type/scalars.js'; | 
|  | 14 | +import { GraphQLSchema } from '../../type/schema.js'; | 
|  | 15 | + | 
|  | 16 | +import { validateExecutionArgs } from '../../execution/execute.js'; | 
|  | 17 | + | 
|  | 18 | +import { collectFields, collectSubfields } from '../collectFields.js'; | 
|  | 19 | + | 
|  | 20 | +describe('collectFields', () => { | 
|  | 21 | +  const someParentInterface = new GraphQLInterfaceType({ | 
|  | 22 | +    name: 'SomeParentInterface', | 
|  | 23 | +    fields: { someField: { type: GraphQLString } }, | 
|  | 24 | +  }); | 
|  | 25 | +  const someChildInterface = new GraphQLInterfaceType({ | 
|  | 26 | +    name: 'SomeChildInterface', | 
|  | 27 | +    interfaces: [someParentInterface], | 
|  | 28 | +    fields: { someField: { type: GraphQLString } }, | 
|  | 29 | +  }); | 
|  | 30 | +  const someObjectType = new GraphQLObjectType({ | 
|  | 31 | +    name: 'SomeObjectType', | 
|  | 32 | +    interfaces: [someChildInterface, someParentInterface], | 
|  | 33 | +    fields: { someField: { type: GraphQLString } }, | 
|  | 34 | +  }); | 
|  | 35 | +  const query = new GraphQLObjectType({ | 
|  | 36 | +    name: 'Query', | 
|  | 37 | +    fields: { | 
|  | 38 | +      someField: { type: GraphQLString }, | 
|  | 39 | +      anotherField: { type: someParentInterface }, | 
|  | 40 | +    }, | 
|  | 41 | +  }); | 
|  | 42 | +  const schema = new GraphQLSchema({ | 
|  | 43 | +    query, | 
|  | 44 | +    types: [someObjectType], | 
|  | 45 | +  }); | 
|  | 46 | + | 
|  | 47 | +  it('collects fields from a selection set', () => { | 
|  | 48 | +    const document = parse('{ someField }'); | 
|  | 49 | + | 
|  | 50 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 51 | +      schema, | 
|  | 52 | +      document, | 
|  | 53 | +    }); | 
|  | 54 | + | 
|  | 55 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 56 | + | 
|  | 57 | +    const { groupedFieldSet } = collectFields( | 
|  | 58 | +      validatedExecutionArgs, | 
|  | 59 | +      query, | 
|  | 60 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 61 | +    ); | 
|  | 62 | + | 
|  | 63 | +    expect(groupedFieldSet.get('someField')).to.deep.equal([ | 
|  | 64 | +      { | 
|  | 65 | +        node: validatedExecutionArgs.operation.selectionSet.selections[0], | 
|  | 66 | +        fragmentVariableValues: undefined, | 
|  | 67 | +      }, | 
|  | 68 | +    ]); | 
|  | 69 | +  }); | 
|  | 70 | + | 
|  | 71 | +  it('collects fields, skipping a field', () => { | 
|  | 72 | +    const document = parse('{ someField @skip(if: true) }'); | 
|  | 73 | + | 
|  | 74 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 75 | +      schema, | 
|  | 76 | +      document, | 
|  | 77 | +    }); | 
|  | 78 | + | 
|  | 79 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 80 | + | 
|  | 81 | +    const { groupedFieldSet } = collectFields( | 
|  | 82 | +      validatedExecutionArgs, | 
|  | 83 | +      query, | 
|  | 84 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 85 | +    ); | 
|  | 86 | + | 
|  | 87 | +    expect(groupedFieldSet.size).to.equal(0); | 
|  | 88 | +  }); | 
|  | 89 | + | 
|  | 90 | +  it('collects fields, not including a field', () => { | 
|  | 91 | +    const document = parse('{ someField @include(if: false) }'); | 
|  | 92 | + | 
|  | 93 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 94 | +      schema, | 
|  | 95 | +      document, | 
|  | 96 | +    }); | 
|  | 97 | + | 
|  | 98 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 99 | + | 
|  | 100 | +    const { groupedFieldSet } = collectFields( | 
|  | 101 | +      validatedExecutionArgs, | 
|  | 102 | +      query, | 
|  | 103 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 104 | +    ); | 
|  | 105 | + | 
|  | 106 | +    expect(groupedFieldSet.size).to.equal(0); | 
|  | 107 | +  }); | 
|  | 108 | + | 
|  | 109 | +  it('collects fields from a selection with an inline fragment', () => { | 
|  | 110 | +    const document = parse('{ ... { someField } }'); | 
|  | 111 | + | 
|  | 112 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 113 | +      schema, | 
|  | 114 | +      document, | 
|  | 115 | +    }); | 
|  | 116 | + | 
|  | 117 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 118 | + | 
|  | 119 | +    const inlineFragment = | 
|  | 120 | +      validatedExecutionArgs.operation.selectionSet.selections[0]; | 
|  | 121 | + | 
|  | 122 | +    invariant(inlineFragment.kind === Kind.INLINE_FRAGMENT); | 
|  | 123 | + | 
|  | 124 | +    const { groupedFieldSet } = collectFields( | 
|  | 125 | +      validatedExecutionArgs, | 
|  | 126 | +      query, | 
|  | 127 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 128 | +    ); | 
|  | 129 | + | 
|  | 130 | +    expect(groupedFieldSet.get('someField')).to.deep.equal([ | 
|  | 131 | +      { | 
|  | 132 | +        node: inlineFragment.selectionSet.selections[0], | 
|  | 133 | +        fragmentVariableValues: undefined, | 
|  | 134 | +      }, | 
|  | 135 | +    ]); | 
|  | 136 | +  }); | 
|  | 137 | + | 
|  | 138 | +  it('collects fields from a selection with a named fragment with a non-matching conditional type', () => { | 
|  | 139 | +    const document = parse(` | 
|  | 140 | +      query { ...SomeFragment } | 
|  | 141 | +      fragment SomeFragment on SomeObject { someField } | 
|  | 142 | +    `); | 
|  | 143 | + | 
|  | 144 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 145 | +      schema, | 
|  | 146 | +      document, | 
|  | 147 | +    }); | 
|  | 148 | + | 
|  | 149 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 150 | + | 
|  | 151 | +    const { groupedFieldSet } = collectFields( | 
|  | 152 | +      validatedExecutionArgs, | 
|  | 153 | +      query, | 
|  | 154 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 155 | +    ); | 
|  | 156 | + | 
|  | 157 | +    expect(groupedFieldSet.size).to.equal(0); | 
|  | 158 | +  }); | 
|  | 159 | + | 
|  | 160 | +  it('collects fields from a selection with an inline fragment with a conditional type', () => { | 
|  | 161 | +    const document = parse('{ ... on Query { someField } }'); | 
|  | 162 | + | 
|  | 163 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 164 | +      schema, | 
|  | 165 | +      document, | 
|  | 166 | +    }); | 
|  | 167 | + | 
|  | 168 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 169 | + | 
|  | 170 | +    const inlineFragment = | 
|  | 171 | +      validatedExecutionArgs.operation.selectionSet.selections[0]; | 
|  | 172 | + | 
|  | 173 | +    invariant(inlineFragment.kind === Kind.INLINE_FRAGMENT); | 
|  | 174 | + | 
|  | 175 | +    const { groupedFieldSet } = collectFields( | 
|  | 176 | +      validatedExecutionArgs, | 
|  | 177 | +      query, | 
|  | 178 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 179 | +    ); | 
|  | 180 | + | 
|  | 181 | +    expect(groupedFieldSet.get('someField')).to.deep.equal([ | 
|  | 182 | +      { | 
|  | 183 | +        node: inlineFragment.selectionSet.selections[0], | 
|  | 184 | +        fragmentVariableValues: undefined, | 
|  | 185 | +      }, | 
|  | 186 | +    ]); | 
|  | 187 | +  }); | 
|  | 188 | + | 
|  | 189 | +  it('collects fields from a selection with an inline fragment with a conditional abstract subtype', () => { | 
|  | 190 | +    const document = parse( | 
|  | 191 | +      '{ anotherField { ... on SomeChildInterface { someField } } }', | 
|  | 192 | +    ); | 
|  | 193 | + | 
|  | 194 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 195 | +      schema, | 
|  | 196 | +      document, | 
|  | 197 | +    }); | 
|  | 198 | + | 
|  | 199 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 200 | + | 
|  | 201 | +    const { groupedFieldSet } = collectFields( | 
|  | 202 | +      validatedExecutionArgs, | 
|  | 203 | +      query, | 
|  | 204 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 205 | +    ); | 
|  | 206 | + | 
|  | 207 | +    const fieldDetailsList = groupedFieldSet.get('anotherField'); | 
|  | 208 | + | 
|  | 209 | +    invariant(fieldDetailsList != null); | 
|  | 210 | + | 
|  | 211 | +    const { groupedFieldSet: nestedGroupedFieldSet } = collectSubfields( | 
|  | 212 | +      validatedExecutionArgs, | 
|  | 213 | +      someObjectType, | 
|  | 214 | +      fieldDetailsList, | 
|  | 215 | +    ); | 
|  | 216 | + | 
|  | 217 | +    const field = validatedExecutionArgs.operation.selectionSet.selections[0]; | 
|  | 218 | + | 
|  | 219 | +    invariant(field.kind === Kind.FIELD); | 
|  | 220 | + | 
|  | 221 | +    const inlineFragment = field.selectionSet?.selections[0]; | 
|  | 222 | + | 
|  | 223 | +    invariant(inlineFragment?.kind === Kind.INLINE_FRAGMENT); | 
|  | 224 | + | 
|  | 225 | +    expect(nestedGroupedFieldSet.get('someField')).to.deep.equal([ | 
|  | 226 | +      { | 
|  | 227 | +        node: inlineFragment.selectionSet.selections[0], | 
|  | 228 | +        fragmentVariableValues: undefined, | 
|  | 229 | +      }, | 
|  | 230 | +    ]); | 
|  | 231 | +  }); | 
|  | 232 | + | 
|  | 233 | +  it('collects fields from a selection with an inline fragment with a non-matching conditional subtype', () => { | 
|  | 234 | +    const document = parse('{ ... on SomeObject { someField } }'); | 
|  | 235 | + | 
|  | 236 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 237 | +      schema, | 
|  | 238 | +      document, | 
|  | 239 | +    }); | 
|  | 240 | + | 
|  | 241 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 242 | + | 
|  | 243 | +    const { groupedFieldSet } = collectFields( | 
|  | 244 | +      validatedExecutionArgs, | 
|  | 245 | +      query, | 
|  | 246 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 247 | +    ); | 
|  | 248 | + | 
|  | 249 | +    expect(groupedFieldSet.size).to.equal(0); | 
|  | 250 | +  }); | 
|  | 251 | + | 
|  | 252 | +  it('collects fields, using fragment variables', () => { | 
|  | 253 | +    const document = parse( | 
|  | 254 | +      ` | 
|  | 255 | +        query { ...SomeFragment(skip: false) } | 
|  | 256 | +        fragment SomeFragment($skip: Boolean ) on Query { someField @skip(if: $skip) } | 
|  | 257 | +      `, | 
|  | 258 | +      { | 
|  | 259 | +        experimentalFragmentArguments: true, | 
|  | 260 | +      }, | 
|  | 261 | +    ); | 
|  | 262 | + | 
|  | 263 | +    const validatedExecutionArgs = validateExecutionArgs({ | 
|  | 264 | +      schema, | 
|  | 265 | +      document, | 
|  | 266 | +    }); | 
|  | 267 | + | 
|  | 268 | +    invariant('schema' in validatedExecutionArgs); | 
|  | 269 | + | 
|  | 270 | +    const { groupedFieldSet } = collectFields( | 
|  | 271 | +      validatedExecutionArgs, | 
|  | 272 | +      query, | 
|  | 273 | +      validatedExecutionArgs.operation.selectionSet, | 
|  | 274 | +    ); | 
|  | 275 | + | 
|  | 276 | +    expect(groupedFieldSet.size).to.equal(1); | 
|  | 277 | +  }); | 
|  | 278 | +}); | 
0 commit comments