@graphprotocol/graph-cli 0.37.2 → 0.37.3-alpha-20230110160324-f11ef4f
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 +7 -0
- package/babel.config.js +3 -0
- package/dist/bin.js +6 -0
- package/dist/cli.js +32 -0
- package/dist/codegen/schema.js +250 -0
- package/dist/codegen/schema.test.js +458 -0
- package/dist/codegen/template.js +65 -0
- package/dist/codegen/types/conversions.js +359 -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 +46 -0
- package/dist/codegen/util.test.js +93 -0
- package/dist/command-helpers/abi.js +78 -0
- package/dist/command-helpers/auth.js +77 -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 +49 -0
- package/dist/command-helpers/ipfs.js +5 -0
- package/dist/command-helpers/jsonrpc.js +29 -0
- package/dist/command-helpers/network.js +93 -0
- package/dist/command-helpers/network.test.js +86 -0
- package/dist/command-helpers/node.js +40 -0
- package/dist/command-helpers/scaffold.js +112 -0
- package/dist/command-helpers/spinner.js +84 -0
- package/dist/command-helpers/studio.js +13 -0
- package/dist/command-helpers/studio.test.js +41 -0
- package/dist/command-helpers/subgraph.js +24 -0
- package/dist/command-helpers/version.js +74 -0
- package/dist/command-helpers/version.test.js +179 -0
- package/dist/commands/add.js +190 -0
- package/dist/commands/auth.js +127 -0
- package/dist/commands/build.js +140 -0
- package/dist/commands/codegen.js +135 -0
- package/dist/commands/create.js +86 -0
- package/dist/commands/deploy.js +334 -0
- package/dist/commands/init.js +788 -0
- package/dist/commands/local.js +387 -0
- package/dist/commands/remove.js +86 -0
- package/dist/commands/test.js +295 -0
- package/dist/compiler/asc.js +83 -0
- package/dist/compiler/index.js +474 -0
- package/dist/debug.js +16 -0
- package/dist/migrations/mapping_api_version_0_0_1.js +78 -0
- package/dist/migrations/mapping_api_version_0_0_2.js +76 -0
- package/dist/migrations/mapping_api_version_0_0_3.js +78 -0
- package/dist/migrations/mapping_api_version_0_0_4.js +80 -0
- package/dist/migrations/mapping_api_version_0_0_5.js +80 -0
- package/dist/migrations/spec_version_0_0_2.js +49 -0
- package/dist/migrations/spec_version_0_0_3.js +54 -0
- package/dist/migrations/util/load-manifest.js +18 -0
- package/dist/migrations/util/versions.js +29 -0
- package/dist/migrations.js +56 -0
- package/dist/protocols/arweave/manifest.graphql +57 -0
- package/dist/protocols/arweave/scaffold/manifest.js +18 -0
- package/dist/protocols/arweave/scaffold/mapping.js +52 -0
- package/dist/protocols/arweave/subgraph.js +23 -0
- package/dist/protocols/cosmos/manifest.graphql +76 -0
- package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
- package/dist/protocols/cosmos/scaffold/mapping.js +38 -0
- package/dist/protocols/cosmos/subgraph.js +25 -0
- package/dist/protocols/ethereum/abi.js +140 -0
- package/dist/protocols/ethereum/codegen/abi.js +451 -0
- package/dist/protocols/ethereum/codegen/abi.test.js +328 -0
- package/dist/protocols/ethereum/codegen/template.js +50 -0
- package/dist/protocols/ethereum/contract.js +20 -0
- package/dist/protocols/ethereum/manifest.graphql +94 -0
- package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
- package/dist/protocols/ethereum/scaffold/mapping.js +65 -0
- package/dist/protocols/ethereum/subgraph.js +171 -0
- package/dist/protocols/ethereum/type-generator.js +125 -0
- package/dist/protocols/index.js +264 -0
- package/dist/protocols/ipfs/codegen/file_template.js +50 -0
- package/dist/protocols/near/contract.js +41 -0
- package/dist/protocols/near/manifest.graphql +64 -0
- package/dist/protocols/near/scaffold/manifest.js +16 -0
- package/dist/protocols/near/scaffold/mapping.js +38 -0
- package/dist/protocols/near/subgraph.js +20 -0
- package/dist/protocols/subgraph.js +2 -0
- package/dist/protocols/substreams/manifest.graphql +45 -0
- package/dist/protocols/substreams/scaffold/manifest.js +12 -0
- package/dist/protocols/substreams/subgraph.js +20 -0
- package/dist/scaffold/cosmos.test.js +83 -0
- package/dist/scaffold/ethereum.test.js +505 -0
- package/dist/scaffold/index.js +128 -0
- package/dist/scaffold/mapping.js +61 -0
- package/dist/scaffold/near.test.js +86 -0
- package/dist/scaffold/schema.js +83 -0
- package/dist/scaffold/tests.js +175 -0
- package/dist/schema.js +54 -0
- package/dist/subgraph.js +251 -0
- package/dist/type-generator.js +224 -0
- package/dist/validation/contract.js +46 -0
- package/dist/validation/index.js +8 -0
- package/dist/validation/manifest.js +271 -0
- package/dist/validation/schema.js +796 -0
- package/dist/validation/schema.test.js +35 -0
- package/dist/watcher.js +79 -0
- package/jest.config.js +1 -1
- package/package.json +16 -9
- package/{bin/graph → src/bin.ts} +2 -1
- package/src/cli.ts +36 -0
- package/src/codegen/schema.js +23 -22
- package/src/codegen/schema.test.js +33 -27
- package/src/codegen/{template.js → template.ts} +8 -6
- package/src/codegen/types/conversions.ts +382 -0
- package/src/codegen/types/index.js +4 -7
- package/src/codegen/types/index.test.js +10 -4
- package/src/codegen/{typescript.js → typescript.ts} +44 -26
- package/src/codegen/util.js +1 -1
- package/src/codegen/util.test.js +5 -2
- package/src/command-helpers/abi.js +46 -30
- package/src/command-helpers/auth.js +6 -9
- package/src/command-helpers/compiler.js +4 -6
- package/src/command-helpers/data-sources.js +8 -11
- package/src/command-helpers/fs.ts +5 -0
- package/src/command-helpers/gluegun.js +2 -4
- package/src/command-helpers/ipfs.ts +3 -0
- package/src/command-helpers/{jsonrpc.js → jsonrpc.ts} +11 -16
- package/src/command-helpers/network.js +58 -44
- package/src/command-helpers/network.test.js +23 -19
- package/src/command-helpers/node.js +3 -7
- package/src/command-helpers/scaffold.js +34 -25
- package/src/command-helpers/{spinner.js → spinner.ts} +10 -10
- package/src/command-helpers/studio.test.js +61 -30
- package/src/command-helpers/studio.ts +20 -0
- package/src/command-helpers/{subgraph.js → subgraph.ts} +6 -6
- package/src/command-helpers/version.js +11 -16
- package/src/command-helpers/version.test.js +111 -82
- package/src/commands/add.js +49 -35
- package/src/commands/auth.js +13 -25
- package/src/commands/build.js +9 -9
- package/src/commands/codegen.js +12 -12
- package/src/commands/create.js +22 -22
- package/src/commands/deploy.js +39 -34
- package/src/commands/init.js +21 -24
- package/src/commands/local.js +15 -14
- package/src/commands/remove.js +22 -22
- package/src/commands/test.js +86 -70
- package/src/compiler/{asc.js → asc.ts} +29 -16
- package/src/compiler/index.js +18 -18
- package/src/{debug.js → debug.ts} +2 -2
- package/src/migrations/mapping_api_version_0_0_1.js +7 -7
- package/src/migrations/mapping_api_version_0_0_2.js +5 -5
- package/src/migrations/mapping_api_version_0_0_3.js +7 -7
- package/src/migrations/mapping_api_version_0_0_4.js +7 -7
- package/src/migrations/mapping_api_version_0_0_5.js +7 -7
- package/src/migrations/spec_version_0_0_2.js +5 -5
- package/src/migrations/spec_version_0_0_3.js +5 -5
- package/src/migrations/util/load-manifest.js +6 -9
- package/src/migrations/util/versions.js +5 -10
- package/src/{migrations.js → migrations.ts} +14 -15
- package/src/protocols/arweave/scaffold/manifest.js +1 -4
- package/src/protocols/arweave/scaffold/mapping.js +1 -3
- package/src/protocols/arweave/subgraph.ts +22 -0
- package/src/protocols/cosmos/scaffold/manifest.js +1 -4
- package/src/protocols/cosmos/scaffold/mapping.js +1 -3
- package/src/protocols/cosmos/subgraph.js +2 -2
- package/src/protocols/ethereum/abi.js +7 -13
- package/src/protocols/ethereum/codegen/abi.js +219 -222
- package/src/protocols/ethereum/codegen/abi.test.js +6 -6
- package/src/protocols/ethereum/codegen/template.js +3 -5
- package/src/protocols/ethereum/contract.js +3 -2
- package/src/protocols/ethereum/scaffold/manifest.js +4 -7
- package/src/protocols/ethereum/scaffold/mapping.js +4 -11
- package/src/protocols/ethereum/subgraph.js +21 -20
- package/src/protocols/ethereum/type-generator.js +24 -30
- package/src/protocols/index.js +26 -27
- package/src/protocols/ipfs/codegen/file_template.js +2 -2
- package/src/protocols/near/{contract.js → contract.ts} +16 -12
- package/src/protocols/near/scaffold/manifest.js +2 -5
- package/src/protocols/near/scaffold/mapping.js +1 -3
- package/src/protocols/near/subgraph.js +3 -6
- package/src/protocols/subgraph.ts +12 -0
- package/src/protocols/substreams/scaffold/manifest.js +1 -4
- package/src/protocols/substreams/subgraph.js +2 -2
- package/src/scaffold/cosmos.test.js +3 -3
- package/src/scaffold/ethereum.test.js +4 -4
- package/src/scaffold/index.js +7 -7
- package/src/scaffold/mapping.js +3 -3
- package/src/scaffold/near.test.js +3 -3
- package/src/scaffold/schema.js +4 -4
- package/src/scaffold/tests.js +72 -58
- package/src/schema.ts +26 -0
- package/src/{subgraph.js → subgraph.ts} +74 -53
- package/src/type-generator.js +20 -20
- package/src/validation/contract.js +2 -6
- package/src/validation/index.js +1 -1
- package/src/validation/manifest.js +24 -22
- package/src/validation/schema.test.js +1 -1
- package/src/validation/{schema.js → schema.ts} +268 -221
- package/src/{watcher.js → watcher.ts} +23 -12
- package/tests/cli/globalSetup.js +2 -2
- package/tests/cli/globalTeardown.js +2 -2
- package/tests/cli/init.test.js +2 -2
- package/tests/cli/util.js +7 -13
- package/tsconfig.json +18 -0
- package/src/cli.js +0 -38
- package/src/codegen/types/conversions.js +0 -329
- package/src/command-helpers/fs.js +0 -8
- package/src/command-helpers/ipfs.js +0 -6
- package/src/command-helpers/studio.js +0 -15
- package/src/protocols/arweave/subgraph.js +0 -20
- package/src/schema.js +0 -23
package/src/scaffold/tests.js
CHANGED
|
@@ -1,34 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import prettier from 'prettier'
|
|
2
|
+
import { strings } from 'gluegun'
|
|
3
|
+
import { ascTypeForEthereum, ethereumFromAsc } from '../codegen/types'
|
|
4
4
|
|
|
5
5
|
const VARIABLES_VALUES = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
i32: 123,
|
|
7
|
+
BigInt: 234,
|
|
8
|
+
Bytes: 1234567890,
|
|
9
|
+
Address: '0x0000000000000000000000000000000000000001',
|
|
10
|
+
string: 'Example string value',
|
|
11
|
+
bool: true,
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
const generateTestsFiles = (contract, events, indexEvents) => {
|
|
15
15
|
const eventsTypes = events
|
|
16
16
|
.flatMap(event =>
|
|
17
|
-
event
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
).filter(type => !type.startsWith("ethereum.") && !isNativeType(type))
|
|
17
|
+
event.inputs.map(input => {
|
|
18
|
+
// If the asc type is Array<T> we need to check if T is a native type or a custom graph-ts type
|
|
19
|
+
// If we don't do that we may miss a type that should be imported from graph-ts
|
|
20
|
+
const ascType = ascTypeForEthereum(input.type)
|
|
21
|
+
const inner = fetchArrayInnerType(ascType)
|
|
22
|
+
return inner ? inner[1] : ascType
|
|
23
|
+
}),
|
|
24
|
+
)
|
|
25
|
+
.filter(type => !type.startsWith('ethereum.') && !isNativeType(type))
|
|
27
26
|
const importTypes = [...new Set(eventsTypes)].join(', ')
|
|
28
27
|
|
|
29
28
|
return {
|
|
30
|
-
[`${strings.kebabCase(contract)}.test.ts`]: prettier.format(
|
|
31
|
-
|
|
29
|
+
[`${strings.kebabCase(contract)}.test.ts`]: prettier.format(
|
|
30
|
+
generateExampleTest(contract, events[0], indexEvents, importTypes),
|
|
31
|
+
{ parser: 'typescript', semi: false },
|
|
32
|
+
),
|
|
33
|
+
[`${strings.kebabCase(contract)}-utils.ts`]: prettier.format(
|
|
34
|
+
generateTestHelper(contract, events, importTypes),
|
|
35
|
+
{ parser: 'typescript', semi: false },
|
|
36
|
+
),
|
|
32
37
|
}
|
|
33
38
|
}
|
|
34
39
|
|
|
@@ -39,26 +44,28 @@ const generateTestsFiles = (contract, events, indexEvents) => {
|
|
|
39
44
|
let displayName = "Example string value"
|
|
40
45
|
let imageUrl = "Example string value"
|
|
41
46
|
*/
|
|
42
|
-
const generateArguments =
|
|
43
|
-
return eventInputs
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
const generateArguments = eventInputs => {
|
|
48
|
+
return eventInputs
|
|
49
|
+
.map((input, index) => {
|
|
50
|
+
let ascType = ascTypeForEthereum(input.type)
|
|
51
|
+
return `let ${input.name || `param${index}`} = ${assignValue(ascType, input.name)}`
|
|
52
|
+
})
|
|
53
|
+
.join('\n')
|
|
47
54
|
}
|
|
48
55
|
|
|
49
56
|
// Generates the value that will be assigned to a variable in generateArguments()
|
|
50
|
-
const assignValue =
|
|
57
|
+
const assignValue = type => {
|
|
51
58
|
switch (type) {
|
|
52
|
-
case
|
|
59
|
+
case 'string':
|
|
53
60
|
return `"${VARIABLES_VALUES[type]}"`
|
|
54
|
-
case
|
|
61
|
+
case 'BigInt':
|
|
55
62
|
return `BigInt.fromI32(${VARIABLES_VALUES[type]})`
|
|
56
|
-
case
|
|
63
|
+
case 'Address':
|
|
57
64
|
return `Address.fromString("${VARIABLES_VALUES[type]}")`
|
|
58
|
-
case
|
|
65
|
+
case 'Bytes':
|
|
59
66
|
return `Bytes.fromI32(${VARIABLES_VALUES[type]})`
|
|
60
67
|
case fetchArrayInnerType(type)?.input:
|
|
61
|
-
innerType = fetchArrayInnerType(type)[1]
|
|
68
|
+
const innerType = fetchArrayInnerType(type)[1]
|
|
62
69
|
return `[${assignValue(innerType)}]`
|
|
63
70
|
default:
|
|
64
71
|
let value = VARIABLES_VALUES[type]
|
|
@@ -75,20 +82,25 @@ const assignValue = (type) => {
|
|
|
75
82
|
"0x0000000000000000000000000000000000000001"
|
|
76
83
|
)
|
|
77
84
|
*/
|
|
78
|
-
const generateFieldsAssertions = (entity, eventInputs, indexEvents) =>
|
|
79
|
-
|
|
85
|
+
const generateFieldsAssertions = (entity, eventInputs, indexEvents) =>
|
|
86
|
+
eventInputs
|
|
87
|
+
.filter(input => input.name != 'id')
|
|
88
|
+
.map(
|
|
89
|
+
(input, index) =>
|
|
90
|
+
`assert.fieldEquals(
|
|
80
91
|
"${entity}",
|
|
81
|
-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a${indexEvents ?
|
|
92
|
+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a${indexEvents ? '-1' : ''}",
|
|
82
93
|
"${input.name || `param${index}`}",
|
|
83
94
|
"${expectedValue(ascTypeForEthereum(input.type))}"
|
|
84
|
-
)
|
|
85
|
-
)
|
|
95
|
+
)`,
|
|
96
|
+
)
|
|
97
|
+
.join('\n')
|
|
86
98
|
|
|
87
99
|
// Returns the expected value for a given type in generateFieldsAssertions()
|
|
88
100
|
const expectedValue = type => {
|
|
89
101
|
switch (type) {
|
|
90
102
|
case fetchArrayInnerType(type)?.input:
|
|
91
|
-
innerType = fetchArrayInnerType(type)[1]
|
|
103
|
+
const innerType = fetchArrayInnerType(type)[1]
|
|
92
104
|
return `[${expectedValue(innerType)}]`
|
|
93
105
|
default:
|
|
94
106
|
let value = VARIABLES_VALUES[type]
|
|
@@ -98,13 +110,9 @@ const expectedValue = type => {
|
|
|
98
110
|
|
|
99
111
|
// Checks if the type is a native AS type or should be imported from graph-ts
|
|
100
112
|
const isNativeType = type => {
|
|
101
|
-
let natives = [
|
|
102
|
-
/i32/,
|
|
103
|
-
/string/,
|
|
104
|
-
/boolean/
|
|
105
|
-
]
|
|
113
|
+
let natives = [/i32/, /string/, /boolean/]
|
|
106
114
|
|
|
107
|
-
return natives.some(rx => rx.test(type))
|
|
115
|
+
return natives.some(rx => rx.test(type))
|
|
108
116
|
}
|
|
109
117
|
|
|
110
118
|
const fetchArrayInnerType = type => type.match(/Array<(.*?)>/)
|
|
@@ -119,7 +127,9 @@ const generateExampleTest = (contract, event, indexEvents, importTypes) => {
|
|
|
119
127
|
import { assert, describe, test, clearStore, beforeAll, afterAll } from "matchstick-as/assembly/index"
|
|
120
128
|
import { ${importTypes} } from "@graphprotocol/graph-ts"
|
|
121
129
|
import { ${entity} } from "../generated/schema"
|
|
122
|
-
import { ${
|
|
130
|
+
import { ${
|
|
131
|
+
indexEvents ? `${eventName} as ${eventName}Event` : eventName
|
|
132
|
+
} } from "../generated/${contract}/${contract}"
|
|
123
133
|
import { handle${eventName} } from "../src/${strings.kebabCase(contract)}"
|
|
124
134
|
import { create${eventName}Event } from "./${strings.kebabCase(contract)}-utils"
|
|
125
135
|
|
|
@@ -130,7 +140,9 @@ const generateExampleTest = (contract, event, indexEvents, importTypes) => {
|
|
|
130
140
|
describe("Describe entity assertions", () => {
|
|
131
141
|
beforeAll(() => {
|
|
132
142
|
${generateArguments(eventInputs)}
|
|
133
|
-
let new${eventName}Event = create${eventName}Event(${eventInputs
|
|
143
|
+
let new${eventName}Event = create${eventName}Event(${eventInputs
|
|
144
|
+
.map((input, index) => input.name || `param${index}`)
|
|
145
|
+
.join(', ')});
|
|
134
146
|
handle${eventName}(new${eventName}Event)
|
|
135
147
|
})
|
|
136
148
|
|
|
@@ -161,21 +173,26 @@ const generateTestHelper = (contract, events, importTypes) => {
|
|
|
161
173
|
return `
|
|
162
174
|
import { newMockEvent } from 'matchstick-as';
|
|
163
175
|
import { ethereum, ${importTypes} } from '@graphprotocol/graph-ts';
|
|
164
|
-
import { ${eventsNames.join(
|
|
176
|
+
import { ${eventsNames.join(', ')} } from '../generated/${contract}/${contract}';
|
|
165
177
|
|
|
166
|
-
${generateMockedEvents(events).join(
|
|
178
|
+
${generateMockedEvents(events).join('\n')}`
|
|
167
179
|
}
|
|
168
180
|
|
|
169
181
|
const generateMockedEvents = events =>
|
|
170
|
-
events.reduce(
|
|
171
|
-
(acc, event) => acc.concat(generateMockedEvent(event)),
|
|
172
|
-
[],
|
|
173
|
-
)
|
|
182
|
+
events.reduce((acc, event) => acc.concat(generateMockedEvent(event)), [])
|
|
174
183
|
|
|
175
184
|
const generateMockedEvent = event => {
|
|
176
185
|
const varName = `${strings.camelCase(event._alias)}Event`
|
|
177
|
-
const fnArgs = event.inputs.map(
|
|
178
|
-
|
|
186
|
+
const fnArgs = event.inputs.map(
|
|
187
|
+
(input, index) =>
|
|
188
|
+
`${input.name || `param${index}`}: ${ascTypeForEthereum(input.type)}`,
|
|
189
|
+
)
|
|
190
|
+
const ascToEth = event.inputs.map(
|
|
191
|
+
(input, index) =>
|
|
192
|
+
`${varName}.parameters.push(new ethereum.EventParam("${
|
|
193
|
+
input.name || `param${index}`
|
|
194
|
+
}", ${ethereumFromAsc(input.name || `param${index}`, input.type)}))`,
|
|
195
|
+
)
|
|
179
196
|
|
|
180
197
|
return `
|
|
181
198
|
export function create${event._alias}Event(${fnArgs.join(', ')}): ${event._alias} {
|
|
@@ -188,9 +205,6 @@ const generateMockedEvent = event => {
|
|
|
188
205
|
return ${varName};
|
|
189
206
|
}
|
|
190
207
|
`
|
|
191
|
-
|
|
192
208
|
}
|
|
193
209
|
|
|
194
|
-
|
|
195
|
-
generateTestsFiles,
|
|
196
|
-
}
|
|
210
|
+
export { generateTestsFiles }
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
import * as graphql from 'graphql/language'
|
|
3
|
+
import immutable from 'immutable'
|
|
4
|
+
import SchemaCodeGenerator from './codegen/schema'
|
|
5
|
+
|
|
6
|
+
export default class Schema {
|
|
7
|
+
constructor(
|
|
8
|
+
public filename: string,
|
|
9
|
+
public document: string,
|
|
10
|
+
public ast: immutable.Collection<unknown, unknown>,
|
|
11
|
+
) {
|
|
12
|
+
this.filename = filename
|
|
13
|
+
this.document = document
|
|
14
|
+
this.ast = ast
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
codeGenerator() {
|
|
18
|
+
return new SchemaCodeGenerator(this)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static async load(filename: string) {
|
|
22
|
+
let document = await fs.readFile(filename, 'utf-8')
|
|
23
|
+
let ast = graphql.parse(document)
|
|
24
|
+
return new Schema(filename, document, immutable.fromJS(ast))
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
import immutable from 'immutable'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import yaml from 'yaml'
|
|
5
|
+
import { strOptions } from 'yaml/types'
|
|
6
|
+
import * as graphql from 'graphql/language'
|
|
7
|
+
import validation from './validation'
|
|
8
|
+
import debug from './debug'
|
|
9
|
+
import { Subgraph as ISubgraph } from './protocols/subgraph'
|
|
10
|
+
|
|
11
|
+
let subgraphDebug = debug('graph-cli:subgraph')
|
|
12
|
+
|
|
13
|
+
const throwCombinedError = (filename: string, errors: immutable.List<any>) => {
|
|
12
14
|
throw new Error(
|
|
13
15
|
errors.reduce(
|
|
14
16
|
(msg, e) =>
|
|
@@ -24,7 +26,7 @@ const throwCombinedError = (filename, errors) => {
|
|
|
24
26
|
)
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
const buildCombinedWarning = (filename, warnings) =>
|
|
29
|
+
const buildCombinedWarning = (filename: string, warnings: immutable.List<any>) =>
|
|
28
30
|
warnings.size > 0
|
|
29
31
|
? warnings.reduce(
|
|
30
32
|
(msg, w) =>
|
|
@@ -39,8 +41,14 @@ const buildCombinedWarning = (filename, warnings) =>
|
|
|
39
41
|
) + '\n'
|
|
40
42
|
: null
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
type ResolveFile = (path: string) => string
|
|
45
|
+
|
|
46
|
+
export default class Subgraph {
|
|
47
|
+
static async validate(
|
|
48
|
+
data: any,
|
|
49
|
+
protocol: any,
|
|
50
|
+
{ resolveFile }: { resolveFile: ResolveFile },
|
|
51
|
+
) {
|
|
44
52
|
subgraphDebug(`Validating Subgraph with protocol "%s"`, protocol)
|
|
45
53
|
if (protocol.name == null) {
|
|
46
54
|
return immutable.fromJS([
|
|
@@ -61,6 +69,7 @@ module.exports = class Subgraph {
|
|
|
61
69
|
|
|
62
70
|
// Obtain the root `SubgraphManifest` type from the schema
|
|
63
71
|
let rootType = schema.definitions.find(definition => {
|
|
72
|
+
// @ts-expect-error TODO: name field does not exist on definition, really?
|
|
64
73
|
return definition.name.value === 'SubgraphManifest'
|
|
65
74
|
})
|
|
66
75
|
|
|
@@ -68,22 +77,22 @@ module.exports = class Subgraph {
|
|
|
68
77
|
return validation.validateManifest(data, rootType, schema, protocol, { resolveFile })
|
|
69
78
|
}
|
|
70
79
|
|
|
71
|
-
static validateSchema(manifest, { resolveFile }) {
|
|
80
|
+
static validateSchema(manifest: any, { resolveFile }: { resolveFile: ResolveFile }) {
|
|
72
81
|
let filename = resolveFile(manifest.getIn(['schema', 'file']))
|
|
73
82
|
let errors = validation.validateSchema(filename)
|
|
74
83
|
|
|
75
84
|
if (errors.size > 0) {
|
|
76
|
-
|
|
77
|
-
let msg =
|
|
78
|
-
|
|
79
|
-
let inner_msgs =
|
|
85
|
+
const groupedErrors = errors.groupBy(error => error.get('entity')).sort()
|
|
86
|
+
let msg = groupedErrors.reduce((msg, groupedErrors, entity) => {
|
|
87
|
+
groupedErrors = groupedErrors.groupBy(error => error.get('directive'))
|
|
88
|
+
let inner_msgs = groupedErrors.reduce((msg, _errors, directive) => {
|
|
80
89
|
return `${msg}${
|
|
81
90
|
directive
|
|
82
91
|
? `
|
|
83
92
|
${directive}:`
|
|
84
93
|
: ''
|
|
85
94
|
}
|
|
86
|
-
${
|
|
95
|
+
${groupedErrors
|
|
87
96
|
.map(error =>
|
|
88
97
|
error
|
|
89
98
|
.get('message')
|
|
@@ -102,7 +111,7 @@ module.exports = class Subgraph {
|
|
|
102
111
|
}
|
|
103
112
|
}
|
|
104
113
|
|
|
105
|
-
static validateRepository(manifest,
|
|
114
|
+
static validateRepository(manifest: immutable.Collection<any, any>) {
|
|
106
115
|
const repository = manifest.get('repository')
|
|
107
116
|
|
|
108
117
|
return /^https:\/\/github\.com\/graphprotocol\/example-subgraphs?$/.test(repository)
|
|
@@ -117,7 +126,7 @@ Please replace it with a link to your subgraph source code.`,
|
|
|
117
126
|
: immutable.List()
|
|
118
127
|
}
|
|
119
128
|
|
|
120
|
-
static validateDescription(manifest,
|
|
129
|
+
static validateDescription(manifest: immutable.Collection<any, any>) {
|
|
121
130
|
// TODO: Maybe implement this in the future for each protocol example description
|
|
122
131
|
return manifest.get('description', '').startsWith('Gravatar for ')
|
|
123
132
|
? immutable.List().push(
|
|
@@ -131,11 +140,15 @@ Please update it to tell users more about your subgraph.`,
|
|
|
131
140
|
: immutable.List()
|
|
132
141
|
}
|
|
133
142
|
|
|
134
|
-
static validateHandlers(
|
|
143
|
+
static validateHandlers(
|
|
144
|
+
manifest: immutable.Collection<any, any>,
|
|
145
|
+
protocol: any,
|
|
146
|
+
protocolSubgraph: ISubgraph,
|
|
147
|
+
) {
|
|
135
148
|
return manifest
|
|
136
149
|
.get('dataSources')
|
|
137
|
-
.filter(dataSource => protocol.isValidKindName(dataSource.get('kind')))
|
|
138
|
-
.reduce((errors, dataSource, dataSourceIndex) => {
|
|
150
|
+
.filter((dataSource: any) => protocol.isValidKindName(dataSource.get('kind')))
|
|
151
|
+
.reduce((errors: any, dataSource: any, dataSourceIndex: any) => {
|
|
139
152
|
let path = ['dataSources', dataSourceIndex, 'mapping']
|
|
140
153
|
let mapping = dataSource.get('mapping')
|
|
141
154
|
const handlerTypes = protocolSubgraph.handlerTypes()
|
|
@@ -151,8 +164,8 @@ Please update it to tell users more about your subgraph.`,
|
|
|
151
164
|
}
|
|
152
165
|
|
|
153
166
|
const areAllHandlersEmpty = handlerTypes
|
|
154
|
-
.map(handlerType => mapping.get(handlerType, immutable.List()))
|
|
155
|
-
.every(handlers => handlers.isEmpty())
|
|
167
|
+
.map((handlerType: any) => mapping.get(handlerType, immutable.List()))
|
|
168
|
+
.every((handlers: immutable.List<any>) => handlers.isEmpty())
|
|
156
169
|
|
|
157
170
|
const handlerNamesWithoutLast = handlerTypes.pop().join(', ')
|
|
158
171
|
|
|
@@ -169,7 +182,7 @@ At least one such handler must be defined.`,
|
|
|
169
182
|
}, immutable.List())
|
|
170
183
|
}
|
|
171
184
|
|
|
172
|
-
static validateContractValues(manifest, protocol) {
|
|
185
|
+
static validateContractValues(manifest: any, protocol: any) {
|
|
173
186
|
if (!protocol.hasContract()) {
|
|
174
187
|
return immutable.List()
|
|
175
188
|
}
|
|
@@ -178,30 +191,32 @@ At least one such handler must be defined.`,
|
|
|
178
191
|
}
|
|
179
192
|
|
|
180
193
|
// Validate that data source names are unique, so they don't overwrite each other.
|
|
181
|
-
static validateUniqueDataSourceNames(manifest) {
|
|
182
|
-
let names = []
|
|
183
|
-
return manifest
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
194
|
+
static validateUniqueDataSourceNames(manifest: any) {
|
|
195
|
+
let names: any[] = []
|
|
196
|
+
return manifest
|
|
197
|
+
.get('dataSources')
|
|
198
|
+
.reduce((errors: immutable.List<any>, dataSource: any, dataSourceIndex: number) => {
|
|
199
|
+
let path = ['dataSources', dataSourceIndex, 'name']
|
|
200
|
+
let name = dataSource.get('name')
|
|
201
|
+
if (names.includes(name)) {
|
|
202
|
+
errors = errors.push(
|
|
203
|
+
immutable.fromJS({
|
|
204
|
+
path,
|
|
205
|
+
message: `\
|
|
191
206
|
More than one data source named '${name}', data source names must be unique.`,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
207
|
+
}),
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
names.push(name)
|
|
211
|
+
return errors
|
|
212
|
+
}, immutable.List())
|
|
198
213
|
}
|
|
199
214
|
|
|
200
|
-
static validateUniqueTemplateNames(manifest) {
|
|
201
|
-
let names = []
|
|
215
|
+
static validateUniqueTemplateNames(manifest: any) {
|
|
216
|
+
let names: any[] = []
|
|
202
217
|
return manifest
|
|
203
218
|
.get('templates', immutable.List())
|
|
204
|
-
.reduce((errors
|
|
219
|
+
.reduce((errors: immutable.List<any>, template: any, templateIndex: number) => {
|
|
205
220
|
let path = ['templates', templateIndex, 'name']
|
|
206
221
|
let name = template.get('name')
|
|
207
222
|
if (names.includes(name)) {
|
|
@@ -218,14 +233,20 @@ More than one template named '${name}', template names must be unique.`,
|
|
|
218
233
|
}, immutable.List())
|
|
219
234
|
}
|
|
220
235
|
|
|
221
|
-
static dump(manifest) {
|
|
236
|
+
static dump(manifest: any) {
|
|
222
237
|
strOptions.fold.lineWidth = 90
|
|
238
|
+
// @ts-expect-error TODO: plain is the value behind the TS constant
|
|
223
239
|
strOptions.defaultType = 'PLAIN'
|
|
224
240
|
|
|
225
241
|
return yaml.stringify(manifest.toJS())
|
|
226
242
|
}
|
|
227
243
|
|
|
228
|
-
static async load(
|
|
244
|
+
static async load(
|
|
245
|
+
filename: string,
|
|
246
|
+
{ protocol, skipValidation }: { protocol?: any; skipValidation?: boolean } = {
|
|
247
|
+
skipValidation: false,
|
|
248
|
+
},
|
|
249
|
+
) {
|
|
229
250
|
// Load and validate the manifest
|
|
230
251
|
let data = null
|
|
231
252
|
let has_file_data_sources = false
|
|
@@ -239,7 +260,7 @@ More than one template named '${name}', template names must be unique.`,
|
|
|
239
260
|
}
|
|
240
261
|
|
|
241
262
|
// Helper to resolve files relative to the subgraph manifest
|
|
242
|
-
let resolveFile = maybeRelativeFile =>
|
|
263
|
+
let resolveFile: ResolveFile = maybeRelativeFile =>
|
|
243
264
|
path.resolve(path.dirname(filename), maybeRelativeFile)
|
|
244
265
|
|
|
245
266
|
// TODO: Validation for file data sources
|
|
@@ -279,8 +300,8 @@ More than one template named '${name}', template names must be unique.`,
|
|
|
279
300
|
let warnings = skipValidation
|
|
280
301
|
? immutable.List()
|
|
281
302
|
: immutable.List.of(
|
|
282
|
-
...Subgraph.validateRepository(manifest
|
|
283
|
-
...Subgraph.validateDescription(manifest
|
|
303
|
+
...Subgraph.validateRepository(manifest),
|
|
304
|
+
...Subgraph.validateDescription(manifest),
|
|
284
305
|
)
|
|
285
306
|
|
|
286
307
|
return {
|
|
@@ -289,7 +310,7 @@ More than one template named '${name}', template names must be unique.`,
|
|
|
289
310
|
}
|
|
290
311
|
}
|
|
291
312
|
|
|
292
|
-
static async write(manifest, filename) {
|
|
313
|
+
static async write(manifest: any, filename: string) {
|
|
293
314
|
await fs.writeFile(filename, Subgraph.dump(manifest))
|
|
294
315
|
}
|
|
295
316
|
}
|
package/src/type-generator.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
import immutable from 'immutable'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import prettier from 'prettier'
|
|
5
|
+
import * as graphql from 'graphql/language'
|
|
6
|
+
import chalk from 'chalk'
|
|
7
|
+
import * as toolbox from 'gluegun/toolbox'
|
|
8
|
+
|
|
9
|
+
import Schema from './schema'
|
|
10
|
+
import Subgraph from './subgraph'
|
|
11
|
+
import DataSourceTemplateCodeGenerator from './codegen/template'
|
|
12
|
+
import Watcher from './watcher'
|
|
13
|
+
import { step, withSpinner } from './command-helpers/spinner'
|
|
14
|
+
import { applyMigrations } from './migrations'
|
|
15
|
+
import { GENERATED_FILE_NOTE } from './codegen/typescript'
|
|
16
|
+
import { displayPath } from './command-helpers/fs'
|
|
17
|
+
import uncrashable from '@float-capital/float-subgraph-uncrashable/src/Index.bs.js'
|
|
18
|
+
|
|
19
|
+
export default class TypeGenerator {
|
|
20
20
|
constructor(options) {
|
|
21
21
|
this.options = options || {}
|
|
22
22
|
this.sourceDir =
|
|
@@ -29,7 +29,7 @@ module.exports = class TypeGenerator {
|
|
|
29
29
|
outputDir: this.options.outputDir,
|
|
30
30
|
})
|
|
31
31
|
|
|
32
|
-
process.on('uncaughtException', function(e) {
|
|
32
|
+
process.on('uncaughtException', function (e) {
|
|
33
33
|
toolbox.print.error(`UNCAUGHT EXCEPTION: ${e}`)
|
|
34
34
|
})
|
|
35
35
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import immutable from 'immutable'
|
|
2
2
|
|
|
3
3
|
const validateContract = (value, ProtocolContract) => {
|
|
4
4
|
const contract = new ProtocolContract(value)
|
|
@@ -33,7 +33,6 @@ const validateContractValues = (manifest, protocol) => {
|
|
|
33
33
|
|
|
34
34
|
let contractValue = dataSource.getIn(['source', fieldName])
|
|
35
35
|
|
|
36
|
-
|
|
37
36
|
const { valid, error } = validateContract(contractValue, ProtocolContract)
|
|
38
37
|
|
|
39
38
|
// Validate whether the contract is valid for the protocol
|
|
@@ -50,7 +49,4 @@ const validateContractValues = (manifest, protocol) => {
|
|
|
50
49
|
}, immutable.List())
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
|
|
54
|
-
validateContract,
|
|
55
|
-
validateContractValues,
|
|
56
|
-
}
|
|
52
|
+
export { validateContract, validateContractValues }
|
package/src/validation/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import immutable from 'immutable'
|
|
2
|
+
import yaml from 'js-yaml'
|
|
3
|
+
import path from 'path'
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import Protocol from '../protocols'
|
|
6
6
|
|
|
7
7
|
const List = immutable.List
|
|
8
8
|
const Map = immutable.Map
|
|
@@ -154,7 +154,7 @@ const validators = immutable.fromJS({
|
|
|
154
154
|
},
|
|
155
155
|
|
|
156
156
|
EnumTypeDefinition: (value, ctx) => {
|
|
157
|
-
const enumValues = ctx.getIn(['type', 'values']).map(
|
|
157
|
+
const enumValues = ctx.getIn(['type', 'values']).map(v => {
|
|
158
158
|
return v.getIn(['name', 'value'])
|
|
159
159
|
})
|
|
160
160
|
|
|
@@ -163,11 +163,11 @@ const validators = immutable.fromJS({
|
|
|
163
163
|
return enumValues.includes(value)
|
|
164
164
|
? List()
|
|
165
165
|
: immutable.fromJS([
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
166
|
+
{
|
|
167
|
+
path: ctx.get('path'),
|
|
168
|
+
message: `Unexpected enum value: ${value}, allowed values: ${allowedValues}`,
|
|
169
|
+
},
|
|
170
|
+
])
|
|
171
171
|
},
|
|
172
172
|
|
|
173
173
|
String: (value, ctx) =>
|
|
@@ -262,15 +262,15 @@ const validateValue = (value, ctx) => {
|
|
|
262
262
|
// },
|
|
263
263
|
// }
|
|
264
264
|
const dataSourceListToMap = dataSources =>
|
|
265
|
-
dataSources
|
|
266
|
-
|
|
267
|
-
(
|
|
268
|
-
|
|
269
|
-
(
|
|
270
|
-
(dataSourceNames || immutable.OrderedSet()).add(dataSource.name)),
|
|
265
|
+
dataSources.reduce(
|
|
266
|
+
(protocolKinds, dataSource) =>
|
|
267
|
+
protocolKinds.update(Protocol.normalizeName(dataSource.kind), networks =>
|
|
268
|
+
(networks || immutable.OrderedMap()).update(dataSource.network, dataSourceNames =>
|
|
269
|
+
(dataSourceNames || immutable.OrderedSet()).add(dataSource.name),
|
|
271
270
|
),
|
|
272
|
-
|
|
273
|
-
)
|
|
271
|
+
),
|
|
272
|
+
immutable.OrderedMap(),
|
|
273
|
+
)
|
|
274
274
|
|
|
275
275
|
const validateDataSourceProtocolAndNetworks = value => {
|
|
276
276
|
const dataSources = [...value.dataSources, ...(value.templates || [])]
|
|
@@ -289,7 +289,11 @@ ${protocolNetworkMap
|
|
|
289
289
|
protocolKind === undefined
|
|
290
290
|
? 'Data sources and templates having no protocol kind set'
|
|
291
291
|
: `Data sources and templates using '${protocolKind}'`
|
|
292
|
-
}:\n${dataSourceNames
|
|
292
|
+
}:\n${dataSourceNames
|
|
293
|
+
.valueSeq()
|
|
294
|
+
.flatten()
|
|
295
|
+
.map(ds => ` - ${ds}`)
|
|
296
|
+
.join('\n')}`,
|
|
293
297
|
)
|
|
294
298
|
.join('\n')}
|
|
295
299
|
Recommendation: Make all data sources and templates use the same protocol kind.`,
|
|
@@ -355,6 +359,4 @@ const validateManifest = (value, type, schema, protocol, { resolveFile }) => {
|
|
|
355
359
|
return validateDataSourceProtocolAndNetworks(value)
|
|
356
360
|
}
|
|
357
361
|
|
|
358
|
-
|
|
359
|
-
validateManifest,
|
|
360
|
-
}
|
|
362
|
+
export { validateManifest }
|