daostack / daostack/arc.js

Auto-Generate Interfaces

Open
#429 0 comments 0 reactions 1 assignee Claimed by @dOrgJelli View on GitHub
Dominant language
TypeScript
Stars
14
Forks
13
PR merge metrics
No merged PRs in 30d

Description

* I think the biggest problem w/ the client library is its maintainability. As the subgraph schema changes (new properties, name changes, entity connections / edges), the client library has to be manually updated to reflect all of these changes. I propose that we auto-generate as much as we can. Here’s how.
* The entity classes have 3 key pieces: **Read, Write, Search**
* **Read & Search can be generated**
* **Write** will be **hand authored**
* I’d also love to lend a hand in writing this code generator, just holler :) I don’t think it’d take that long to implement (1-2 weeks), since GraphQL AST parsing libraries exist.
* Here’s a code sample (2 pages):

**Subgraph Schema (Hand Authored):**
```graphql
type DAO @entity {
id: ID!
address: Bytes! @map(type: "Address")
name: String!
nativeToken: Token!
proposals: [Proposal!] @derivedFrom(field: "dao")
...
}
```

**State + Search (Auto-Generated):**
```typescript
interface IDAOState {
id: ID,
address: Address,
name: String,
nativeToken: EntityRef
}

interface IDAOQueryFilter extends ICommonQueryOptions {
where?: {
id?: ID,
address?: Address,
name?: string,
nativeToken?: ID,
[key: string]: any
}
}

class DAOState implements IStateful {
public id: ID

constructor(public context: Arc, idOrState: ID | IDAOState) {
if (typeof idOrState === 'string') {
this.id = idOrOpts.toLowerCase()
} else {
this.id = idOrState.id
this.setState(idOrState)
}
}

public state(options: IQueryOptions) {
// query all fields...
}

public search(
context: Arc,
filter: IDAOQueryFilter = { },
options: IQueryOptions = { }
) {
// build the 'where' filter & query...
}

public proposals(
filter: IProposalQueryFilter = { },
options: IQueryOptions
) {
if (!options.where) {
options.where = {}
}
options.where.dao = this.id
return Proposal.search(this.context, filter, options)
}
}
```

**Write (Hand Authored)**
```typescript
class DAO extends DAOState {
public createProposal(...) {
// hand authored
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.