UNPKG

apollo-client

Version:
103 lines 3.46 kB
export function getMutationDefinition(doc) { checkDocument(doc); var mutationDef = null; doc.definitions.forEach(function (definition) { if (definition.kind === 'OperationDefinition' && definition.operation === 'mutation') { mutationDef = definition; } }); if (!mutationDef) { throw new Error('Must contain a mutation definition.'); } return mutationDef; } export function checkDocument(doc) { if (doc.kind !== 'Document') { throw new Error("Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql"); } var foundOperation = false; doc.definitions.forEach(function (definition) { switch (definition.kind) { case 'FragmentDefinition': break; case 'OperationDefinition': if (foundOperation) { throw new Error('Queries must have exactly one operation definition.'); } foundOperation = true; break; default: throw new Error("Schema type definitions not allowed in queries. Found: \"" + definition.kind + "\""); } }); } export function getOperationName(doc) { var res = ''; doc.definitions.forEach(function (definition) { if (definition.kind === 'OperationDefinition' && definition.name) { res = definition.name.value; } }); return res; } export function getFragmentDefinitions(doc) { var fragmentDefinitions = doc.definitions.filter(function (definition) { if (definition.kind === 'FragmentDefinition') { return true; } else { return false; } }); return fragmentDefinitions; } export function getQueryDefinition(doc) { checkDocument(doc); var queryDef = null; doc.definitions.map(function (definition) { if (definition.kind === 'OperationDefinition' && definition.operation === 'query') { queryDef = definition; } }); if (!queryDef) { throw new Error('Must contain a query definition.'); } return queryDef; } export function getOperationDefinition(doc) { checkDocument(doc); var opDef = null; doc.definitions.map(function (definition) { if (definition.kind === 'OperationDefinition') { opDef = definition; } }); if (!opDef) { throw new Error('Must contain a query definition.'); } return opDef; } export function getFragmentDefinition(doc) { if (doc.kind !== 'Document') { throw new Error("Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a \"gql\" tag? http://docs.apollostack.com/apollo-client/core.html#gql"); } if (doc.definitions.length > 1) { throw new Error('Fragment must have exactly one definition.'); } var fragmentDef = doc.definitions[0]; if (fragmentDef.kind !== 'FragmentDefinition') { throw new Error('Must be a fragment definition.'); } return fragmentDef; } export function createFragmentMap(fragments) { if (fragments === void 0) { fragments = []; } var symTable = {}; fragments.forEach(function (fragment) { symTable[fragment.name.value] = fragment; }); return symTable; } //# sourceMappingURL=getFromAST.js.map