1 | # The Graph TypeScript Library (graph-ts)
|
2 |
|
3 | [![npm (scoped)](https://img.shields.io/npm/v/@graphprotocol/graph-ts.svg)](https://www.npmjs.com/package/@graphprotocol/graph-ts)
|
4 | [![Build Status](https://travis-ci.org/graphprotocol/graph-ts.svg?branch=master)](https://travis-ci.org/graphprotocol/graph-ts)
|
5 |
|
6 | TypeScript/AssemblyScript library for writing subgraph mappings to be deployed to
|
7 | [The Graph](https://github.com/graphprotocol/graph-node).
|
8 |
|
9 | ## Usage
|
10 |
|
11 | For a detailed guide on how to create a subgraph, please see the
|
12 | [Graph CLI docs](https://github.com/graphprotocol/graph-cli).
|
13 |
|
14 | One step of creating the subgraph is writing mappings that will process blockchain events and will
|
15 | write entities into the store. These mappings are written in TypeScript/AssemblyScript.
|
16 |
|
17 | The `graph-ts` library provides APIs to access the Graph Node store, blockchain data, smart
|
18 | contracts, data on IPFS, cryptographic functions and more. To use it, all you have to do is add a
|
19 | dependency on it:
|
20 |
|
21 | ```sh
|
22 | npm install --dev @graphprotocol/graph-ts # NPM
|
23 | yarn add --dev @graphprotocol/graph-ts # Yarn
|
24 | ```
|
25 |
|
26 | After that, you can import the `store` API and other features from this library in your mappings. A
|
27 | few examples:
|
28 |
|
29 | ```typescript
|
30 | import { store, crypto } from '@graphprotocol/graph-ts'
|
31 |
|
32 | // This is just an example event type generated by `graph-cli`
|
33 | // from an Ethereum smart contract ABI
|
34 | import { NameRegistered } from './types/abis/SomeContract'
|
35 |
|
36 | // This is an example of an entity type generated from a
|
37 | // subgraph's GraphQL schema
|
38 | import { Domain } from './types/schema'
|
39 |
|
40 | function handleNameRegistered(event: NameRegistered) {
|
41 | // Example use of a crypto function
|
42 | let id = crypto.keccak256(name).toHexString()
|
43 |
|
44 | // Example use of the generated `Entry` class
|
45 | let domain = new Domain()
|
46 | domain.name = name
|
47 | domain.owner = event.params.owner
|
48 | domain.timeRegistered = event.block.timestamp
|
49 |
|
50 | // Example use of the store API
|
51 | store.set('Name', id, entity)
|
52 | }
|
53 | ```
|
54 |
|
55 | ## Helper Functions for AssemblyScript
|
56 |
|
57 | Refer to the `helper-functions.ts` file in this repository for a few common functions that help
|
58 | build on top of the AssemblyScript library, such as byte array concatenation, among others.
|
59 |
|
60 | ## API
|
61 |
|
62 | Documentation on the API can be found
|
63 | [here](https://thegraph.com/docs/en/developer/assemblyscript-api/).
|
64 |
|
65 | For examples of `graph-ts` in use take a look at one of the following subgraphs:
|
66 |
|
67 | - https://github.com/graphprotocol/ens-subgraph
|
68 | - https://github.com/graphprotocol/decentraland-subgraph
|
69 | - https://github.com/graphprotocol/adchain-subgraph
|
70 | - https://github.com/graphprotocol/0x-subgraph
|
71 | - https://github.com/graphprotocol/aragon-subgraph
|
72 | - https://github.com/graphprotocol/dharma-subgraph
|
73 |
|
74 | ## License
|
75 |
|
76 | Copyright © 2018 Graph Protocol, Inc. and contributors.
|
77 |
|
78 | The Graph TypeScript library is dual-licensed under the [MIT license](LICENSE-MIT) and the
|
79 | [Apache License, Version 2.0](LICENSE-APACHE).
|
80 |
|
81 | Unless required by applicable law or agreed to in writing, software distributed under the License is
|
82 | distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
83 | implied. See the License for the specific language governing permissions and limitations under the
|
84 | License.
|