@graphprotocol/graph-cli 0.37.3 → 0.37.4-alpha-20230117095424-b5750b1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/bin.js +6 -0
- package/dist/cli.js +26 -0
- package/dist/codegen/schema.js +244 -0
- package/dist/codegen/schema.test.js +455 -0
- package/dist/codegen/template.js +66 -0
- package/dist/codegen/types/conversions.js +335 -0
- package/dist/codegen/types/index.js +71 -0
- package/dist/codegen/types/index.test.js +563 -0
- package/dist/codegen/typescript.js +200 -0
- package/dist/codegen/util.js +40 -0
- package/dist/codegen/util.test.js +93 -0
- package/dist/command-helpers/abi.js +76 -0
- package/dist/command-helpers/auth.js +76 -0
- package/dist/command-helpers/compiler.js +66 -0
- package/dist/command-helpers/data-sources.js +29 -0
- package/dist/command-helpers/fs.js +9 -0
- package/dist/command-helpers/gluegun.js +48 -0
- package/dist/command-helpers/ipfs.js +5 -0
- package/dist/command-helpers/jsonrpc.js +27 -0
- package/dist/command-helpers/network.js +90 -0
- package/dist/command-helpers/network.test.js +86 -0
- package/dist/command-helpers/node.js +40 -0
- package/dist/command-helpers/scaffold.js +110 -0
- package/dist/command-helpers/spinner.js +80 -0
- package/dist/command-helpers/studio.js +14 -0
- package/dist/command-helpers/studio.test.js +41 -0
- package/dist/command-helpers/subgraph.js +22 -0
- package/dist/command-helpers/version.js +66 -0
- package/dist/command-helpers/version.test.js +186 -0
- package/dist/commands/add.js +187 -0
- package/dist/commands/auth.js +122 -0
- package/dist/commands/build.js +136 -0
- package/dist/commands/codegen.js +128 -0
- package/dist/commands/create.js +94 -0
- package/dist/commands/deploy.js +335 -0
- package/dist/commands/init.js +800 -0
- package/dist/commands/local.js +380 -0
- package/dist/commands/remove.js +95 -0
- package/dist/commands/test.js +293 -0
- package/dist/compiler/asc.js +83 -0
- package/dist/compiler/index.js +466 -0
- package/dist/debug.js +16 -0
- package/dist/migrations/mapping_api_version_0_0_1.js +83 -0
- package/dist/migrations/mapping_api_version_0_0_2.js +83 -0
- package/dist/migrations/mapping_api_version_0_0_3.js +83 -0
- package/dist/migrations/mapping_api_version_0_0_4.js +85 -0
- package/dist/migrations/mapping_api_version_0_0_5.js +85 -0
- package/dist/migrations/spec_version_0_0_2.js +45 -0
- package/dist/migrations/spec_version_0_0_3.js +50 -0
- package/dist/migrations/util/load-manifest.js +16 -0
- package/dist/migrations/util/versions.js +28 -0
- package/dist/migrations.js +54 -0
- package/{src → dist}/protocols/arweave/manifest.graphql +0 -0
- package/{src/protocols/arweave/scaffold/manifest.ts → dist/protocols/arweave/scaffold/manifest.js} +7 -3
- package/{src/protocols/arweave/scaffold/mapping.ts → dist/protocols/arweave/scaffold/mapping.js} +5 -2
- package/dist/protocols/arweave/subgraph.js +23 -0
- package/dist/protocols/contract.js +2 -0
- package/{src → dist}/protocols/cosmos/manifest.graphql +0 -0
- package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
- package/{src/protocols/cosmos/scaffold/mapping.ts → dist/protocols/cosmos/scaffold/mapping.js} +5 -2
- package/dist/protocols/cosmos/subgraph.js +28 -0
- package/dist/protocols/ethereum/abi.js +138 -0
- package/dist/protocols/ethereum/codegen/abi.js +474 -0
- package/dist/protocols/ethereum/codegen/abi.test.js +325 -0
- package/dist/protocols/ethereum/codegen/template.js +52 -0
- package/dist/protocols/ethereum/contract.js +22 -0
- package/{src → dist}/protocols/ethereum/manifest.graphql +0 -0
- package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
- package/{src/protocols/ethereum/scaffold/mapping.ts → dist/protocols/ethereum/scaffold/mapping.js} +18 -29
- package/dist/protocols/ethereum/subgraph.js +172 -0
- package/dist/protocols/ethereum/type-generator.js +123 -0
- package/dist/protocols/index.js +266 -0
- package/dist/protocols/ipfs/codegen/file_template.js +53 -0
- package/dist/protocols/near/contract.js +41 -0
- package/{src → dist}/protocols/near/manifest.graphql +0 -0
- package/dist/protocols/near/scaffold/manifest.js +16 -0
- package/{src/protocols/near/scaffold/mapping.ts → dist/protocols/near/scaffold/mapping.js} +5 -2
- package/dist/protocols/near/subgraph.js +23 -0
- package/dist/protocols/subgraph.js +2 -0
- package/{src → dist}/protocols/substreams/manifest.graphql +0 -0
- package/dist/protocols/substreams/scaffold/manifest.js +12 -0
- package/dist/protocols/substreams/subgraph.js +23 -0
- package/{src/scaffold/cosmos.test.ts → dist/scaffold/cosmos.test.js} +21 -22
- package/{src/scaffold/ethereum.test.ts → dist/scaffold/ethereum.test.js} +85 -101
- package/dist/scaffold/index.js +133 -0
- package/dist/scaffold/mapping.js +57 -0
- package/{src/scaffold/near.test.ts → dist/scaffold/near.test.js} +22 -23
- package/dist/scaffold/schema.js +86 -0
- package/dist/scaffold/tests.js +179 -0
- package/dist/schema.js +54 -0
- package/dist/subgraph.js +243 -0
- package/dist/type-generator.js +225 -0
- package/dist/validation/contract.js +44 -0
- package/dist/validation/index.js +10 -0
- package/dist/validation/manifest.js +266 -0
- package/dist/validation/schema.js +768 -0
- package/dist/validation/schema.test.js +35 -0
- package/dist/watcher.js +79 -0
- package/package.json +5 -1
- package/babel.config.js +0 -3
- package/jest.config.js +0 -175
- package/src/bin.ts +0 -6
- package/src/cli.ts +0 -30
- package/src/codegen/schema.test.ts +0 -460
- package/src/codegen/schema.ts +0 -299
- package/src/codegen/template.ts +0 -45
- package/src/codegen/types/conversions.ts +0 -358
- package/src/codegen/types/index.test.ts +0 -715
- package/src/codegen/types/index.ts +0 -111
- package/src/codegen/typescript.ts +0 -224
- package/src/codegen/util.test.ts +0 -112
- package/src/codegen/util.ts +0 -54
- package/src/command-helpers/abi.ts +0 -99
- package/src/command-helpers/auth.ts +0 -46
- package/src/command-helpers/compiler.ts +0 -60
- package/src/command-helpers/data-sources.ts +0 -36
- package/src/command-helpers/fs.ts +0 -5
- package/src/command-helpers/gluegun.ts +0 -52
- package/src/command-helpers/ipfs-http-client.d.ts +0 -1
- package/src/command-helpers/ipfs.ts +0 -3
- package/src/command-helpers/jsonrpc.ts +0 -22
- package/src/command-helpers/network.test.ts +0 -85
- package/src/command-helpers/network.ts +0 -133
- package/src/command-helpers/node.ts +0 -46
- package/src/command-helpers/scaffold.ts +0 -174
- package/src/command-helpers/spinner.ts +0 -60
- package/src/command-helpers/studio.test.ts +0 -84
- package/src/command-helpers/studio.ts +0 -25
- package/src/command-helpers/subgraph.ts +0 -21
- package/src/command-helpers/version.test.ts +0 -241
- package/src/command-helpers/version.ts +0 -53
- package/src/commands/add.ts +0 -220
- package/src/commands/auth.ts +0 -138
- package/src/commands/binary-install-raw.d.ts +0 -1
- package/src/commands/build.ts +0 -147
- package/src/commands/codegen.ts +0 -123
- package/src/commands/create.ts +0 -109
- package/src/commands/deploy.ts +0 -388
- package/src/commands/init.ts +0 -1060
- package/src/commands/local.ts +0 -539
- package/src/commands/remove.ts +0 -110
- package/src/commands/test.ts +0 -369
- package/src/compiler/asc.ts +0 -77
- package/src/compiler/index.ts +0 -677
- package/src/debug.ts +0 -15
- package/src/migrations/mapping_api_version_0_0_1.ts +0 -75
- package/src/migrations/mapping_api_version_0_0_2.ts +0 -75
- package/src/migrations/mapping_api_version_0_0_3.ts +0 -75
- package/src/migrations/mapping_api_version_0_0_4.ts +0 -77
- package/src/migrations/mapping_api_version_0_0_5.ts +0 -77
- package/src/migrations/spec_version_0_0_2.ts +0 -24
- package/src/migrations/spec_version_0_0_3.ts +0 -29
- package/src/migrations/util/load-manifest.ts +0 -10
- package/src/migrations/util/versions.ts +0 -27
- package/src/migrations.ts +0 -33
- package/src/protocols/arweave/subgraph.ts +0 -22
- package/src/protocols/contract.ts +0 -9
- package/src/protocols/cosmos/scaffold/manifest.ts +0 -11
- package/src/protocols/cosmos/subgraph.ts +0 -27
- package/src/protocols/ethereum/abi.ts +0 -153
- package/src/protocols/ethereum/codegen/abi.test.ts +0 -330
- package/src/protocols/ethereum/codegen/abi.ts +0 -719
- package/src/protocols/ethereum/codegen/template.ts +0 -41
- package/src/protocols/ethereum/contract.ts +0 -24
- package/src/protocols/ethereum/scaffold/manifest.ts +0 -28
- package/src/protocols/ethereum/subgraph.ts +0 -189
- package/src/protocols/ethereum/type-generator.ts +0 -191
- package/src/protocols/index.ts +0 -287
- package/src/protocols/ipfs/codegen/file_template.ts +0 -43
- package/src/protocols/near/contract.ts +0 -47
- package/src/protocols/near/scaffold/manifest.ts +0 -13
- package/src/protocols/near/subgraph.ts +0 -22
- package/src/protocols/subgraph.ts +0 -12
- package/src/protocols/substreams/scaffold/manifest.ts +0 -8
- package/src/protocols/substreams/subgraph.ts +0 -22
- package/src/scaffold/index.ts +0 -172
- package/src/scaffold/mapping.ts +0 -44
- package/src/scaffold/schema.ts +0 -89
- package/src/scaffold/tests.ts +0 -217
- package/src/schema.ts +0 -26
- package/src/subgraph.ts +0 -302
- package/src/type-generator.ts +0 -266
- package/src/validation/contract.ts +0 -51
- package/src/validation/index.ts +0 -5
- package/src/validation/manifest.ts +0 -360
- package/src/validation/schema.test.ts +0 -38
- package/src/validation/schema.ts +0 -957
- package/src/watcher.ts +0 -96
- package/tests/cli/globalSetup.ts +0 -8
- package/tests/cli/globalTeardown.ts +0 -8
- package/tests/cli/init/ethereum/abis/Marketplace.json +0 -408
- package/tests/cli/init/ethereum/abis/OverloadedElements.json +0 -91
- package/tests/cli/init/ethereum/abis/SoloMargin.json +0 -2729
- package/tests/cli/init/ethereum/from-contract-with-abi-and-structs.stderr +0 -14
- package/tests/cli/init/ethereum/from-contract-with-abi-and-structs.stdout +0 -12
- package/tests/cli/init/ethereum/from-contract-with-abi.stderr +0 -14
- package/tests/cli/init/ethereum/from-contract-with-abi.stdout +0 -12
- package/tests/cli/init/ethereum/from-contract-with-overloaded-elements.stderr +0 -14
- package/tests/cli/init/ethereum/from-contract-with-overloaded-elements.stdout +0 -12
- package/tests/cli/init/ethereum/from-contract.stderr +0 -16
- package/tests/cli/init/ethereum/from-contract.stdout +0 -12
- package/tests/cli/init/ethereum/from-example.stderr +0 -12
- package/tests/cli/init/ethereum/from-example.stdout +0 -12
- package/tests/cli/init/near/from-contract.stderr +0 -14
- package/tests/cli/init/near/from-contract.stdout +0 -12
- package/tests/cli/init.test.ts +0 -157
- package/tests/cli/spawn-command.d.ts +0 -7
- package/tests/cli/util.ts +0 -132
- package/tests/cli/validation/abi-not-found/Abi.json +0 -7
- package/tests/cli/validation/abi-not-found/mapping.ts +0 -0
- package/tests/cli/validation/abi-not-found/schema.graphql +0 -3
- package/tests/cli/validation/abi-not-found/subgraph.yaml +0 -42
- package/tests/cli/validation/abi-not-found copy.stderr +0 -13
- package/tests/cli/validation/big-decimal-is-valid/Abi.json +0 -7
- package/tests/cli/validation/big-decimal-is-valid/mapping.ts +0 -0
- package/tests/cli/validation/big-decimal-is-valid/schema.graphql +0 -4
- package/tests/cli/validation/big-decimal-is-valid/subgraph.yaml +0 -23
- package/tests/cli/validation/call-function-not-found/Abi.json +0 -57
- package/tests/cli/validation/call-function-not-found/mapping.ts +0 -0
- package/tests/cli/validation/call-function-not-found/schema.graphql +0 -3
- package/tests/cli/validation/call-function-not-found/subgraph.yaml +0 -22
- package/tests/cli/validation/call-function-not-found.stderr +0 -10
- package/tests/cli/validation/call-handler-with-tuple/Abi.json +0 -24
- package/tests/cli/validation/call-handler-with-tuple/generated/schema.ts +0 -44
- package/tests/cli/validation/call-handler-with-tuple/mapping.ts +0 -0
- package/tests/cli/validation/call-handler-with-tuple/schema.graphql +0 -3
- package/tests/cli/validation/call-handler-with-tuple/subgraph.yaml +0 -22
- package/tests/cli/validation/call-handler-with-tuple.stderr +0 -24
- package/tests/cli/validation/conflicting-network-names/Abi.json +0 -7
- package/tests/cli/validation/conflicting-network-names/mapping.ts +0 -0
- package/tests/cli/validation/conflicting-network-names/schema.graphql +0 -3
- package/tests/cli/validation/conflicting-network-names/subgraph.yaml +0 -41
- package/tests/cli/validation/conflicting-network-names.stderr +0 -10
- package/tests/cli/validation/conflicting-protocol-names/Abi.json +0 -7
- package/tests/cli/validation/conflicting-protocol-names/mapping.ts +0 -0
- package/tests/cli/validation/conflicting-protocol-names/schema.graphql +0 -3
- package/tests/cli/validation/conflicting-protocol-names/subgraph.yaml +0 -41
- package/tests/cli/validation/conflicting-protocol-names.stderr +0 -10
- package/tests/cli/validation/derived-from-target-type-missing/Abi.json +0 -6
- package/tests/cli/validation/derived-from-target-type-missing/mapping.ts +0 -0
- package/tests/cli/validation/derived-from-target-type-missing/schema.graphql +0 -4
- package/tests/cli/validation/derived-from-target-type-missing/subgraph.yaml +0 -22
- package/tests/cli/validation/derived-from-target-type-missing.stderr +0 -5
- package/tests/cli/validation/derived-from-with-interface/Gravity.json +0 -1
- package/tests/cli/validation/derived-from-with-interface/mapping.ts +0 -22
- package/tests/cli/validation/derived-from-with-interface/schema.graphql +0 -19
- package/tests/cli/validation/derived-from-with-interface/subgraph.yaml +0 -25
- package/tests/cli/validation/duplicate-data-source-name/Abi.json +0 -7
- package/tests/cli/validation/duplicate-data-source-name/mapping.ts +0 -0
- package/tests/cli/validation/duplicate-data-source-name/schema.graphql +0 -4
- package/tests/cli/validation/duplicate-data-source-name/subgraph.yaml +0 -40
- package/tests/cli/validation/duplicate-data-source-name.stderr +0 -5
- package/tests/cli/validation/duplicate-template-name/Abi.json +0 -7
- package/tests/cli/validation/duplicate-template-name/mapping.ts +0 -0
- package/tests/cli/validation/duplicate-template-name/schema.graphql +0 -4
- package/tests/cli/validation/duplicate-template-name/subgraph.yaml +0 -59
- package/tests/cli/validation/duplicate-template-name.stderr +0 -5
- package/tests/cli/validation/entity-field-args/Abi.json +0 -7
- package/tests/cli/validation/entity-field-args/mapping.ts +0 -0
- package/tests/cli/validation/entity-field-args/schema.graphql +0 -4
- package/tests/cli/validation/entity-field-args/subgraph.yaml +0 -22
- package/tests/cli/validation/entity-field-args.stderr +0 -5
- package/tests/cli/validation/ethereum-contract-without-handlers/Abi.json +0 -7
- package/tests/cli/validation/ethereum-contract-without-handlers/mapping.ts +0 -0
- package/tests/cli/validation/ethereum-contract-without-handlers/schema.graphql +0 -3
- package/tests/cli/validation/ethereum-contract-without-handlers/subgraph.yaml +0 -19
- package/tests/cli/validation/ethereum-contract-without-handlers.stderr +0 -6
- package/tests/cli/validation/event-not-found/Abi.json +0 -7
- package/tests/cli/validation/event-not-found/mapping.ts +0 -0
- package/tests/cli/validation/event-not-found/schema.graphql +0 -3
- package/tests/cli/validation/event-not-found/subgraph.yaml +0 -40
- package/tests/cli/validation/event-not-found.stderr +0 -12
- package/tests/cli/validation/example-values-found/Abi.json +0 -7
- package/tests/cli/validation/example-values-found/generated/ExampleSubgraph/ExampleContract.ts +0 -35
- package/tests/cli/validation/example-values-found/generated/schema.ts +0 -44
- package/tests/cli/validation/example-values-found/mapping.ts +0 -0
- package/tests/cli/validation/example-values-found/schema.graphql +0 -3
- package/tests/cli/validation/example-values-found/subgraph.yaml +0 -24
- package/tests/cli/validation/example-values-found.stderr +0 -34
- package/tests/cli/validation/invalid-abis/InvalidAbi.json +0 -1
- package/tests/cli/validation/invalid-abis/mapping.ts +0 -0
- package/tests/cli/validation/invalid-abis/schema.graphql +0 -3
- package/tests/cli/validation/invalid-abis/subgraph.yaml +0 -22
- package/tests/cli/validation/invalid-abis.stderr +0 -5
- package/tests/cli/validation/invalid-contract-addresses/Abi.json +0 -7
- package/tests/cli/validation/invalid-contract-addresses/mapping.ts +0 -0
- package/tests/cli/validation/invalid-contract-addresses/schema.graphql +0 -3
- package/tests/cli/validation/invalid-contract-addresses/subgraph.yaml +0 -58
- package/tests/cli/validation/invalid-contract-addresses.stderr +0 -14
- package/tests/cli/validation/invalid-data-source-template/mapping.ts +0 -0
- package/tests/cli/validation/invalid-data-source-template/schema.graphql +0 -3
- package/tests/cli/validation/invalid-data-source-template/subgraph.yaml +0 -32
- package/tests/cli/validation/invalid-data-source-template.stderr +0 -30
- package/tests/cli/validation/invalid-entity-field-types/Abi.json +0 -7
- package/tests/cli/validation/invalid-entity-field-types/mapping.ts +0 -0
- package/tests/cli/validation/invalid-entity-field-types/schema.graphql +0 -157
- package/tests/cli/validation/invalid-entity-field-types/subgraph.yaml +0 -22
- package/tests/cli/validation/invalid-entity-field-types.stderr +0 -148
- package/tests/cli/validation/invalid-fulltext-directive/Abi.json +0 -7
- package/tests/cli/validation/invalid-fulltext-directive/mapping.ts +0 -0
- package/tests/cli/validation/invalid-fulltext-directive/schema.graphql +0 -140
- package/tests/cli/validation/invalid-fulltext-directive/subgraph.yaml +0 -22
- package/tests/cli/validation/invalid-fulltext-directive.stderr +0 -32
- package/tests/cli/validation/invalid-graphql-schema/Abi.json +0 -7
- package/tests/cli/validation/invalid-graphql-schema/mapping.ts +0 -0
- package/tests/cli/validation/invalid-graphql-schema/schema.graphql +0 -8
- package/tests/cli/validation/invalid-graphql-schema/subgraph.yaml +0 -22
- package/tests/cli/validation/invalid-graphql-schema.stderr +0 -25
- package/tests/cli/validation/invalid-manifest/subgraph.yaml +0 -13
- package/tests/cli/validation/invalid-manifest-cannot-infer-protocol/subgraph.yaml +0 -12
- package/tests/cli/validation/invalid-manifest-cannot-infer-protocol.stderr +0 -5
- package/tests/cli/validation/invalid-manifest.stderr +0 -28
- package/tests/cli/validation/missing-entity-id/Abi.json +0 -7
- package/tests/cli/validation/missing-entity-id/mapping.ts +0 -0
- package/tests/cli/validation/missing-entity-id/schema.graphql +0 -3
- package/tests/cli/validation/missing-entity-id/subgraph.yaml +0 -22
- package/tests/cli/validation/missing-entity-id/types/ExampleSubgraph/ExampleContract.ts +0 -35
- package/tests/cli/validation/missing-entity-id/types/schema.ts +0 -27
- package/tests/cli/validation/missing-entity-id.stderr +0 -5
- package/tests/cli/validation/missing-or-invalid-derived-from-fields/Abi.json +0 -7
- package/tests/cli/validation/missing-or-invalid-derived-from-fields/mapping.ts +0 -0
- package/tests/cli/validation/missing-or-invalid-derived-from-fields/schema.graphql +0 -40
- package/tests/cli/validation/missing-or-invalid-derived-from-fields/subgraph.yaml +0 -22
- package/tests/cli/validation/missing-or-invalid-derived-from-fields.stderr +0 -10
- package/tests/cli/validation/missing-or-invalid-derived-from-fields.stdout +0 -0
- package/tests/cli/validation/near-is-valid/mapping.ts +0 -0
- package/tests/cli/validation/near-is-valid/schema.graphql +0 -8
- package/tests/cli/validation/near-is-valid/subgraph.yaml +0 -30
- package/tests/cli/validation/nested-template-nice-error/Abi.json +0 -7
- package/tests/cli/validation/nested-template-nice-error/mapping.ts +0 -0
- package/tests/cli/validation/nested-template-nice-error/schema.graphql +0 -3
- package/tests/cli/validation/nested-template-nice-error/subgraph.yaml +0 -35
- package/tests/cli/validation/nested-template-nice-error.stderr +0 -5
- package/tests/cli/validation/no-network-names/Abi.json +0 -7
- package/tests/cli/validation/no-network-names/mapping.ts +0 -0
- package/tests/cli/validation/no-network-names/schema.graphql +0 -3
- package/tests/cli/validation/no-network-names/subgraph.yaml +0 -40
- package/tests/cli/validation/no-network-names.stderr +0 -34
- package/tests/cli/validation/source-without-address-is-valid/Abi.json +0 -7
- package/tests/cli/validation/source-without-address-is-valid/generated/ExampleSubgraph/ExampleContract.ts +0 -35
- package/tests/cli/validation/source-without-address-is-valid/mapping.ts +0 -0
- package/tests/cli/validation/source-without-address-is-valid/schema.graphql +0 -3
- package/tests/cli/validation/source-without-address-is-valid/subgraph.yaml +0 -23
- package/tests/cli/validation/source-without-address-is-valid.stderr +0 -24
- package/tests/cli/validation/topic0-is-valid/Abi.json +0 -7
- package/tests/cli/validation/topic0-is-valid/mapping.ts +0 -0
- package/tests/cli/validation/topic0-is-valid/schema.graphql +0 -4
- package/tests/cli/validation/topic0-is-valid/subgraph.yaml +0 -24
- package/tests/cli/validation.test.ts +0 -226
- package/tsconfig.build.json +0 -4
- package/tsconfig.json +0 -19
package/src/type-generator.ts
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
// @ts-expect-error TODO: type out if necessary
|
|
3
|
-
import uncrashable from '@float-capital/float-subgraph-uncrashable/src/Index.bs.js';
|
|
4
|
-
import fs from 'fs-extra';
|
|
5
|
-
import * as toolbox from 'gluegun';
|
|
6
|
-
import * as graphql from 'graphql/language';
|
|
7
|
-
import immutable from 'immutable';
|
|
8
|
-
import prettier from 'prettier';
|
|
9
|
-
import DataSourceTemplateCodeGenerator from './codegen/template';
|
|
10
|
-
import { GENERATED_FILE_NOTE } from './codegen/typescript';
|
|
11
|
-
import { displayPath } from './command-helpers/fs';
|
|
12
|
-
import { Spinner, step, withSpinner } from './command-helpers/spinner';
|
|
13
|
-
import { applyMigrations } from './migrations';
|
|
14
|
-
import Protocol from './protocols';
|
|
15
|
-
import Schema from './schema';
|
|
16
|
-
import Subgraph from './subgraph';
|
|
17
|
-
import Watcher from './watcher';
|
|
18
|
-
|
|
19
|
-
export interface TypeGeneratorOptions {
|
|
20
|
-
sourceDir?: string;
|
|
21
|
-
subgraphManifest: string;
|
|
22
|
-
subgraph?: string;
|
|
23
|
-
protocol: Protocol;
|
|
24
|
-
outputDir: string;
|
|
25
|
-
skipMigrations?: boolean;
|
|
26
|
-
uncrashable: any;
|
|
27
|
-
uncrashableConfig: boolean;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export default class TypeGenerator {
|
|
31
|
-
private sourceDir: string;
|
|
32
|
-
private options: TypeGeneratorOptions;
|
|
33
|
-
private protocol: Protocol;
|
|
34
|
-
private protocolTypeGenerator: any;
|
|
35
|
-
|
|
36
|
-
constructor(options: TypeGeneratorOptions) {
|
|
37
|
-
this.options = options;
|
|
38
|
-
this.sourceDir =
|
|
39
|
-
this.options.sourceDir ||
|
|
40
|
-
(this.options.subgraphManifest && path.dirname(this.options.subgraphManifest));
|
|
41
|
-
|
|
42
|
-
this.protocol = this.options.protocol;
|
|
43
|
-
this.protocolTypeGenerator = this.protocol?.getTypeGenerator?.({
|
|
44
|
-
sourceDir: this.sourceDir,
|
|
45
|
-
outputDir: this.options.outputDir,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
process.on('uncaughtException', e => {
|
|
49
|
-
toolbox.print.error(`UNCAUGHT EXCEPTION: ${e}`);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async generateTypes() {
|
|
54
|
-
try {
|
|
55
|
-
if (!this.options.skipMigrations && this.options.subgraphManifest) {
|
|
56
|
-
await applyMigrations({
|
|
57
|
-
sourceDir: this.sourceDir,
|
|
58
|
-
manifestFile: this.options.subgraphManifest,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
const subgraph = await this.loadSubgraph();
|
|
62
|
-
|
|
63
|
-
// Not all protocols support/have ABIs.
|
|
64
|
-
if (this.protocol.hasABIs()) {
|
|
65
|
-
const abis = await this.protocolTypeGenerator.loadABIs(subgraph);
|
|
66
|
-
await this.protocolTypeGenerator.generateTypesForABIs(abis);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
await this.generateTypesForDataSourceTemplates(subgraph);
|
|
70
|
-
|
|
71
|
-
// Not all protocols support/have ABIs.
|
|
72
|
-
if (this.protocol.hasABIs()) {
|
|
73
|
-
const templateAbis = await this.protocolTypeGenerator.loadDataSourceTemplateABIs(subgraph);
|
|
74
|
-
await this.protocolTypeGenerator.generateTypesForDataSourceTemplateABIs(templateAbis);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const schema = await this.loadSchema(subgraph);
|
|
78
|
-
await this.generateTypesForSchema(schema);
|
|
79
|
-
|
|
80
|
-
toolbox.print.success('\nTypes generated successfully\n');
|
|
81
|
-
|
|
82
|
-
if (this.options.uncrashable && this.options.uncrashableConfig) {
|
|
83
|
-
await this.generateUncrashableEntities(schema);
|
|
84
|
-
toolbox.print.success('\nUncrashable Helpers generated successfully\n');
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
} catch (e) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async generateUncrashableEntities(graphSchema: any) {
|
|
93
|
-
const ast = graphql.parse(graphSchema.document);
|
|
94
|
-
const entityDefinitions = ast['definitions'];
|
|
95
|
-
return await withSpinner(
|
|
96
|
-
`Generate Uncrashable Entity Helpers`,
|
|
97
|
-
`Failed to generate Uncrashable Entity Helpers`,
|
|
98
|
-
`Warnings while generating Uncrashable Entity Helpers`,
|
|
99
|
-
async spinner => {
|
|
100
|
-
uncrashable.run(entityDefinitions, this.options.uncrashableConfig, this.options.outputDir);
|
|
101
|
-
const outputFile = path.join(this.options.outputDir, 'UncrashableEntityHelpers.ts');
|
|
102
|
-
step(spinner, 'Save uncrashable entities to', displayPath(outputFile));
|
|
103
|
-
},
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async loadSubgraph({ quiet } = { quiet: false }) {
|
|
108
|
-
const subgraphLoadOptions = { protocol: this.protocol, skipValidation: false };
|
|
109
|
-
|
|
110
|
-
if (quiet) {
|
|
111
|
-
return this.options.subgraph
|
|
112
|
-
? this.options.subgraph
|
|
113
|
-
: (await Subgraph.load(this.options.subgraphManifest, subgraphLoadOptions)).result;
|
|
114
|
-
}
|
|
115
|
-
const manifestPath = displayPath(this.options.subgraphManifest);
|
|
116
|
-
|
|
117
|
-
return await withSpinner(
|
|
118
|
-
`Load subgraph from ${manifestPath}`,
|
|
119
|
-
`Failed to load subgraph from ${manifestPath}`,
|
|
120
|
-
`Warnings while loading subgraph from ${manifestPath}`,
|
|
121
|
-
async _spinner => {
|
|
122
|
-
return this.options.subgraph
|
|
123
|
-
? this.options.subgraph
|
|
124
|
-
: Subgraph.load(this.options.subgraphManifest, subgraphLoadOptions);
|
|
125
|
-
},
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async loadSchema(subgraph: immutable.Map<any, any>) {
|
|
130
|
-
const maybeRelativePath = subgraph.getIn(['schema', 'file']) as string;
|
|
131
|
-
const absolutePath = path.resolve(this.sourceDir, maybeRelativePath);
|
|
132
|
-
return await withSpinner(
|
|
133
|
-
`Load GraphQL schema from ${displayPath(absolutePath)}`,
|
|
134
|
-
`Failed to load GraphQL schema from ${displayPath(absolutePath)}`,
|
|
135
|
-
`Warnings while loading GraphQL schema from ${displayPath(absolutePath)}`,
|
|
136
|
-
async _spinner => {
|
|
137
|
-
const absolutePath = path.resolve(this.sourceDir, maybeRelativePath);
|
|
138
|
-
return Schema.load(absolutePath);
|
|
139
|
-
},
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async generateTypesForSchema(schema: any) {
|
|
144
|
-
return await withSpinner(
|
|
145
|
-
`Generate types for GraphQL schema`,
|
|
146
|
-
`Failed to generate types for GraphQL schema`,
|
|
147
|
-
`Warnings while generating types for GraphQL schema`,
|
|
148
|
-
async spinner => {
|
|
149
|
-
// Generate TypeScript module from schema
|
|
150
|
-
const codeGenerator = schema.codeGenerator();
|
|
151
|
-
const code = prettier.format(
|
|
152
|
-
[
|
|
153
|
-
GENERATED_FILE_NOTE,
|
|
154
|
-
...codeGenerator.generateModuleImports(),
|
|
155
|
-
...codeGenerator.generateTypes(),
|
|
156
|
-
].join('\n'),
|
|
157
|
-
{
|
|
158
|
-
parser: 'typescript',
|
|
159
|
-
},
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
const outputFile = path.join(this.options.outputDir, 'schema.ts');
|
|
163
|
-
step(spinner, 'Write types to', displayPath(outputFile));
|
|
164
|
-
await fs.mkdirs(path.dirname(outputFile));
|
|
165
|
-
await fs.writeFile(outputFile, code);
|
|
166
|
-
},
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
async generateTypesForDataSourceTemplates(subgraph: immutable.Map<any, any>) {
|
|
171
|
-
return await withSpinner(
|
|
172
|
-
`Generate types for data source templates`,
|
|
173
|
-
`Failed to generate types for data source templates`,
|
|
174
|
-
`Warnings while generating types for data source templates`,
|
|
175
|
-
async spinner => {
|
|
176
|
-
// Combine the generated code for all templates
|
|
177
|
-
const codeSegments = subgraph
|
|
178
|
-
.get('templates', immutable.List())
|
|
179
|
-
.reduce((codeSegments: any, template: any) => {
|
|
180
|
-
step(spinner, 'Generate types for data source template', String(template.get('name')));
|
|
181
|
-
|
|
182
|
-
const codeGenerator = new DataSourceTemplateCodeGenerator(template, this.protocol);
|
|
183
|
-
|
|
184
|
-
// Only generate module imports once, because they are identical for
|
|
185
|
-
// all types generated for data source templates.
|
|
186
|
-
if (codeSegments.isEmpty()) {
|
|
187
|
-
codeSegments = codeSegments.concat(codeGenerator.generateModuleImports());
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return codeSegments.concat(codeGenerator.generateTypes());
|
|
191
|
-
}, immutable.List());
|
|
192
|
-
|
|
193
|
-
if (!codeSegments.isEmpty()) {
|
|
194
|
-
const code = prettier.format([GENERATED_FILE_NOTE, ...codeSegments].join('\n'), {
|
|
195
|
-
parser: 'typescript',
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
const outputFile = path.join(this.options.outputDir, 'templates.ts');
|
|
199
|
-
step(spinner, `Write types for templates to`, displayPath(outputFile));
|
|
200
|
-
await fs.mkdirs(path.dirname(outputFile));
|
|
201
|
-
await fs.writeFile(outputFile, code);
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
async getFilesToWatch() {
|
|
208
|
-
try {
|
|
209
|
-
const files = [];
|
|
210
|
-
const subgraph = await this.loadSubgraph({ quiet: true });
|
|
211
|
-
|
|
212
|
-
// Add the subgraph manifest file
|
|
213
|
-
files.push(this.options.subgraphManifest);
|
|
214
|
-
|
|
215
|
-
// Add the GraphQL schema to the watched files
|
|
216
|
-
files.push(subgraph.getIn(['schema', 'file']));
|
|
217
|
-
|
|
218
|
-
// Add all file paths specified in manifest
|
|
219
|
-
subgraph.get('dataSources').map((dataSource: any) => {
|
|
220
|
-
dataSource.getIn(['mapping', 'abis']).map((abi: any) => {
|
|
221
|
-
files.push(abi.get('file'));
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
// Make paths absolute
|
|
226
|
-
return files.map(file => path.resolve(file));
|
|
227
|
-
} catch (e) {
|
|
228
|
-
throw Error(`Failed to load subgraph: ${e.message}`);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
async watchAndGenerateTypes() {
|
|
233
|
-
const generator = this;
|
|
234
|
-
let spinner: Spinner;
|
|
235
|
-
|
|
236
|
-
// Create watcher and generate types once and then on every change to a watched file
|
|
237
|
-
const watcher = new Watcher({
|
|
238
|
-
onReady: () => (spinner = toolbox.print.spin('Watching subgraph files')),
|
|
239
|
-
onTrigger: async changedFile => {
|
|
240
|
-
if (changedFile !== undefined) {
|
|
241
|
-
spinner.info(`File change detected: ${displayPath(changedFile)}\n`);
|
|
242
|
-
}
|
|
243
|
-
await generator.generateTypes();
|
|
244
|
-
spinner.start();
|
|
245
|
-
},
|
|
246
|
-
onCollectFiles: async () => await generator.getFilesToWatch(),
|
|
247
|
-
onError: error => {
|
|
248
|
-
spinner.stop();
|
|
249
|
-
toolbox.print.error(`${error}\n`);
|
|
250
|
-
spinner.start();
|
|
251
|
-
},
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
// Catch keyboard interrupt: close watcher and exit process
|
|
255
|
-
process.on('SIGINT', () => {
|
|
256
|
-
watcher.close();
|
|
257
|
-
process.exit();
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
try {
|
|
261
|
-
await watcher.watch();
|
|
262
|
-
} catch (e) {
|
|
263
|
-
toolbox.print.error(String(e.message));
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import immutable from 'immutable';
|
|
2
|
-
import Protocol from '../protocols';
|
|
3
|
-
import { ContractCtor } from '../protocols/contract';
|
|
4
|
-
|
|
5
|
-
export const validateContract = (value: string, ProtocolContract: ContractCtor) => {
|
|
6
|
-
const contract = new ProtocolContract(value);
|
|
7
|
-
|
|
8
|
-
const { valid, error } = contract.validate();
|
|
9
|
-
|
|
10
|
-
if (!valid) {
|
|
11
|
-
return {
|
|
12
|
-
valid,
|
|
13
|
-
error: `Contract ${ProtocolContract.identifierName()} is invalid: ${value}\n${error}`,
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return { valid, error };
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const validateContractValues = (manifest: immutable.Map<any, any>, protocol: Protocol) => {
|
|
21
|
-
const ProtocolContract = protocol.getContract()!;
|
|
22
|
-
|
|
23
|
-
const fieldName = ProtocolContract.identifierName();
|
|
24
|
-
|
|
25
|
-
return manifest
|
|
26
|
-
.get('dataSources')
|
|
27
|
-
.filter((dataSource: any) => protocol.isValidKindName(dataSource.get('kind')))
|
|
28
|
-
.reduce((errors: any[], dataSource: any, dataSourceIndex: number) => {
|
|
29
|
-
const path = ['dataSources', dataSourceIndex, 'source', fieldName];
|
|
30
|
-
|
|
31
|
-
// No need to validate if the source has no contract field
|
|
32
|
-
if (!dataSource.get('source').has(fieldName)) {
|
|
33
|
-
return errors;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const contractValue = dataSource.getIn(['source', fieldName]);
|
|
37
|
-
|
|
38
|
-
const { valid, error } = validateContract(contractValue, ProtocolContract);
|
|
39
|
-
|
|
40
|
-
// Validate whether the contract is valid for the protocol
|
|
41
|
-
if (valid) {
|
|
42
|
-
return errors;
|
|
43
|
-
}
|
|
44
|
-
return errors.push(
|
|
45
|
-
immutable.fromJS({
|
|
46
|
-
path,
|
|
47
|
-
message: error,
|
|
48
|
-
}),
|
|
49
|
-
);
|
|
50
|
-
}, immutable.List());
|
|
51
|
-
};
|
package/src/validation/index.ts
DELETED
|
@@ -1,360 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import immutable from 'immutable';
|
|
4
|
-
import yaml from 'js-yaml';
|
|
5
|
-
import Protocol from '../protocols';
|
|
6
|
-
|
|
7
|
-
const List = immutable.List;
|
|
8
|
-
const Map = immutable.Map;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Returns a user-friendly type name for a value.
|
|
12
|
-
*/
|
|
13
|
-
const typeName = (value: immutable.Collection<unknown, unknown>) =>
|
|
14
|
-
List.isList(value) ? 'list' : Map.isMap(value) ? 'map' : typeof value;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Converts an immutable or plain JavaScript value to a YAML string.
|
|
18
|
-
*/
|
|
19
|
-
const toYAML = (x: immutable.Collection<unknown, unknown>) =>
|
|
20
|
-
yaml
|
|
21
|
-
.safeDump(typeName(x) === 'list' || typeName(x) === 'map' ? x.toJS() : x, {
|
|
22
|
-
indent: 2,
|
|
23
|
-
})
|
|
24
|
-
.trim();
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Looks up the type of a field in a GraphQL object type.
|
|
28
|
-
*/
|
|
29
|
-
const getFieldType = (type: immutable.Map<any, any>, fieldName: string) => {
|
|
30
|
-
const fieldDef = type
|
|
31
|
-
.get('fields')
|
|
32
|
-
.find((field: any) => field.getIn(['name', 'value']) === fieldName);
|
|
33
|
-
|
|
34
|
-
return fieldDef !== undefined ? fieldDef.get('type') : undefined;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Resolves a type in the GraphQL schema.
|
|
39
|
-
*/
|
|
40
|
-
const resolveType = (schema: immutable.Map<any, any>, type: immutable.Map<any, any>): any =>
|
|
41
|
-
type.has('type')
|
|
42
|
-
? resolveType(schema, type.get('type'))
|
|
43
|
-
: type.get('kind') === 'NamedType'
|
|
44
|
-
? schema
|
|
45
|
-
.get('definitions')
|
|
46
|
-
.find((def: any) => def.getIn(['name', 'value']) === type.getIn(['name', 'value']))
|
|
47
|
-
: 'resolveType: unimplemented';
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* A map of supported validators.
|
|
51
|
-
*/
|
|
52
|
-
const validators = immutable.fromJS({
|
|
53
|
-
ScalarTypeDefinition: (value: any, ctx: immutable.Map<any, any>) =>
|
|
54
|
-
(validators.get(ctx.getIn(['type', 'name', 'value'])) as any)(value, ctx),
|
|
55
|
-
|
|
56
|
-
UnionTypeDefinition: (value: any, ctx: immutable.Map<any, any>) => {
|
|
57
|
-
const unionVariants = ctx.getIn(['type', 'types']) as any[];
|
|
58
|
-
|
|
59
|
-
let errors = List();
|
|
60
|
-
|
|
61
|
-
for (const variantType of unionVariants) {
|
|
62
|
-
const variantErrors = validateValue(value, ctx.set('type', variantType));
|
|
63
|
-
// No errors found, union variant matched, early return
|
|
64
|
-
if (variantErrors.isEmpty()) {
|
|
65
|
-
return List();
|
|
66
|
-
}
|
|
67
|
-
errors = errors.push(variantErrors);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Return errors from variant that matched the most
|
|
71
|
-
return List(errors.minBy((variantErrors: any) => variantErrors.count()) as any);
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
NamedType: (value: any, ctx: immutable.Map<any, any>) =>
|
|
75
|
-
validateValue(
|
|
76
|
-
value,
|
|
77
|
-
ctx.update('type', type => resolveType(ctx.get('schema'), type)),
|
|
78
|
-
),
|
|
79
|
-
|
|
80
|
-
NonNullType: (value: any, ctx: immutable.Map<any, any>) =>
|
|
81
|
-
value !== null && value !== undefined
|
|
82
|
-
? validateValue(
|
|
83
|
-
value,
|
|
84
|
-
ctx.update('type', type => type.get('type')),
|
|
85
|
-
)
|
|
86
|
-
: immutable.fromJS([
|
|
87
|
-
{
|
|
88
|
-
path: ctx.get('path'),
|
|
89
|
-
message: `No value provided`,
|
|
90
|
-
},
|
|
91
|
-
]),
|
|
92
|
-
|
|
93
|
-
ListType: (value: any, ctx: immutable.Map<any, any>) =>
|
|
94
|
-
List.isList(value)
|
|
95
|
-
? value.reduce(
|
|
96
|
-
(errors, value, i) =>
|
|
97
|
-
errors.concat(
|
|
98
|
-
validateValue(
|
|
99
|
-
value,
|
|
100
|
-
ctx.update('path', path => path.push(i)).update('type', type => type.get('type')),
|
|
101
|
-
),
|
|
102
|
-
),
|
|
103
|
-
List(),
|
|
104
|
-
)
|
|
105
|
-
: immutable.fromJS([
|
|
106
|
-
{
|
|
107
|
-
path: ctx.get('path'),
|
|
108
|
-
message: `Expected list, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
109
|
-
},
|
|
110
|
-
]),
|
|
111
|
-
|
|
112
|
-
ObjectTypeDefinition: (value: any, ctx: any) => {
|
|
113
|
-
return Map.isMap(value)
|
|
114
|
-
? ctx
|
|
115
|
-
.getIn(['type', 'fields'])
|
|
116
|
-
.map((fieldDef: any) => fieldDef.getIn(['name', 'value']))
|
|
117
|
-
.concat(value.keySeq())
|
|
118
|
-
.toSet()
|
|
119
|
-
.reduce(
|
|
120
|
-
(errors: any[], key: string) =>
|
|
121
|
-
getFieldType(ctx.get('type'), key)
|
|
122
|
-
? errors.concat(
|
|
123
|
-
validateValue(
|
|
124
|
-
value.get(key),
|
|
125
|
-
ctx
|
|
126
|
-
.update('path', (path: string[]) => path.push(key))
|
|
127
|
-
.set('type', getFieldType(ctx.get('type'), key)),
|
|
128
|
-
),
|
|
129
|
-
)
|
|
130
|
-
: errors.push(
|
|
131
|
-
key == 'templates' && ctx.get('protocol').hasTemplates()
|
|
132
|
-
? immutable.fromJS({
|
|
133
|
-
path: ctx.get('path'),
|
|
134
|
-
message:
|
|
135
|
-
`The way to declare data source templates has changed, ` +
|
|
136
|
-
`please move the templates from inside data sources to ` +
|
|
137
|
-
`a \`templates:\` field at the top level of the manifest.`,
|
|
138
|
-
})
|
|
139
|
-
: immutable.fromJS({
|
|
140
|
-
path: ctx.get('path'),
|
|
141
|
-
message: `Unexpected key in map: ${key}`,
|
|
142
|
-
}),
|
|
143
|
-
),
|
|
144
|
-
List(),
|
|
145
|
-
)
|
|
146
|
-
: immutable.fromJS([
|
|
147
|
-
{
|
|
148
|
-
path: ctx.get('path'),
|
|
149
|
-
message: `Expected map, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
150
|
-
},
|
|
151
|
-
]);
|
|
152
|
-
},
|
|
153
|
-
|
|
154
|
-
EnumTypeDefinition: (value: any, ctx: immutable.Map<any, any>) => {
|
|
155
|
-
const enumValues = (ctx.getIn(['type', 'values']) as any).map((v: any) => {
|
|
156
|
-
return v.getIn(['name', 'value']);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
const allowedValues = enumValues.toArray().join(', ');
|
|
160
|
-
|
|
161
|
-
return enumValues.includes(value)
|
|
162
|
-
? List()
|
|
163
|
-
: immutable.fromJS([
|
|
164
|
-
{
|
|
165
|
-
path: ctx.get('path'),
|
|
166
|
-
message: `Unexpected enum value: ${value}, allowed values: ${allowedValues}`,
|
|
167
|
-
},
|
|
168
|
-
]);
|
|
169
|
-
},
|
|
170
|
-
|
|
171
|
-
String: (value: any, ctx: immutable.Map<any, any>) =>
|
|
172
|
-
typeof value === 'string'
|
|
173
|
-
? List()
|
|
174
|
-
: immutable.fromJS([
|
|
175
|
-
{
|
|
176
|
-
path: ctx.get('path'),
|
|
177
|
-
message: `Expected string, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
178
|
-
},
|
|
179
|
-
]),
|
|
180
|
-
|
|
181
|
-
BigInt: (value: any, ctx: immutable.Map<any, any>) =>
|
|
182
|
-
typeof value === 'number'
|
|
183
|
-
? List()
|
|
184
|
-
: immutable.fromJS([
|
|
185
|
-
{
|
|
186
|
-
path: ctx.get('path'),
|
|
187
|
-
message: `Expected BigInt, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
188
|
-
},
|
|
189
|
-
]),
|
|
190
|
-
|
|
191
|
-
File: (value: any, ctx: immutable.Map<any, any>) =>
|
|
192
|
-
typeof value === 'string'
|
|
193
|
-
? fs.existsSync(ctx.get('resolveFile')(value))
|
|
194
|
-
? List()
|
|
195
|
-
: immutable.fromJS([
|
|
196
|
-
{
|
|
197
|
-
path: ctx.get('path'),
|
|
198
|
-
message: `File does not exist: ${path.relative(process.cwd(), value)}`,
|
|
199
|
-
},
|
|
200
|
-
])
|
|
201
|
-
: immutable.fromJS([
|
|
202
|
-
{
|
|
203
|
-
path: ctx.get('path'),
|
|
204
|
-
message: `Expected filename, found ${typeName(value)}:\n${value}`,
|
|
205
|
-
},
|
|
206
|
-
]),
|
|
207
|
-
|
|
208
|
-
Boolean: (value: any, ctx: immutable.Map<any, any>) =>
|
|
209
|
-
typeof value === 'boolean'
|
|
210
|
-
? List()
|
|
211
|
-
: immutable.fromJS([
|
|
212
|
-
{
|
|
213
|
-
path: ctx.get('path'),
|
|
214
|
-
message: `Expected true or false, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
215
|
-
},
|
|
216
|
-
]),
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
const validateValue = (value: any, ctx: immutable.Collection<any, any>) => {
|
|
220
|
-
const kind = ctx.getIn(['type', 'kind']);
|
|
221
|
-
const validator = validators.get(kind) as any;
|
|
222
|
-
|
|
223
|
-
if (validator !== undefined) {
|
|
224
|
-
// If the type is nullable, accept undefined and null; if the nullable
|
|
225
|
-
// type is wrapped in a `NonNullType`, the validator for that `NonNullType`
|
|
226
|
-
// will catch the missing/unset value
|
|
227
|
-
if (kind !== 'NonNullType' && (value === undefined || value === null)) {
|
|
228
|
-
return List();
|
|
229
|
-
}
|
|
230
|
-
return validator(value, ctx);
|
|
231
|
-
}
|
|
232
|
-
return immutable.fromJS([
|
|
233
|
-
{
|
|
234
|
-
path: ctx.get('path'),
|
|
235
|
-
message: `No validator for unsupported schema type: ${kind}`,
|
|
236
|
-
},
|
|
237
|
-
]);
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
// Transforms list of data sources like this:
|
|
241
|
-
// [
|
|
242
|
-
// { name: 'contract0', kind: 'ethereum/contract', network: 'mainnet' },
|
|
243
|
-
// { name: 'contract1', kind: 'ethereum', network: 'mainnet' },
|
|
244
|
-
// { name: 'contract2', kind: 'ethereum/contract', network: 'gnosis' },
|
|
245
|
-
// { name: 'contract3', kind: 'near', network: 'near-mainnet' },
|
|
246
|
-
// ]
|
|
247
|
-
//
|
|
248
|
-
// Into Immutable JS structure like this (protocol kind is normalized):
|
|
249
|
-
// {
|
|
250
|
-
// ethereum: {
|
|
251
|
-
// mainnet: ['contract0', 'contract1'],
|
|
252
|
-
// gnosis: ['contract2'],
|
|
253
|
-
// },
|
|
254
|
-
// near: {
|
|
255
|
-
// 'near-mainnet': ['contract3'],
|
|
256
|
-
// },
|
|
257
|
-
// }
|
|
258
|
-
const dataSourceListToMap = (dataSources: any[]) =>
|
|
259
|
-
dataSources.reduce(
|
|
260
|
-
(protocolKinds, dataSource) =>
|
|
261
|
-
protocolKinds.update(Protocol.normalizeName(dataSource.kind), (networks: any) =>
|
|
262
|
-
(networks || immutable.OrderedMap()).update(dataSource.network, (dataSourceNames: any) =>
|
|
263
|
-
(dataSourceNames || immutable.OrderedSet()).add(dataSource.name),
|
|
264
|
-
),
|
|
265
|
-
),
|
|
266
|
-
immutable.OrderedMap(),
|
|
267
|
-
);
|
|
268
|
-
|
|
269
|
-
const validateDataSourceProtocolAndNetworks = (value: any) => {
|
|
270
|
-
const dataSources = [...value.dataSources, ...(value.templates || [])];
|
|
271
|
-
|
|
272
|
-
const protocolNetworkMap = dataSourceListToMap(dataSources);
|
|
273
|
-
|
|
274
|
-
if (protocolNetworkMap.size > 1) {
|
|
275
|
-
return immutable.fromJS([
|
|
276
|
-
{
|
|
277
|
-
path: [],
|
|
278
|
-
message: `Conflicting protocol kinds used in data sources and templates:
|
|
279
|
-
${protocolNetworkMap
|
|
280
|
-
.map(
|
|
281
|
-
(dataSourceNames: any, protocolKind: string | undefined) =>
|
|
282
|
-
` ${
|
|
283
|
-
protocolKind === undefined
|
|
284
|
-
? 'Data sources and templates having no protocol kind set'
|
|
285
|
-
: `Data sources and templates using '${protocolKind}'`
|
|
286
|
-
}:\n${dataSourceNames
|
|
287
|
-
.valueSeq()
|
|
288
|
-
.flatten()
|
|
289
|
-
.map((ds: string) => ` - ${ds}`)
|
|
290
|
-
.join('\n')}`,
|
|
291
|
-
)
|
|
292
|
-
.join('\n')}
|
|
293
|
-
Recommendation: Make all data sources and templates use the same protocol kind.`,
|
|
294
|
-
},
|
|
295
|
-
]);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
const networks = protocolNetworkMap.first();
|
|
299
|
-
|
|
300
|
-
if (networks.size > 1) {
|
|
301
|
-
return immutable.fromJS([
|
|
302
|
-
{
|
|
303
|
-
path: [],
|
|
304
|
-
message: `Conflicting networks used in data sources and templates:
|
|
305
|
-
${networks
|
|
306
|
-
.map(
|
|
307
|
-
(dataSources: any, network: string | undefined) =>
|
|
308
|
-
` ${
|
|
309
|
-
network === undefined
|
|
310
|
-
? 'Data sources and templates having no network set'
|
|
311
|
-
: `Data sources and templates using '${network}'`
|
|
312
|
-
}:\n${dataSources.map((ds: string) => ` - ${ds}`).join('\n')}`,
|
|
313
|
-
)
|
|
314
|
-
.join('\n')}
|
|
315
|
-
Recommendation: Make all data sources and templates use the same network name.`,
|
|
316
|
-
},
|
|
317
|
-
]);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
return List();
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
export const validateManifest = (
|
|
324
|
-
value: any,
|
|
325
|
-
type: any,
|
|
326
|
-
schema: any,
|
|
327
|
-
protocol: Protocol,
|
|
328
|
-
{ resolveFile }: { resolveFile: (path: string) => string },
|
|
329
|
-
) => {
|
|
330
|
-
// Validate manifest using the GraphQL schema that defines its structure
|
|
331
|
-
const errors =
|
|
332
|
-
value !== null && value !== undefined
|
|
333
|
-
? validateValue(
|
|
334
|
-
immutable.fromJS(value),
|
|
335
|
-
immutable.fromJS({
|
|
336
|
-
schema,
|
|
337
|
-
type,
|
|
338
|
-
path: [],
|
|
339
|
-
errors: [],
|
|
340
|
-
resolveFile,
|
|
341
|
-
protocol,
|
|
342
|
-
}),
|
|
343
|
-
)
|
|
344
|
-
: immutable.fromJS([
|
|
345
|
-
{
|
|
346
|
-
path: [],
|
|
347
|
-
message: `Expected non-empty value, found ${typeName(value)}:\n ${value}`,
|
|
348
|
-
},
|
|
349
|
-
]);
|
|
350
|
-
|
|
351
|
-
// Fail early because a broken manifest prevents us from performing
|
|
352
|
-
// additional validation steps
|
|
353
|
-
if (!errors.isEmpty()) {
|
|
354
|
-
return errors;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// Validate that all data sources are for the same `network` and `protocol` (kind)
|
|
358
|
-
// (this includes _no_ network/protocol at all)
|
|
359
|
-
return validateDataSourceProtocolAndNetworks(value);
|
|
360
|
-
};
|