apollo-codegen
Version:
Generate API code or type annotations based on a GraphQL schema and query documents
127 lines (109 loc) • 4.68 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isBuiltInScalarType = isBuiltInScalarType;
exports.withTypenameFieldAddedWhereNeeded = withTypenameFieldAddedWhereNeeded;
exports.sourceAt = sourceAt;
exports.filePathForNode = filePathForNode;
exports.valueFromValueNode = valueFromValueNode;
exports.isTypeProperSuperTypeOf = isTypeProperSuperTypeOf;
exports.getOperationRootType = getOperationRootType;
exports.getFieldDef = getFieldDef;
var _graphql = require('graphql');
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var builtInScalarTypes = new Set([_graphql.GraphQLString, _graphql.GraphQLInt, _graphql.GraphQLFloat, _graphql.GraphQLBoolean, _graphql.GraphQLID]);
function isBuiltInScalarType(type) {
return builtInScalarTypes.has(type);
}
var typenameField = { kind: _graphql.Kind.FIELD, name: { kind: _graphql.Kind.NAME, value: '__typename' } };
function withTypenameFieldAddedWhereNeeded(schema, ast) {
function isOperationRootType(type) {
return type === schema.getQueryType() || type === schema.getMutationType() || type === schema.getSubscriptionType();
}
var typeInfo = new _graphql.TypeInfo(schema);
return (0, _graphql.visit)(ast, (0, _graphql.visitWithTypeInfo)(typeInfo, {
leave: {
SelectionSet: function SelectionSet(node) {
var parentType = typeInfo.getParentType();
if (!isOperationRootType(parentType)) {
return Object.assign({}, node, { selections: [typenameField].concat(_toConsumableArray(node.selections)) });
}
}
}
}));
}
function sourceAt(location) {
return location.source.body.slice(location.start, location.end);
}
function filePathForNode(node) {
var name = node.loc.source && node.loc.source.name;
return name === "GraphQL" ? undefined : name;
}
function valueFromValueNode(valueNode) {
var kind = valueNode.kind;
if (kind === 'IntValue' || kind === 'FloatValue') {
return Number(valueNode.value);
} else if (kind === 'NullValue') {
return null;
} else if (kind === 'ListValue') {
return valueNode.values.map(valueFromValueNode);
} else if (kind === 'ObjectValue') {
return valueNode.fields.reduce(function (object, field) {
object[field.name.value] = valueFromValueNode(field.value);
return object;
}, {});
} else if (kind === 'Variable') {
return { kind, variableName: valueNode.name.value };
} else {
return valueNode.value;
}
}
function isTypeProperSuperTypeOf(schema, maybeSuperType, subType) {
return (0, _graphql.isEqualType)(maybeSuperType, subType) || (0, _graphql.isAbstractType)(maybeSuperType) && schema.isPossibleType(maybeSuperType, subType);
}
// Utility functions extracted from graphql-js
/**
* Extracts the root type of the operation from the schema.
*/
function getOperationRootType(schema, operation) {
switch (operation.operation) {
case 'query':
return schema.getQueryType();
case 'mutation':
var mutationType = schema.getMutationType();
if (!mutationType) {
throw new GraphQLError('Schema is not configured for mutations', [operation]);
}
return mutationType;
case 'subscription':
var subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError('Schema is not configured for subscriptions', [operation]);
}
return subscriptionType;
default:
throw new GraphQLError('Can only compile queries, mutations and subscriptions', [operation]);
}
}
/**
* Not exactly the same as the executor's definition of getFieldDef, in this
* statically evaluated environment we do not always have an Object type,
* and need to handle Interface and Union types.
*/
function getFieldDef(schema, parentType, fieldAST) {
var name = fieldAST.name.value;
if (name === _graphql.SchemaMetaFieldDef.name && schema.getQueryType() === parentType) {
return _graphql.SchemaMetaFieldDef;
}
if (name === _graphql.TypeMetaFieldDef.name && schema.getQueryType() === parentType) {
return _graphql.TypeMetaFieldDef;
}
if (name === _graphql.TypeNameMetaFieldDef.name && (parentType instanceof _graphql.GraphQLObjectType || parentType instanceof _graphql.GraphQLInterfaceType || parentType instanceof _graphql.GraphQLUnionType)) {
return _graphql.TypeNameMetaFieldDef;
}
if (parentType instanceof _graphql.GraphQLObjectType || parentType instanceof _graphql.GraphQLInterfaceType) {
return parentType.getFields()[name];
}
}
//# sourceMappingURL=graphql.js.map
;