@graphprotocol/graph-cli 0.37.2 → 0.37.3-alpha-20230110163550-d64bef6
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 +268 -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 +23 -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 +249 -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 → index.ts} +65 -46
- 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 → manifest.ts} +2 -7
- package/src/protocols/substreams/subgraph.ts +22 -0
- 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} +80 -60
- 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/protocols/substreams/subgraph.js +0 -17
- 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,21 +77,18 @@ 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
|
+
const validationErrors = validation.validateSchema(filename)
|
|
83
|
+
let errors: immutable.Collection<any, any>
|
|
74
84
|
|
|
75
|
-
if (
|
|
76
|
-
errors =
|
|
85
|
+
if (validationErrors.size > 0) {
|
|
86
|
+
errors = validationErrors.groupBy(error => error.get('entity')).sort()
|
|
77
87
|
let msg = errors.reduce((msg, errors, entity) => {
|
|
78
|
-
errors = errors.groupBy(error => error.get('directive'))
|
|
79
|
-
let inner_msgs = errors.reduce(
|
|
80
|
-
|
|
81
|
-
directive
|
|
82
|
-
? `
|
|
83
|
-
${directive}:`
|
|
84
|
-
: ''
|
|
85
|
-
}
|
|
88
|
+
errors = errors.groupBy((error: any) => error.get('directive'))
|
|
89
|
+
let inner_msgs = errors.reduce(
|
|
90
|
+
(msg: string, errors: any[], directive: string) => {
|
|
91
|
+
return `${msg}${directive ? `${directive}:` : ''}
|
|
86
92
|
${errors
|
|
87
93
|
.map(error =>
|
|
88
94
|
error
|
|
@@ -92,7 +98,9 @@ module.exports = class Subgraph {
|
|
|
92
98
|
)
|
|
93
99
|
.map(msg => `${directive ? ' ' : ''}- ${msg}`)
|
|
94
100
|
.join('\n ')}`
|
|
95
|
-
|
|
101
|
+
},
|
|
102
|
+
``,
|
|
103
|
+
)
|
|
96
104
|
return `${msg}
|
|
97
105
|
|
|
98
106
|
${entity}:${inner_msgs}`
|
|
@@ -102,7 +110,7 @@ module.exports = class Subgraph {
|
|
|
102
110
|
}
|
|
103
111
|
}
|
|
104
112
|
|
|
105
|
-
static validateRepository(manifest,
|
|
113
|
+
static validateRepository(manifest: immutable.Collection<any, any>) {
|
|
106
114
|
const repository = manifest.get('repository')
|
|
107
115
|
|
|
108
116
|
return /^https:\/\/github\.com\/graphprotocol\/example-subgraphs?$/.test(repository)
|
|
@@ -117,7 +125,7 @@ Please replace it with a link to your subgraph source code.`,
|
|
|
117
125
|
: immutable.List()
|
|
118
126
|
}
|
|
119
127
|
|
|
120
|
-
static validateDescription(manifest,
|
|
128
|
+
static validateDescription(manifest: immutable.Collection<any, any>) {
|
|
121
129
|
// TODO: Maybe implement this in the future for each protocol example description
|
|
122
130
|
return manifest.get('description', '').startsWith('Gravatar for ')
|
|
123
131
|
? immutable.List().push(
|
|
@@ -131,11 +139,15 @@ Please update it to tell users more about your subgraph.`,
|
|
|
131
139
|
: immutable.List()
|
|
132
140
|
}
|
|
133
141
|
|
|
134
|
-
static validateHandlers(
|
|
142
|
+
static validateHandlers(
|
|
143
|
+
manifest: immutable.Collection<any, any>,
|
|
144
|
+
protocol: any,
|
|
145
|
+
protocolSubgraph: ISubgraph,
|
|
146
|
+
) {
|
|
135
147
|
return manifest
|
|
136
148
|
.get('dataSources')
|
|
137
|
-
.filter(dataSource => protocol.isValidKindName(dataSource.get('kind')))
|
|
138
|
-
.reduce((errors, dataSource, dataSourceIndex) => {
|
|
149
|
+
.filter((dataSource: any) => protocol.isValidKindName(dataSource.get('kind')))
|
|
150
|
+
.reduce((errors: any, dataSource: any, dataSourceIndex: any) => {
|
|
139
151
|
let path = ['dataSources', dataSourceIndex, 'mapping']
|
|
140
152
|
let mapping = dataSource.get('mapping')
|
|
141
153
|
const handlerTypes = protocolSubgraph.handlerTypes()
|
|
@@ -151,8 +163,8 @@ Please update it to tell users more about your subgraph.`,
|
|
|
151
163
|
}
|
|
152
164
|
|
|
153
165
|
const areAllHandlersEmpty = handlerTypes
|
|
154
|
-
.map(handlerType => mapping.get(handlerType, immutable.List()))
|
|
155
|
-
.every(handlers => handlers.isEmpty())
|
|
166
|
+
.map((handlerType: any) => mapping.get(handlerType, immutable.List()))
|
|
167
|
+
.every((handlers: immutable.List<any>) => handlers.isEmpty())
|
|
156
168
|
|
|
157
169
|
const handlerNamesWithoutLast = handlerTypes.pop().join(', ')
|
|
158
170
|
|
|
@@ -169,7 +181,7 @@ At least one such handler must be defined.`,
|
|
|
169
181
|
}, immutable.List())
|
|
170
182
|
}
|
|
171
183
|
|
|
172
|
-
static validateContractValues(manifest, protocol) {
|
|
184
|
+
static validateContractValues(manifest: any, protocol: any) {
|
|
173
185
|
if (!protocol.hasContract()) {
|
|
174
186
|
return immutable.List()
|
|
175
187
|
}
|
|
@@ -178,30 +190,32 @@ At least one such handler must be defined.`,
|
|
|
178
190
|
}
|
|
179
191
|
|
|
180
192
|
// 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
|
-
|
|
193
|
+
static validateUniqueDataSourceNames(manifest: any) {
|
|
194
|
+
let names: any[] = []
|
|
195
|
+
return manifest
|
|
196
|
+
.get('dataSources')
|
|
197
|
+
.reduce((errors: immutable.List<any>, dataSource: any, dataSourceIndex: number) => {
|
|
198
|
+
let path = ['dataSources', dataSourceIndex, 'name']
|
|
199
|
+
let name = dataSource.get('name')
|
|
200
|
+
if (names.includes(name)) {
|
|
201
|
+
errors = errors.push(
|
|
202
|
+
immutable.fromJS({
|
|
203
|
+
path,
|
|
204
|
+
message: `\
|
|
191
205
|
More than one data source named '${name}', data source names must be unique.`,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
206
|
+
}),
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
names.push(name)
|
|
210
|
+
return errors
|
|
211
|
+
}, immutable.List())
|
|
198
212
|
}
|
|
199
213
|
|
|
200
|
-
static validateUniqueTemplateNames(manifest) {
|
|
201
|
-
let names = []
|
|
214
|
+
static validateUniqueTemplateNames(manifest: any) {
|
|
215
|
+
let names: any[] = []
|
|
202
216
|
return manifest
|
|
203
217
|
.get('templates', immutable.List())
|
|
204
|
-
.reduce((errors
|
|
218
|
+
.reduce((errors: immutable.List<any>, template: any, templateIndex: number) => {
|
|
205
219
|
let path = ['templates', templateIndex, 'name']
|
|
206
220
|
let name = template.get('name')
|
|
207
221
|
if (names.includes(name)) {
|
|
@@ -218,14 +232,20 @@ More than one template named '${name}', template names must be unique.`,
|
|
|
218
232
|
}, immutable.List())
|
|
219
233
|
}
|
|
220
234
|
|
|
221
|
-
static dump(manifest) {
|
|
235
|
+
static dump(manifest: any) {
|
|
222
236
|
strOptions.fold.lineWidth = 90
|
|
237
|
+
// @ts-expect-error TODO: plain is the value behind the TS constant
|
|
223
238
|
strOptions.defaultType = 'PLAIN'
|
|
224
239
|
|
|
225
240
|
return yaml.stringify(manifest.toJS())
|
|
226
241
|
}
|
|
227
242
|
|
|
228
|
-
static async load(
|
|
243
|
+
static async load(
|
|
244
|
+
filename: string,
|
|
245
|
+
{ protocol, skipValidation }: { protocol?: any; skipValidation?: boolean } = {
|
|
246
|
+
skipValidation: false,
|
|
247
|
+
},
|
|
248
|
+
) {
|
|
229
249
|
// Load and validate the manifest
|
|
230
250
|
let data = null
|
|
231
251
|
let has_file_data_sources = false
|
|
@@ -239,7 +259,7 @@ More than one template named '${name}', template names must be unique.`,
|
|
|
239
259
|
}
|
|
240
260
|
|
|
241
261
|
// Helper to resolve files relative to the subgraph manifest
|
|
242
|
-
let resolveFile = maybeRelativeFile =>
|
|
262
|
+
let resolveFile: ResolveFile = maybeRelativeFile =>
|
|
243
263
|
path.resolve(path.dirname(filename), maybeRelativeFile)
|
|
244
264
|
|
|
245
265
|
// TODO: Validation for file data sources
|
|
@@ -279,8 +299,8 @@ More than one template named '${name}', template names must be unique.`,
|
|
|
279
299
|
let warnings = skipValidation
|
|
280
300
|
? immutable.List()
|
|
281
301
|
: immutable.List.of(
|
|
282
|
-
...Subgraph.validateRepository(manifest
|
|
283
|
-
...Subgraph.validateDescription(manifest
|
|
302
|
+
...Subgraph.validateRepository(manifest),
|
|
303
|
+
...Subgraph.validateDescription(manifest),
|
|
284
304
|
)
|
|
285
305
|
|
|
286
306
|
return {
|
|
@@ -289,7 +309,7 @@ More than one template named '${name}', template names must be unique.`,
|
|
|
289
309
|
}
|
|
290
310
|
}
|
|
291
311
|
|
|
292
|
-
static async write(manifest, filename) {
|
|
312
|
+
static async write(manifest: any, filename: string) {
|
|
293
313
|
await fs.writeFile(filename, Subgraph.dump(manifest))
|
|
294
314
|
}
|
|
295
315
|
}
|
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