@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.
Files changed (206) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/babel.config.js +3 -0
  3. package/dist/bin.js +6 -0
  4. package/dist/cli.js +32 -0
  5. package/dist/codegen/schema.js +250 -0
  6. package/dist/codegen/schema.test.js +458 -0
  7. package/dist/codegen/template.js +65 -0
  8. package/dist/codegen/types/conversions.js +359 -0
  9. package/dist/codegen/types/index.js +71 -0
  10. package/dist/codegen/types/index.test.js +563 -0
  11. package/dist/codegen/typescript.js +200 -0
  12. package/dist/codegen/util.js +46 -0
  13. package/dist/codegen/util.test.js +93 -0
  14. package/dist/command-helpers/abi.js +78 -0
  15. package/dist/command-helpers/auth.js +77 -0
  16. package/dist/command-helpers/compiler.js +66 -0
  17. package/dist/command-helpers/data-sources.js +29 -0
  18. package/dist/command-helpers/fs.js +9 -0
  19. package/dist/command-helpers/gluegun.js +49 -0
  20. package/dist/command-helpers/ipfs.js +5 -0
  21. package/dist/command-helpers/jsonrpc.js +29 -0
  22. package/dist/command-helpers/network.js +93 -0
  23. package/dist/command-helpers/network.test.js +86 -0
  24. package/dist/command-helpers/node.js +40 -0
  25. package/dist/command-helpers/scaffold.js +112 -0
  26. package/dist/command-helpers/spinner.js +84 -0
  27. package/dist/command-helpers/studio.js +13 -0
  28. package/dist/command-helpers/studio.test.js +41 -0
  29. package/dist/command-helpers/subgraph.js +24 -0
  30. package/dist/command-helpers/version.js +74 -0
  31. package/dist/command-helpers/version.test.js +179 -0
  32. package/dist/commands/add.js +190 -0
  33. package/dist/commands/auth.js +127 -0
  34. package/dist/commands/build.js +140 -0
  35. package/dist/commands/codegen.js +135 -0
  36. package/dist/commands/create.js +86 -0
  37. package/dist/commands/deploy.js +334 -0
  38. package/dist/commands/init.js +788 -0
  39. package/dist/commands/local.js +387 -0
  40. package/dist/commands/remove.js +86 -0
  41. package/dist/commands/test.js +295 -0
  42. package/dist/compiler/asc.js +83 -0
  43. package/dist/compiler/index.js +474 -0
  44. package/dist/debug.js +16 -0
  45. package/dist/migrations/mapping_api_version_0_0_1.js +78 -0
  46. package/dist/migrations/mapping_api_version_0_0_2.js +76 -0
  47. package/dist/migrations/mapping_api_version_0_0_3.js +78 -0
  48. package/dist/migrations/mapping_api_version_0_0_4.js +80 -0
  49. package/dist/migrations/mapping_api_version_0_0_5.js +80 -0
  50. package/dist/migrations/spec_version_0_0_2.js +49 -0
  51. package/dist/migrations/spec_version_0_0_3.js +54 -0
  52. package/dist/migrations/util/load-manifest.js +18 -0
  53. package/dist/migrations/util/versions.js +29 -0
  54. package/dist/migrations.js +56 -0
  55. package/dist/protocols/arweave/manifest.graphql +57 -0
  56. package/dist/protocols/arweave/scaffold/manifest.js +18 -0
  57. package/dist/protocols/arweave/scaffold/mapping.js +52 -0
  58. package/dist/protocols/arweave/subgraph.js +23 -0
  59. package/dist/protocols/cosmos/manifest.graphql +76 -0
  60. package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
  61. package/dist/protocols/cosmos/scaffold/mapping.js +38 -0
  62. package/dist/protocols/cosmos/subgraph.js +25 -0
  63. package/dist/protocols/ethereum/abi.js +140 -0
  64. package/dist/protocols/ethereum/codegen/abi.js +451 -0
  65. package/dist/protocols/ethereum/codegen/abi.test.js +328 -0
  66. package/dist/protocols/ethereum/codegen/template.js +50 -0
  67. package/dist/protocols/ethereum/contract.js +20 -0
  68. package/dist/protocols/ethereum/manifest.graphql +94 -0
  69. package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
  70. package/dist/protocols/ethereum/scaffold/mapping.js +65 -0
  71. package/dist/protocols/ethereum/subgraph.js +171 -0
  72. package/dist/protocols/ethereum/type-generator.js +125 -0
  73. package/dist/protocols/index.js +268 -0
  74. package/dist/protocols/ipfs/codegen/file_template.js +50 -0
  75. package/dist/protocols/near/contract.js +41 -0
  76. package/dist/protocols/near/manifest.graphql +64 -0
  77. package/dist/protocols/near/scaffold/manifest.js +16 -0
  78. package/dist/protocols/near/scaffold/mapping.js +38 -0
  79. package/dist/protocols/near/subgraph.js +20 -0
  80. package/dist/protocols/subgraph.js +2 -0
  81. package/dist/protocols/substreams/manifest.graphql +45 -0
  82. package/dist/protocols/substreams/scaffold/manifest.js +12 -0
  83. package/dist/protocols/substreams/subgraph.js +23 -0
  84. package/dist/scaffold/cosmos.test.js +83 -0
  85. package/dist/scaffold/ethereum.test.js +505 -0
  86. package/dist/scaffold/index.js +128 -0
  87. package/dist/scaffold/mapping.js +61 -0
  88. package/dist/scaffold/near.test.js +86 -0
  89. package/dist/scaffold/schema.js +83 -0
  90. package/dist/scaffold/tests.js +175 -0
  91. package/dist/schema.js +54 -0
  92. package/dist/subgraph.js +249 -0
  93. package/dist/type-generator.js +224 -0
  94. package/dist/validation/contract.js +46 -0
  95. package/dist/validation/index.js +8 -0
  96. package/dist/validation/manifest.js +271 -0
  97. package/dist/validation/schema.js +796 -0
  98. package/dist/validation/schema.test.js +35 -0
  99. package/dist/watcher.js +79 -0
  100. package/jest.config.js +1 -1
  101. package/package.json +16 -9
  102. package/{bin/graph → src/bin.ts} +2 -1
  103. package/src/cli.ts +36 -0
  104. package/src/codegen/schema.js +23 -22
  105. package/src/codegen/schema.test.js +33 -27
  106. package/src/codegen/{template.js → template.ts} +8 -6
  107. package/src/codegen/types/conversions.ts +382 -0
  108. package/src/codegen/types/index.js +4 -7
  109. package/src/codegen/types/index.test.js +10 -4
  110. package/src/codegen/{typescript.js → typescript.ts} +44 -26
  111. package/src/codegen/util.js +1 -1
  112. package/src/codegen/util.test.js +5 -2
  113. package/src/command-helpers/abi.js +46 -30
  114. package/src/command-helpers/auth.js +6 -9
  115. package/src/command-helpers/compiler.js +4 -6
  116. package/src/command-helpers/data-sources.js +8 -11
  117. package/src/command-helpers/fs.ts +5 -0
  118. package/src/command-helpers/gluegun.js +2 -4
  119. package/src/command-helpers/ipfs.ts +3 -0
  120. package/src/command-helpers/{jsonrpc.js → jsonrpc.ts} +11 -16
  121. package/src/command-helpers/network.js +58 -44
  122. package/src/command-helpers/network.test.js +23 -19
  123. package/src/command-helpers/node.js +3 -7
  124. package/src/command-helpers/scaffold.js +34 -25
  125. package/src/command-helpers/{spinner.js → spinner.ts} +10 -10
  126. package/src/command-helpers/studio.test.js +61 -30
  127. package/src/command-helpers/studio.ts +20 -0
  128. package/src/command-helpers/{subgraph.js → subgraph.ts} +6 -6
  129. package/src/command-helpers/version.js +11 -16
  130. package/src/command-helpers/version.test.js +111 -82
  131. package/src/commands/add.js +49 -35
  132. package/src/commands/auth.js +13 -25
  133. package/src/commands/build.js +9 -9
  134. package/src/commands/codegen.js +12 -12
  135. package/src/commands/create.js +22 -22
  136. package/src/commands/deploy.js +39 -34
  137. package/src/commands/init.js +21 -24
  138. package/src/commands/local.js +15 -14
  139. package/src/commands/remove.js +22 -22
  140. package/src/commands/test.js +86 -70
  141. package/src/compiler/{asc.js → asc.ts} +29 -16
  142. package/src/compiler/index.js +18 -18
  143. package/src/{debug.js → debug.ts} +2 -2
  144. package/src/migrations/mapping_api_version_0_0_1.js +7 -7
  145. package/src/migrations/mapping_api_version_0_0_2.js +5 -5
  146. package/src/migrations/mapping_api_version_0_0_3.js +7 -7
  147. package/src/migrations/mapping_api_version_0_0_4.js +7 -7
  148. package/src/migrations/mapping_api_version_0_0_5.js +7 -7
  149. package/src/migrations/spec_version_0_0_2.js +5 -5
  150. package/src/migrations/spec_version_0_0_3.js +5 -5
  151. package/src/migrations/util/load-manifest.js +6 -9
  152. package/src/migrations/util/versions.js +5 -10
  153. package/src/{migrations.js → migrations.ts} +14 -15
  154. package/src/protocols/arweave/scaffold/manifest.js +1 -4
  155. package/src/protocols/arweave/scaffold/mapping.js +1 -3
  156. package/src/protocols/arweave/subgraph.ts +22 -0
  157. package/src/protocols/cosmos/scaffold/manifest.js +1 -4
  158. package/src/protocols/cosmos/scaffold/mapping.js +1 -3
  159. package/src/protocols/cosmos/subgraph.js +2 -2
  160. package/src/protocols/ethereum/abi.js +7 -13
  161. package/src/protocols/ethereum/codegen/abi.js +219 -222
  162. package/src/protocols/ethereum/codegen/abi.test.js +6 -6
  163. package/src/protocols/ethereum/codegen/template.js +3 -5
  164. package/src/protocols/ethereum/contract.js +3 -2
  165. package/src/protocols/ethereum/scaffold/manifest.js +4 -7
  166. package/src/protocols/ethereum/scaffold/mapping.js +4 -11
  167. package/src/protocols/ethereum/subgraph.js +21 -20
  168. package/src/protocols/ethereum/type-generator.js +24 -30
  169. package/src/protocols/{index.js → index.ts} +65 -46
  170. package/src/protocols/ipfs/codegen/file_template.js +2 -2
  171. package/src/protocols/near/{contract.js → contract.ts} +16 -12
  172. package/src/protocols/near/scaffold/manifest.js +2 -5
  173. package/src/protocols/near/scaffold/mapping.js +1 -3
  174. package/src/protocols/near/subgraph.js +3 -6
  175. package/src/protocols/subgraph.ts +12 -0
  176. package/src/protocols/substreams/scaffold/{manifest.js → manifest.ts} +2 -7
  177. package/src/protocols/substreams/subgraph.ts +22 -0
  178. package/src/scaffold/cosmos.test.js +3 -3
  179. package/src/scaffold/ethereum.test.js +4 -4
  180. package/src/scaffold/index.js +7 -7
  181. package/src/scaffold/mapping.js +3 -3
  182. package/src/scaffold/near.test.js +3 -3
  183. package/src/scaffold/schema.js +4 -4
  184. package/src/scaffold/tests.js +72 -58
  185. package/src/schema.ts +26 -0
  186. package/src/{subgraph.js → subgraph.ts} +80 -60
  187. package/src/type-generator.js +20 -20
  188. package/src/validation/contract.js +2 -6
  189. package/src/validation/index.js +1 -1
  190. package/src/validation/manifest.js +24 -22
  191. package/src/validation/schema.test.js +1 -1
  192. package/src/validation/{schema.js → schema.ts} +268 -221
  193. package/src/{watcher.js → watcher.ts} +23 -12
  194. package/tests/cli/globalSetup.js +2 -2
  195. package/tests/cli/globalTeardown.js +2 -2
  196. package/tests/cli/init.test.js +2 -2
  197. package/tests/cli/util.js +7 -13
  198. package/tsconfig.json +18 -0
  199. package/src/cli.js +0 -38
  200. package/src/codegen/types/conversions.js +0 -329
  201. package/src/command-helpers/fs.js +0 -8
  202. package/src/command-helpers/ipfs.js +0 -6
  203. package/src/command-helpers/studio.js +0 -15
  204. package/src/protocols/arweave/subgraph.js +0 -20
  205. package/src/protocols/substreams/subgraph.js +0 -17
  206. package/src/schema.js +0 -23
