|
1 | 1 | import _ from "lodash"; |
| 2 | +import * as R from "ramda"; |
2 | 3 |
|
3 | 4 | import { |
4 | 5 | GraphQLNamedType, |
@@ -208,7 +209,40 @@ function assignTypesAndIDs(schema: SimplifiedIntrospection) { |
208 | 209 | } |
209 | 210 |
|
210 | 211 | function addParent(schema: SimplifiedIntrospection) { |
211 | | - return schema; |
| 212 | + function extractFields(type) { |
| 213 | + // no need to inspect circular types any further |
| 214 | + if (type.type === "[Circular]") return [type]; |
| 215 | + |
| 216 | + // same with scalars and enums |
| 217 | + if (R.includes(type.kind && type.kind, ["SCALAR", "ENUM"])) return []; |
| 218 | + if ( |
| 219 | + type.type && |
| 220 | + type.type.kind && |
| 221 | + R.includes(type.type.kind, ["SCALAR", "ENUM"]) |
| 222 | + ) |
| 223 | + return []; |
| 224 | + |
| 225 | + return R.chain((currentType) => { |
| 226 | + // if it's a composite object, use recursion to introspect the inner object |
| 227 | + const innerTypes = currentType.type.fields |
| 228 | + ? R.chain((subType) => extractFields(subType), currentType.type.fields) |
| 229 | + : [currentType]; |
| 230 | + // dedupe types by their id |
| 231 | + return R.uniqBy((x) => x.id, R.append(currentType, innerTypes)); |
| 232 | + }, R.values(type.fields)); |
| 233 | + } |
| 234 | + |
| 235 | + const allFields = R.chain( |
| 236 | + (type) => extractFields(type), |
| 237 | + R.values(schema.types) |
| 238 | + ); |
| 239 | + |
| 240 | + /* |
| 241 | + * Group fields by their corresponding type id |
| 242 | + */ |
| 243 | + schema.types = R.groupBy(function (field) { |
| 244 | + return field.type.id; |
| 245 | + }, allFields); |
212 | 246 | } |
213 | 247 |
|
214 | 248 | export function getSchema(schema: GraphQLSchema) { |
|
0 commit comments