UNPKG

3.21 kBMarkdownView Raw
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
6TypeScript/AssemblyScript library for writing subgraph mappings to be deployed to
7[The Graph](https://github.com/graphprotocol/graph-node).
8
9## Usage
10
11For a detailed guide on how to create a subgraph, please see the
12[Graph CLI docs](https://github.com/graphprotocol/graph-cli).
13
14One step of creating the subgraph is writing mappings that will process blockchain events and will
15write entities into the store. These mappings are written in TypeScript/AssemblyScript.
16
17The `graph-ts` library provides APIs to access the Graph Node store, blockchain data, smart
18contracts, data on IPFS, cryptographic functions and more. To use it, all you have to do is add a
19dependency on it:
20
21```sh
22npm install --dev @graphprotocol/graph-ts # NPM
23yarn add --dev @graphprotocol/graph-ts # Yarn
24```
25
26After that, you can import the `store` API and other features from this library in your mappings. A
27few examples:
28
29```typescript
30import { 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
34import { NameRegistered } from './types/abis/SomeContract'
35
36// This is an example of an entity type generated from a
37// subgraph's GraphQL schema
38import { Domain } from './types/schema'
39
40function 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
57Refer to the `helper-functions.ts` file in this repository for a few common functions that help
58build on top of the AssemblyScript library, such as byte array concatenation, among others.
59
60## API
61
62Documentation on the API can be found
63[here](https://thegraph.com/docs/en/developer/assemblyscript-api/).
64
65For 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
76Copyright © 2018 Graph Protocol, Inc. and contributors.
77
78The Graph TypeScript library is dual-licensed under the [MIT license](LICENSE-MIT) and the
79[Apache License, Version 2.0](LICENSE-APACHE).
80
81Unless required by applicable law or agreed to in writing, software distributed under the License is
82distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
83implied. See the License for the specific language governing permissions and limitations under the
84License.