graphql-codegen-core
Version:
GraphQL types and code generator based on schema
114 lines • 6.35 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var types_1 = require("../types");
var graphql_1 = require("graphql");
var get_field_def_1 = require("../utils/get-field-def");
var resolve_type_1 = require("../schema/resolve-type");
var debugging_1 = require("../debugging");
var resolve_type_indicators_1 = require("../schema/resolve-type-indicators");
function isMetadataFieldName(name) {
return ['__schema', '__type'].includes(name);
}
function isRootType(type, schema) {
return (graphql_1.isEqualType(type, schema.getQueryType()) ||
graphql_1.isEqualType(type, schema.getMutationType()) ||
graphql_1.isEqualType(type, schema.getSubscriptionType()));
}
function separateSelectionSet(selectionSet) {
var fields = selectionSet.filter(function (n) { return types_1.isFieldNode(n); });
var fragmentsSpread = selectionSet.filter(function (n) { return types_1.isFragmentSpreadNode(n); });
var inlineFragments = selectionSet.filter(function (n) { return types_1.isInlineFragmentNode(n); });
return {
fragmentsSpread: fragmentsSpread,
fields: fields,
inlineFragments: inlineFragments,
hasFragmentsSpread: fragmentsSpread.length > 0,
hasFields: fields.length > 0,
hasInlineFragments: inlineFragments.length > 0
};
}
exports.separateSelectionSet = separateSelectionSet;
var metadataObjectMap = {
__schema: graphql_1.__Schema,
__type: graphql_1.__Type
};
var metadataFieldMap = {
__schema: graphql_1.SchemaMetaFieldDef,
__type: graphql_1.TypeMetaFieldDef
};
function buildMetadata(schema, fieldNode) {
var name = fieldNode.name.value;
var type = metadataObjectMap[name];
var field = metadataFieldMap[name];
return resolveFieldNode(schema, fieldNode, field, fieldNode.alias && fieldNode.alias.value ? fieldNode.alias.value : fieldNode.name.value, type);
}
exports.buildMetadata = buildMetadata;
function resolveFieldNode(schema, fieldNode, field, name, type) {
var resolvedType = resolve_type_1.resolveType(field.type);
var childSelectionSet = buildSelectionSet(schema, type, fieldNode.selectionSet);
var namedType = type;
var indicators = resolve_type_indicators_1.resolveTypeIndicators(namedType);
return __assign({ isField: true, isFragmentSpread: false, isInlineFragment: false, isLeaf: childSelectionSet.length === 0, schemaFieldName: fieldNode.name.value, name: name, isAliased: fieldNode.alias && fieldNode.alias.value, selectionSet: childSelectionSet }, separateSelectionSet(childSelectionSet), { type: resolvedType.name, raw: resolvedType.raw, isRequired: resolvedType.isRequired, isNullableArray: resolvedType.isNullableArray, isArray: resolvedType.isArray, dimensionOfArray: resolvedType.dimensionOfArray, hasTypename: hasTypename(fieldNode), isEnum: indicators.isEnum, isScalar: indicators.isScalar, isInterface: indicators.isInterface, isUnion: indicators.isUnion, isInputType: indicators.isInputType, isType: indicators.isType });
}
function buildSelectionSet(schema, rootObject, node) {
return (node && node.selections ? node.selections : [])
.map(function (selectionNode) {
if (selectionNode.kind === graphql_1.Kind.FIELD) {
var fieldNode = selectionNode;
var name = fieldNode.alias && fieldNode.alias.value ? fieldNode.alias.value : fieldNode.name.value;
debugging_1.debugLog("[buildSelectionSet] transforming FIELD with name " + name);
// Kamil: `__query` and `__type` metadata fields are available only in root types.
// Or I'm wrong and maybe just in Query type?
// Or I'm completely wrong and even outside of root types?
// So many unanswered questions...
if (isRootType(rootObject, schema) && isMetadataFieldName(fieldNode.name.value)) {
return buildMetadata(schema, fieldNode);
}
var field = get_field_def_1.getFieldDef(rootObject, fieldNode);
if (!field) {
debugging_1.debugLog("[buildSelectionSet] Ignoring field because of null result from getFieldDef...");
return null;
}
return resolveFieldNode(schema, fieldNode, field, name, graphql_1.getNamedType(field.type));
}
else if (selectionNode.kind === graphql_1.Kind.FRAGMENT_SPREAD) {
var fieldNode = selectionNode;
debugging_1.debugLog("[buildSelectionSet] transforming FRAGMENT_SPREAD with name " + fieldNode.name.value + "...");
return {
isField: false,
isFragmentSpread: true,
isInlineFragment: false,
isLeaf: true,
fragmentName: fieldNode.name.value
};
}
else if (selectionNode.kind === graphql_1.Kind.INLINE_FRAGMENT) {
debugging_1.debugLog("[buildSelectionSet] transforming INLINE_FRAGMENT...");
var fieldNode = selectionNode;
var nextRoot = graphql_1.typeFromAST(schema, fieldNode.typeCondition);
var childSelectionSet = buildSelectionSet(schema, nextRoot, fieldNode.selectionSet);
return __assign({ isField: false, isFragmentSpread: false, isInlineFragment: true, isLeaf: childSelectionSet.length === 0, selectionSet: childSelectionSet }, separateSelectionSet(childSelectionSet), { onType: fieldNode.typeCondition.name.value, hasTypename: hasTypename(fieldNode) });
}
else {
throw new Error("Unexpected GraphQL type: " + selectionNode.kind + "!");
}
})
.filter(function (item) { return item; }); // filter to remove null types
}
exports.buildSelectionSet = buildSelectionSet;
function hasTypename(fieldNode) {
return (fieldNode.selectionSet &&
fieldNode.selectionSet.selections.some(function (f) { return f.kind === 'Field' && f.name.value === '__typename'; }));
}
//# sourceMappingURL=build-selection-set.js.map