UNPKG

@graphile/pro

Version:

Professional PostGraphile enhancements

1 lines 16.2 kB
module.exports=(()=>{"use strict";var e={95:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0});const n=1e5,r=5e-4,i=1.2;t.default=e=>{function t(e,t,n,r=1e3,i=.12){var o;return Object.assign(Object.assign({},e),{extensions:Object.assign(Object.assign({},e.extensions),{graphilePro:Object.assign(Object.assign({},null===(o=e.extensions)||void 0===o?void 0:o.graphilePro),{getCosts(e){const{first:o,last:s}=e,a=o||s||r,l=0===a?0:1*Math.pow(a+3,.95);return{cost:t,baseChildCost:i,multiplier:Math.max(1,n*l)}}})})})}function o(e,t){var n;const r="number"==typeof t?{cost:t}:t;return Object.assign(Object.assign({},e),{extensions:Object.assign(Object.assign({},e.extensions),{graphilePro:Object.assign(Object.assign({},null===(n=e.extensions)||void 0===n?void 0:n.graphilePro),{getCosts:()=>r})})})}e.hook("GraphQLObjectType:fields:field",((e,t,n)=>{var r,i,o,s,a;const{scope:{isLiveField:l,originalField:p}}=n;if(!l)return e;const c=(null===(i=null===(r=e.extensions)||void 0===r?void 0:r.graphilePro)||void 0===i?void 0:i.getCosts)||(null===(s=null===(o=p.extensions)||void 0===o?void 0:o.graphilePro)||void 0===s?void 0:s.getCosts);return c?Object.assign(Object.assign({},e),{extensions:Object.assign(Object.assign({},e.extensions),{graphilePro:Object.assign(Object.assign({},null===(a=e.extensions)||void 0===a?void 0:a.graphilePro),{getCosts(...e){const t=c.apply(this,e);return Object.assign(Object.assign({},t),{cost:10*t.cost})}})})}):e})),e.hook("GraphQLObjectType:fields:field",((e,s,{scope:{isRootQuery:a,isPgFieldConnection:l,isPgFieldSimpleCollection:p,isPgConnectionTotalCountField:c,isPgRowConnectionType:u,isPageInfoHasNextPageField:d,isPageInfoHasPreviousPageField:g,isPgForwardRelationField:f,isPgBackwardRelationField:h,pgFieldIntrospection:m,fieldName:v}})=>{if(l||p)return m&&"class"===m.kind?a?t(e,1,1,parseInt(m.tags.unboundedAllCost,10)||n):t(e,.06,i,parseInt(m.tags.unboundedRelationCost,10)||1e3):m&&"procedure"===m.kind?a?t(e,(m.cost||100)*r,1,parseInt(m.tags.unboundedCost,10)||1e3):t(e,(m.cost||100)*r,i,parseInt(m.tags.unboundedCost,10)||50):t(e,2,i,n);if(c)return m&&"class"===m.kind?o(e,{cost:2,useParentMultiplier:!1}):(m&&m.kind,o(e,{cost:10,useParentMultiplier:!1}));if(u){if("pageInfo"===v)return o(e,{useParentMultiplier:!1,multiplier:1})}else{if(d||g)return o(e,{cost:7});if(f)return o(e,{cost:.06});if(h)return t(e,.06,i,1e3);if(m&&"procedure"===m.kind)return o(e,{cost:(m.cost||100)*r})}return e}))}},212:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=e=>{e.hook("GraphQLObjectType:fields:field",((e,t,n)=>{var r,i,o,s,a;const{scope:{isLiveField:l,originalField:p}}=n;return l?Object.assign(Object.assign({},e),{extensions:Object.assign(Object.assign({},e.extensions),{graphilePro:Object.assign(Object.assign({},null===(r=e.extensions)||void 0===r?void 0:r.graphilePro),{paginationCap:(null===(o=null===(i=e.extensions)||void 0===i?void 0:i.graphilePro)||void 0===o?void 0:o.paginationCap)||(null===(a=null===(s=p.extensions)||void 0===s?void 0:s.graphilePro)||void 0===a?void 0:a.paginationCap)})})}):e})),e.hook("GraphQLObjectType:fields:field",((e,t,{scope:{isPgFieldConnection:n,isPgFieldSimpleCollection:r,pgFieldIntrospection:i,fieldName:o}})=>{var s;if(!n&&!r||!i||"class"!==i.kind&&"procedure"!==i.kind)return e;const a=null!=i.tags.paginationCap?parseInt(i.tags.paginationCap,10):1/0;if(-1===a||void 0===a)return e;if("number"!=typeof a||!isFinite(a)&&a!==1/0||a<-1)throw new Error(`Invalid pagination cap for '${o}', expected a number but instead received '${a}'`);return Object.assign(Object.assign({},e),{extensions:Object.assign(Object.assign({},e.extensions),{graphilePro:Object.assign(Object.assign({},null===(s=e.extensions)||void 0===s?void 0:s.graphilePro),{paginationCap:a,isPgFieldConnection:n,isPgFieldSimpleCollection:r})})})}))}},636:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0});const r=n(313),i=n(393),o=n(679);t.default=e=>t=>{const{coerced:n,errors:s}=i.default(e,t);return{Document:{enter(){s&&s.forEach((e=>t.reportError(e)))}},Field:{enter(i){var s,a,l,p;const c=t.getFieldDef();if(!c)return;const u=(d=null===(a=null===(s=c.extensions)||void 0===s?void 0:s.graphilePro)||void 0===a?void 0:a.paginationCap,g=e.defaultPaginationCap,d===1/0?g:d);var d,g;if(null==u||!isFinite(u)||u<0)return;if(!(!(null===(p=null===(l=c.extensions)||void 0===l?void 0:l.graphilePro)||void 0===p?void 0:p.isPgFieldConnection)||i.selectionSet&&i.selectionSet.selections.find((e=>"Field"!==e.kind||"totalCount"!==e.name.value))))return;let f;try{f=o.getArgumentValues(c,i,n)||{}}catch(e){return void t.reportError(e)}const h=f.first,m=f.last;if(!h&&!m){const e=i.name.value;return void t.reportError(new r.GraphQLError(`You must provide a 'first' or 'last' argument to properly paginate the '${e}' field.`,i))}const v=h||m;if(v<0){const e=i.name.value;t.reportError(new r.GraphQLError(`Invalid pagination limit '${v}' on field '${e}', please pass an integer greater than zero.`,i))}else if(v>u){const e=i.name.value;t.reportError(new r.GraphQLError(`Invalid pagination limit '${v}' on field '${e}', please pass an integer no larger than '${u}'.`,i))}}}}}},393:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0});const r=n(281);t.default=function(e,t){if(!e.calculatedVariableValues){const{operationName:n,variables:i}=e,o=t.getSchema(),s=t.getDocument().definitions.find((e=>"OperationDefinition"===e.kind&&(!n||!!e.name&&e.name.value===n)));if(!s)throw new Error(`Could not find operation named '${n}'`);const{errors:a,coerced:l}=r.getVariableValues(o,[...s.variableDefinitions||[]],i||{});e.calculatedVariableValues={errors:a,coerced:l}}return e.calculatedVariableValues}},607:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PgForceConnectionLimitPlugin=t.PgCostsPlugin=void 0;const r=n(780),i=n(662),o=n(680),s=n(636),a=n(95);t.PgCostsPlugin=a.default;const l=n(212);t.PgForceConnectionLimitPlugin=l.default;const p=n(723),c=n(417),u=n(488),d=36e5,g=24*d;function f(e){const t=e||process.env.GRAPHILE_LICENSE;if("string"!=typeof t)throw new Error("You must set the GRAPHILE_LICENSE environmental variable to use @graphile/pro, find out more: https://www.graphile.org/postgraphile/plugins/#first-party-premium-plugins");let n;const r=(i=t,Buffer.from(String(i),"base64"));var i;if(r.length>7&&0==(7&r.readUInt8(r.length-1))){const e=r.readUInt8(r.length-1)>>3,t=r.readUInt8(r.length-2);if(r.length>5+t){const i=r.slice(2,2+t),o=r.slice(2+t,r.length-2);if(0===e){const e=c.createVerify("SHA256");if(e.write(o),e.end(),e.verify("-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEiapOpoQOUtpoIoJjCZPyAiX51aIt\nwMJFV4OIttJG7N7EVGOAsgLddp02m8M35S82aHwCHW/AqeBbDpcCVvKGsQ==\n-----END PUBLIC KEY-----",i))try{n=JSON.parse(o.toString("utf8"))}catch(e){throw new Error("Invalid (corrupt) license file")}}}}if(!n)throw new Error("Invalid license key");const{t:o,g:s,e:a}=n,l=o;if(2!=(2&s))throw new Error("Your license does not cover usage of the @graphile/pro module");if(a<Date.now())throw new Error("Your Graphile license has expired, please renew it!");if(a<Date.now()+90*g){const e=a-Date.now(),t=Math.floor(e/g),n=Math.floor((e-t*g)/d);console.error(`\n\n⚠️ ⚠️ ⚠️ \n\nYour Graphile license key will expire in ${t} ${1===t?"day":"days"} ${n} ${1===n?"hour":"hours"}, please update it at the Graphile Store.\n\n⚠️ ⚠️ ⚠️ \n\n`)}return{licensedTo:l}}function h(e){return e&&"function"==typeof e.constructor&&e.constructor.name&&String(e.constructor.name)||null}let m=!1;const v={"cli:flags:add:standard":e=>(e("--read-only-connection <string>","⚡️pass the PostgreSQL connection string to use for read-only queries (i.e. not mutations) - typically for connecting to replicas via PgBouncer or similar"),e),"cli:flags:add":e=>(e("--default-pagination-cap [int]","⚡️Ensures all connections have first/last specified and are no large than this value (default: 50), set to ' -1' to disable; override via smart comment `@paginationCap 50`"),e("--graphql-depth-limit [int]","⚡️Validates GraphQL queries cannot be deeper than the specified int (default: 16), set to ' -1' to disable"),e("--graphql-cost-limit [int]","⚡️[experimental] Only allows queries with a computed cost below the specified int (default: 30000), set to ' -1' to disable"),e),"cli:library:options"(e,{config:t,cliOptions:n}){const r=Object.assign(Object.assign({},t.options),n),{defaultPaginationCap:i,graphqlDepthLimit:o,graphqlCostLimit:s,exposeGraphQLCost:a=!0,readOnlyConnection:l}=r;return Object.assign(Object.assign({},e),{defaultPaginationCap:parseInt(String(i||""),10)||50,graphqlDepthLimit:parseInt(String(o||""),10)||16,graphqlCostLimit:parseInt(String(s||""),10)||3e4,exposeGraphQLCost:a,readOnlyConnection:l})},"cli:greeting"(e,{options:t}){const{license:n}=t;try{const{licensedTo:t}=f(n);return[...e,`@graphile/pro: v1.0.4, licensed to '${t}'`]}catch(t){return m=!0,[...e,`@graphile/pro IS DISABLED - INVALID LICENSE: ${t.message}`]}},"postgraphile:options"(e){const{defaultPaginationCap:t,graphqlDepthLimit:n,graphqlCostLimit:r,exposeGraphQLCost:i=!0,readOnlyConnection:o,license:s,allowExplain:c}=e;try{f(s)}catch(t){return m=!0,console.error(),console.error(),console.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"),console.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"),console.error(),console.error(`ERROR: @graphile/pro IS DISABLED (due to invalid/expired license), none of its protections will function. ${t.message}`),console.error(),console.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"),console.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"),console.error(),console.error(),e}let d;var g,v,b;return o&&(d=(v=g=o)instanceof p.Pool||(b=v)&&"function"==typeof b.constructor&&b.constructor===p.Pool.super_||v&&"object"==typeof v&&("Pool"===h(v)||"BoundPool"===h(v))&&v.Client&&v.options&&"function"==typeof v.connect&&"function"==typeof v.end&&"function"==typeof v.query?g:new p.Pool("string"==typeof g?{connectionString:g}:g||{}),d.on("error",(e=>{console.error("Read only PostgreSQL client generated error: ",e.message)})),u.debugPgClient&&d.on("connect",(e=>{u.debugPgClient(e,!!c)}))),Object.assign(Object.assign({},e),{graphqlDepthLimit:null!=n?n:16,graphqlCostLimit:null!=r?r:3e4,exposeGraphQLCost:i,readReplicaPgPool:d,appendPlugins:[...e.appendPlugins||[],a.default,l.default],graphileBuildOptions:Object.assign(Object.assign({},e.graphileBuildOptions),{defaultPaginationCap:null!=t?t:50})})},"postgraphile:httpParamsList"(e,{options:t,httpError:n}){if(m)return e;const{batchedQueryLimit:r=10}=t;if(e.length>r)throw n(400,`Too many batched queries, send no more than ${r}`);return e},"postgraphile:validationRules"(e,{options:t,req:n,operationName:a,variables:l,meta:p}){if(m)return e;const{overrideOptionsForRequest:c}=t,u=r.__rest(t,["overrideOptionsForRequest"]),d="function"==typeof c?Object.assign(Object.assign({},u),c(n,{options:t})):u;return[...e,s.default({operationName:a,variables:l,defaultPaginationCap:d.defaultPaginationCap}),...d.graphqlDepthLimit>=0?[i(d.graphqlDepthLimit)]:[],...d.graphqlCostLimit>=0?[o.default({operationName:a,variables:l,costLimit:d.graphqlCostLimit,onComplete(e){d.exposeGraphQLCost&&p&&(p.graphqlQueryCost=e)}})]:[]]},withPostGraphileContext(e,{options:{readReplicaPgPool:t,queryDocumentAst:n,operationName:r}}){if(m)return e;const i=n;if(!t)return e;if(i&&"Document"===i.kind){const n=i.definitions.find((e=>"OperationDefinition"===e.kind&&(!r||!!e.name&&e.name.value===r)));if(n){const r=n.operation;if("query"===r||"subscription"===r)return(n,r)=>e(Object.assign(Object.assign({},n),{pgPool:t}),r)}}return e}};t.default=v},679:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.getArgumentValues=void 0;const r=n(281);Object.defineProperty(t,"getArgumentValues",{enumerable:!0,get:function(){return r.getArgumentValues}})},680:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0});const r=n(313),i=n(393),o=n(679),s=.001;function a(e,t,n,r={},i){if(Object.keys(r).find((e=>!["cost","baseChildCost","multiplier","parent","useParentMultiplier"].includes(e))))throw new Error("Query cost visitor failed, invalid options passed to newNode");const{cost:o=s,baseChildCost:a=0,multiplier:l=1,parent:p=null,useParentMultiplier:c=!0}=r;if(!isFinite(o))throw new Error(`Invalid cost for ${t} ${n}: '${o}'`);if(!isFinite(l))throw new Error(`Invalid multiplier for ${t} ${n}: '${l}'`);i&&i(`->cost=${o}, baseChildCost=${a}, multiplier=${l}, ${c?`use parent multiplier ${p?p.multiplier:"-"}`:"independent"}`);const u={astNode:e,type:t,name:"string"==typeof n?n:null,cost:o,baseChildCost:a,multiplier:l,parent:p,useParentMultiplier:c,children:[]};return p&&p.children.push(u),u}function l(e,t,n=0){const{type:r,name:i,cost:o,baseChildCost:s,multiplier:a}=e;if("FragmentSpread"===r){if(!i)throw new Error("Met a fragment with no name");const e=t[i];if(!e)throw new Error(`Query cost visitor failed, could not find fragment '${i}'`);return o+e.children.reduce(((e,r)=>e+(r.useParentMultiplier?a:1)*((r.useParentMultiplier?s:0)+l(r,t,n+1))),0)}return o+e.children.reduce(((e,r)=>e+(r.useParentMultiplier?a:1)*((r.useParentMultiplier?s:0)+l(r,t,n+1))),0)}t.default=e=>t=>{const{coerced:n,errors:s}=i.default(e,t),{costLimit:p,operationName:c,onComplete:u}=e,d=[],g={};function f(e){if("OperationDefinition"!==e.type)throw new Error("Query cost visitor failed, validate called on wrong type of node");return l(e,g)}let h=null,m=0;const v=null;return{Document:{enter(){s&&s.forEach((e=>t.reportError(e)))},leave(e){const n=d.length>1?d.find((e=>e.name===c)):d[0];if(n){const e=.2*f(n);e>p&&t.reportError(new r.GraphQLError(`This query has a computed cost of '${Math.ceil(e)}' which exceeds the limit of '${p}'. Try reducing the depth of the query, reducing the number of rows returned from connections or avoiding more complex fields.`,n.astNode)),"function"==typeof u&&u(Math.ceil(e),e)}else t.reportError(new r.GraphQLError(`Query cost visitor failed, could not find operation '${c}'`,e))}},OperationDefinition:{enter(e){m++;const n=e.name?e.name.value:"",i={query:1,mutation:2,subscription:10}[e.operation];i?(h=a(e,"OperationDefinition",n,{multiplier:i},v),d.push(h)):t.reportError(new r.GraphQLError(`Operation '${e.operation}' not understood.`,e))},leave(e){e.name&&e.name.value;h=null,m--,0!==m&&t.reportError(new r.GraphQLError(`Query cost visitor failed, '${m}' !== '0'`,e))}},FragmentDefinition:{enter(e){m++;const t=e.name.value;h=a(e,"FragmentDefinition",t,{},v),g[t]=h},leave(e){e.name.value;h=null,m--,0!==m&&t.reportError(new r.GraphQLError(`Query cost visitor failed, '${m}' !== '0'`,e))}},Field:{enter(e){var i,s;m++;const l=h,p=e.name.value,c=t.getParentType(),u=t.getFieldDef();if(!u)return;let d={};try{d=o.getArgumentValues(u,e,n)}catch(e){return void t.reportError(e)}const g="function"==typeof(null===(s=null===(i=u.extensions)||void 0===i?void 0:i.graphilePro)||void 0===s?void 0:s.getCosts)?u.extensions.graphilePro.getCosts(d):{},f=Object.keys(g).find((e=>!["cost","baseChildCost","multiplier","useParentMultiplier"].includes(e)));if(f)return void t.reportError(new r.GraphQLError(`Query cost visitor failed, invalid cost key '${f}' for ${c}#${p}`,e));const{cost:b,baseChildCost:P,multiplier:C,useParentMultiplier:y}=g;h=a(e,"Field",`${c}#${p}`,{parent:l,cost:b,baseChildCost:P,multiplier:C,useParentMultiplier:y},v)},leave(e){e.name.value,t.getParentType();h&&(h=h.parent),m--}},FragmentSpread:{enter(e){m++;const t=e.name.value;h=a(e,"FragmentSpread",t,{parent:h},v)},leave(){h=h?h.parent:null,m--}},InlineFragment:{enter(e){m++,h=a(e,"InlineFragment","(anonymous)",{parent:h},v)},leave(){h=h?h.parent:null,m--}}}}},417:e=>{e.exports=require("crypto")},313:e=>{e.exports=require("graphql")},662:e=>{e.exports=require("graphql-depth-limit")},281:e=>{e.exports=require("graphql/execution/values")},723:e=>{e.exports=require("pg")},488:e=>{e.exports=require("postgraphile")},780:e=>{e.exports=require("tslib")}},t={};return function n(r){if(t[r])return t[r].exports;var i=t[r]={exports:{}};return e[r](i,i.exports,n),i.exports}(607)})();