UNPKG

15 kBSource Map (JSON)View Raw
1{"version":3,"file":"bundle.esm.js","sources":["../src/graphql.ts","../src/utilities.ts"],"sourcesContent":["import {\n DocumentNode,\n SelectionSetNode,\n FieldNode,\n FragmentDefinitionNode,\n InlineFragmentNode,\n} from 'graphql';\n\nimport {\n getMainDefinition,\n getFragmentDefinitions,\n createFragmentMap,\n FragmentMap,\n DirectiveInfo,\n shouldInclude,\n getDirectiveInfoFromField,\n isField,\n isInlineFragment,\n resultKeyNameFromField,\n argumentsObjectFromField,\n} from 'apollo-utilities';\n\nexport type Resolver = (\n fieldName: string,\n rootValue: any,\n args: any,\n context: any,\n info: ExecInfo,\n) => any;\n\nexport type VariableMap = { [name: string]: any };\n\nexport type ResultMapper = (\n values: { [fieldName: string]: any },\n rootValue: any,\n) => any;\nexport type FragmentMatcher = (\n rootValue: any,\n typeCondition: string,\n context: any,\n) => boolean;\n\nexport type ExecContext = {\n fragmentMap: FragmentMap;\n contextValue: any;\n variableValues: VariableMap;\n resultMapper: ResultMapper;\n resolver: Resolver;\n fragmentMatcher: FragmentMatcher;\n};\n\nexport type ExecInfo = {\n isLeaf: boolean;\n resultKey: string;\n directives: DirectiveInfo;\n field: FieldNode;\n};\n\nexport type ExecOptions = {\n resultMapper?: ResultMapper;\n fragmentMatcher?: FragmentMatcher;\n};\n\n/* Based on graphql function from graphql-js:\n *\n * graphql(\n * schema: GraphQLSchema,\n * requestString: string,\n * rootValue?: ?any,\n * contextValue?: ?any,\n * variableValues?: ?{[key: string]: any},\n * operationName?: ?string\n * ): Promise<GraphQLResult>\n *\n * The default export as of graphql-anywhere is sync as of 4.0,\n * but below is an exported alternative that is async.\n * In the 5.0 version, this will be the only export again\n * and it will be async\n */\nexport function graphql(\n resolver: Resolver,\n document: DocumentNode,\n rootValue?: any,\n contextValue?: any,\n variableValues: VariableMap = {},\n execOptions: ExecOptions = {},\n) {\n const mainDefinition = getMainDefinition(document);\n\n const fragments = getFragmentDefinitions(document);\n const fragmentMap = createFragmentMap(fragments);\n\n const resultMapper = execOptions.resultMapper;\n\n // Default matcher always matches all fragments\n const fragmentMatcher = execOptions.fragmentMatcher || (() => true);\n\n const execContext: ExecContext = {\n fragmentMap,\n contextValue,\n variableValues,\n resultMapper,\n resolver,\n fragmentMatcher,\n };\n\n return executeSelectionSet(\n mainDefinition.selectionSet,\n rootValue,\n execContext,\n );\n}\n\nfunction executeSelectionSet(\n selectionSet: SelectionSetNode,\n rootValue: any,\n execContext: ExecContext,\n) {\n const { fragmentMap, contextValue, variableValues: variables } = execContext;\n\n const result = {};\n\n selectionSet.selections.forEach(selection => {\n if (variables && !shouldInclude(selection, variables)) {\n // Skip selection sets which we're able to determine should not be run\n return;\n }\n\n if (isField(selection)) {\n const fieldResult = executeField(selection, rootValue, execContext);\n\n const resultFieldKey = resultKeyNameFromField(selection);\n\n if (fieldResult !== undefined) {\n if (result[resultFieldKey] === undefined) {\n result[resultFieldKey] = fieldResult;\n } else {\n merge(result[resultFieldKey], fieldResult);\n }\n }\n } else {\n let fragment: InlineFragmentNode | FragmentDefinitionNode;\n\n if (isInlineFragment(selection)) {\n fragment = selection;\n } else {\n // This is a named fragment\n fragment = fragmentMap[selection.name.value];\n\n if (!fragment) {\n throw new Error(`No fragment named ${selection.name.value}`);\n }\n }\n\n const typeCondition = fragment.typeCondition.name.value;\n\n if (execContext.fragmentMatcher(rootValue, typeCondition, contextValue)) {\n const fragmentResult = executeSelectionSet(\n fragment.selectionSet,\n rootValue,\n execContext,\n );\n\n merge(result, fragmentResult);\n }\n }\n });\n\n if (execContext.resultMapper) {\n return execContext.resultMapper(result, rootValue);\n }\n\n return result;\n}\n\nfunction executeField(\n field: FieldNode,\n rootValue: any,\n execContext: ExecContext,\n): any {\n const { variableValues: variables, contextValue, resolver } = execContext;\n\n const fieldName = field.name.value;\n const args = argumentsObjectFromField(field, variables);\n\n const info: ExecInfo = {\n isLeaf: !field.selectionSet,\n resultKey: resultKeyNameFromField(field),\n directives: getDirectiveInfoFromField(field, variables),\n field,\n };\n\n const result = resolver(fieldName, rootValue, args, contextValue, info);\n\n // Handle all scalar types here\n if (!field.selectionSet) {\n return result;\n }\n\n // From here down, the field has a selection set, which means it's trying to\n // query a GraphQLObjectType\n if (result == null) {\n // Basically any field in a GraphQL response can be null, or missing\n return result;\n }\n\n if (Array.isArray(result)) {\n return executeSubSelectedArray(field, result, execContext);\n }\n\n // Returned value is an object, and the query has a sub-selection. Recurse.\n return executeSelectionSet(field.selectionSet, result, execContext);\n}\n\nfunction executeSubSelectedArray(field, result, execContext) {\n return result.map(item => {\n // null value in array\n if (item === null) {\n return null;\n }\n\n // This is a nested array, recurse\n if (Array.isArray(item)) {\n return executeSubSelectedArray(field, item, execContext);\n }\n\n // This is an object, run the selection set on it\n return executeSelectionSet(field.selectionSet, item, execContext);\n });\n}\n\nconst hasOwn = Object.prototype.hasOwnProperty;\n\nexport function merge(dest, src) {\n if (src !== null && typeof src === 'object') {\n Object.keys(src).forEach(key => {\n const srcVal = src[key];\n if (!hasOwn.call(dest, key)) {\n dest[key] = srcVal;\n } else {\n merge(dest[key], srcVal);\n }\n });\n }\n}\n","import { DocumentNode, DirectiveNode } from 'graphql';\n\nimport { getInclusionDirectives } from 'apollo-utilities';\n\nimport { graphql, VariableMap, ExecInfo, ExecContext } from './graphql';\n\nimport { invariant } from 'ts-invariant';\n\nconst { hasOwnProperty } = Object.prototype;\n\nexport function filter<FD = any, D extends FD = any>(\n doc: DocumentNode,\n data: D,\n variableValues: VariableMap = {},\n): FD {\n if (data === null) return data;\n\n const resolver = (\n fieldName: string,\n root: any,\n args: Object,\n context: ExecContext,\n info: ExecInfo,\n ) => {\n return root[info.resultKey];\n };\n\n return Array.isArray(data)\n ? data.map(dataObj => graphql(resolver, doc, dataObj, null, variableValues))\n : graphql(resolver, doc, data, null, variableValues);\n}\n\n// TODO: we should probably make check call propType and then throw,\n// rather than the other way round, to avoid constructing stack traces\n// for things like oneOf uses in React. At this stage I doubt many people\n// are using this like that, but in the future, who knows?\nexport function check(\n doc: DocumentNode,\n data: any,\n variables: VariableMap = {},\n): void {\n const resolver = (\n fieldName: string,\n root: any,\n args: any,\n context: any,\n info: any,\n ) => {\n // When variables is null, fields with @include/skip directives that\n // reference variables are considered optional.\n invariant(\n hasOwnProperty.call(root, info.resultKey) ||\n (!variables && hasVariableInclusions(info.field.directives)),\n `${info.resultKey} missing on ${JSON.stringify(root)}`,\n );\n return root[info.resultKey];\n };\n\n graphql(resolver, doc, data, {}, variables, {\n fragmentMatcher: () => false,\n });\n}\n\nfunction hasVariableInclusions(\n directives: ReadonlyArray<DirectiveNode>,\n): boolean {\n return getInclusionDirectives(directives).some(\n ({ ifArgument }) =>\n ifArgument.value && ifArgument.value.kind === 'Variable',\n );\n}\n\n// Lifted/adapted from\n// https://github.com/facebook/react/blob/master/src/isomorphic/classic/types/ReactPropTypes.js\nconst ANONYMOUS = '<<anonymous>>';\nfunction PropTypeError(message) {\n this.message = message;\n this.stack = '';\n}\n// Make `instanceof Error` still work for returned errors.\nPropTypeError.prototype = Error.prototype;\n\nconst reactPropTypeLocationNames = {\n prop: 'prop',\n context: 'context',\n childContext: 'child context',\n};\n\nfunction createChainableTypeChecker(validate) {\n function checkType(\n isRequired,\n props,\n propName,\n componentName,\n location,\n propFullName,\n ) {\n componentName = componentName || ANONYMOUS;\n propFullName = propFullName || propName;\n if (props[propName] == null) {\n const locationName = reactPropTypeLocationNames[location];\n if (isRequired) {\n if (props[propName] === null) {\n return new PropTypeError(\n `The ${locationName} \\`${propFullName}\\` is marked as required ` +\n `in \\`${componentName}\\`, but its value is \\`null\\`.`,\n );\n }\n return new PropTypeError(\n `The ${locationName} \\`${propFullName}\\` is marked as required in ` +\n `\\`${componentName}\\`, but its value is \\`undefined\\`.`,\n );\n }\n return null;\n } else {\n return validate(props, propName, componentName, location, propFullName);\n }\n }\n\n const chainedCheckType = checkType.bind(null, false);\n chainedCheckType.isRequired = checkType.bind(null, true);\n\n return chainedCheckType;\n}\n\nexport function propType(\n doc: DocumentNode,\n mapPropsToVariables = props => null,\n) {\n return createChainableTypeChecker((props, propName) => {\n const prop = props[propName];\n try {\n if (!prop.loading) {\n check(doc, prop, mapPropsToVariables(props));\n }\n return null;\n } catch (e) {\n // Need a much better error.\n // Also we aren't checking for extra fields\n return e;\n }\n });\n}\n"],"names":[],"mappings":";;;SA+EgB,OAAO,CACrB,QAAkB,EAClB,QAAsB,EACtB,SAAe,EACf,YAAkB,EAClB,cAAgC,EAChC,WAA6B;IAD7B,+BAAA,EAAA,mBAAgC;IAChC,4BAAA,EAAA,gBAA6B;IAE7B,IAAM,cAAc,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IAEnD,IAAM,SAAS,GAAG,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IACnD,IAAM,WAAW,GAAG,iBAAiB,CAAC,SAAS,CAAC,CAAC;IAEjD,IAAM,YAAY,GAAG,WAAW,CAAC,YAAY,CAAC;IAG9C,IAAM,eAAe,GAAG,WAAW,CAAC,eAAe,KAAK,cAAM,OAAA,IAAI,GAAA,CAAC,CAAC;IAEpE,IAAM,WAAW,GAAgB;QAC/B,WAAW,aAAA;QACX,YAAY,cAAA;QACZ,cAAc,gBAAA;QACd,YAAY,cAAA;QACZ,QAAQ,UAAA;QACR,eAAe,iBAAA;KAChB,CAAC;IAEF,OAAO,mBAAmB,CACxB,cAAc,CAAC,YAAY,EAC3B,SAAS,EACT,WAAW,CACZ,CAAC;CACH;AAED,SAAS,mBAAmB,CAC1B,YAA8B,EAC9B,SAAc,EACd,WAAwB;IAEhB,IAAA,qCAAW,EAAE,uCAAY,EAAE,sCAAyB,CAAiB;IAE7E,IAAM,MAAM,GAAG,EAAE,CAAC;IAElB,YAAY,CAAC,UAAU,CAAC,OAAO,CAAC,UAAA,SAAS;QACvC,IAAI,SAAS,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,SAAS,CAAC,EAAE;YAErD,OAAO;SACR;QAED,IAAI,OAAO,CAAC,SAAS,CAAC,EAAE;YACt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