@@ -1,34 +1,39 @@
1
- const prettier = require('prettier')
2
- const { strings } = require('gluegun')
3
- const { ascTypeForEthereum, ethereumFromAsc } = require("../codegen/types")
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
- "i32": 123,
7
- "BigInt": 234,
8
- "Bytes": 1234567890,
9
- "Address": "0x0000000000000000000000000000000000000001",
10
- "string": "Example string value",
11
- "bool": true,
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
- .inputs
19
- .map(input => {
20
- // If the asc type is Array<T> we need to check if T is a native type or a custom graph-ts type
21
- // If we don't do that we may miss a type that should be imported from graph-ts
22
- const ascType = ascTypeForEthereum(input.type)
23
- const inner = fetchArrayInnerType(ascType)
24
- return inner ? inner[1] : ascType
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(generateExampleTest(contract, events[0], indexEvents, importTypes), { parser: 'typescript', semi: false }),
31
- [`${strings.kebabCase(contract)}-utils.ts`]: prettier.format(generateTestHelper(contract, events, importTypes), { parser: 'typescript', semi: false }),
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 = (eventInputs) => {
43
- return eventInputs.map((input, index) => {
44
- let ascType = ascTypeForEthereum(input.type)
45
- return `let ${input.name || `param${index}`} = ${assignValue(ascType, input.name)}`
46
- }).join('\n')
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 = (type) => {
57
+ const assignValue = type => {
51
58
  switch (type) {
52
- case "string":
59
+ case 'string':
53
60
  return `"${VARIABLES_VALUES[type]}"`
54
- case "BigInt":
61
+ case 'BigInt':
55
62
  return `BigInt.fromI32(${VARIABLES_VALUES[type]})`
56
- case "Address":
63
+ case 'Address':
57
64
  return `Address.fromString("${VARIABLES_VALUES[type]}")`
58
- case "Bytes":
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) => eventInputs.filter(input => input.name != "id").map((input, index) =>
79
- `assert.fieldEquals(
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 ? "-1" : ""}",
92
+ "0xa16081f360e3847006db660bae1c6d1b2e17ec2a${indexEvents ? '-1' : ''}",
82
93
  "${input.name || `param${index}`}",
83
94
  "${expectedValue(ascTypeForEthereum(input.type))}"
84
- )`
85
- ).join('\n')
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 { ${indexEvents ? `${eventName} as ${eventName}Event` : eventName} } from "../generated/${contract}/${contract}"
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.map((input, index) => input.name || `param${index}`).join(', ')});
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(", ")} } from '../generated/${contract}/${contract}';
176
+ import { ${eventsNames.join(', ')} } from '../generated/${contract}/${contract}';
165
177
 
166
- ${generateMockedEvents(events).join("\n")}`
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((input, index) => `${input.name || `param${index}`}: ${ascTypeForEthereum(input.type)}`);
178
- const ascToEth = event.inputs.map((input, index) => `${varName}.parameters.push(new ethereum.EventParam("${input.name || `param${index}`}", ${ethereumFromAsc(input.name || `param${index}`, input.type)}))`);
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
- module.exports = {
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
- let fs = require('fs-extra')
2
- let immutable = require('immutable')
3
- let path = require('path')
4
- let yaml = require('yaml')
5
- let { strOptions } = require('yaml/types')
6
- let graphql = require('graphql/language')
7
- let validation = require('./validation')
8
-
9
- let subgraphDebug = require('./debug')('graph-cli:subgraph')
10
-
11
- const throwCombinedError = (filename, errors) => {
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
- module.exports = class Subgraph {
43
- static async validate(data, protocol, { resolveFile }) {
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
- let errors = validation.validateSchema(filename)
82
+ const validationErrors = validation.validateSchema(filename)
83
+ let errors: immutable.Collection<any, any>
74
84
 
75
- if (errors.size > 0) {
76
- errors = errors.groupBy(error => error.get('entity')).sort()
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((msg, errors, directive) => {
80
- return `${msg}${
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, { resolveFile }) {
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, { resolveFile }) {
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(manifest, protocol, protocolSubgraph) {
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.get('dataSources').reduce((errors, dataSource, dataSourceIndex) => {
184
- let path = ['dataSources', dataSourceIndex, 'name']
185
- let name = dataSource.get('name')
186
- if (names.includes(name)) {
187
- errors = errors.push(
188
- immutable.fromJS({
189
- path,
190
- message: `\
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
- names.push(name)
196
- return errors
197
- }, immutable.List())
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, template, templateIndex) => {
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(filename, { protocol, skipValidation } = { skipValidation: false }) {
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, { resolveFile }),
283
- ...Subgraph.validateDescription(manifest, { resolveFile }),
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
  }
@@ -1,22 +1,22 @@
1
- const fs = require('fs-extra')
2
- const immutable = require('immutable')
3
- const path = require('path')
4
- const prettier = require('prettier')
5
- const graphql = require('graphql/language')
6
- const chalk = require('chalk')
7
- const toolbox = require('gluegun/toolbox')
8
-
9
- const Schema = require('./schema')
10
- const Subgraph = require('./subgraph')
11
- const DataSourceTemplateCodeGenerator = require('./codegen/template')
12
- const Watcher = require('./watcher')
13
- const { step, withSpinner } = require('./command-helpers/spinner')
14
- const { applyMigrations } = require('./migrations')
15
- const { GENERATED_FILE_NOTE } = require('./codegen/typescript')
16
- const { displayPath } = require('./command-helpers/fs')
17
- const uncrashable = require('@float-capital/float-subgraph-uncrashable/src/Index.bs.js')
18
-
19
- module.exports = class TypeGenerator {
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
- const immutable = require('immutable')
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
- module.exports = {
54
- validateContract,
55
- validateContractValues,
56
- }
52
+ export { validateContract, validateContractValues }
@@ -1,4 +1,4 @@
1
- module.exports = {
1
+ export default {
2
2
  validateSchema: require('./schema').validateSchema,
3
3
  validateManifest: require('./manifest').validateManifest,
4
4
  validateContractValues: require('./contract').validateContractValues,