Skip to content

Commit

Permalink
Codegen get TS from GQL types
Browse files Browse the repository at this point in the history
  • Loading branch information
dafaqdhruv committed Apr 18, 2023
1 parent b7917dd commit dd2a55f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 139 deletions.
12 changes: 8 additions & 4 deletions packages/codegen/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Writable } from 'stream';
import _ from 'lodash';
import { gqlGenerate } from 'gql-generator';

import { getTsForSol } from './utils/type-mappings';
import { getGqlForSol, getTsForGql } from './utils/type-mappings';
import { Param } from './utils/types';
import { getBaseType } from './utils/helpers';

Expand Down Expand Up @@ -52,13 +52,17 @@ export class Client {
: `get${name.charAt(0).toUpperCase()}${name.slice(1)}`;

queryObject.params = queryObject.params.map((param) => {
const tsParamType = getTsForSol(param.type);
const gqlParamType = getGqlForSol(param.type);
assert(gqlParamType);
const tsParamType = getTsForGql(gqlParamType);
assert(tsParamType);
param.type = tsParamType;
param.type = gqlParamType;
return param;
});

const tsReturnType = getTsForSol(returnType);
const gqlReturnType = getGqlForSol(returnType);
assert(gqlReturnType);
const tsReturnType = getTsForGql(gqlReturnType);
assert(tsReturnType);
queryObject.returnType = tsReturnType;

Expand Down
12 changes: 8 additions & 4 deletions packages/codegen/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Handlebars from 'handlebars';
import { Writable } from 'stream';
import _ from 'lodash';

import { getTsForSol } from './utils/type-mappings';
import { getGqlForSol, getTsForGql } from './utils/type-mappings';
import { Param } from './utils/types';
import { getBaseType } from './utils/helpers';

Expand Down Expand Up @@ -65,16 +65,20 @@ export class Database {
}

queryObject.params = queryObject.params.map((param) => {
const tsParamType = getTsForSol(param.type);
const gqlParamType = getGqlForSol(param.type);
assert(gqlParamType);
const tsParamType = getTsForGql(gqlParamType);
assert(tsParamType);
param.type = tsParamType;
return param;
});

const tsReturnType = getTsForSol(returnType);
const gqlReturnType = getGqlForSol(returnType);
assert(gqlReturnType);
const tsReturnType = getTsForGql(gqlReturnType);
assert(tsReturnType);
queryObject.returnType = tsReturnType;

queryObject.returnType = tsReturnType;
this._queries.push(queryObject);
}

Expand Down
17 changes: 10 additions & 7 deletions packages/codegen/src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import yaml from 'js-yaml';
import Handlebars from 'handlebars';
import { Writable } from 'stream';

import { getTsForSol, getPgForTs, getTsForGql } from './utils/type-mappings';
import { getPgForTs, getTsForGql, getGqlForSol } from './utils/type-mappings';
import { Param } from './utils/types';
import { getFieldType } from './utils/subgraph';
import { getBaseType } from './utils/helpers';
Expand Down Expand Up @@ -110,9 +110,10 @@ export class Entity {
params.map((param) => {
const name = param.name;

const tsType = getTsForSol(param.type);
const gqlType = getGqlForSol(param.type);
assert(gqlType);
const tsType = getTsForGql(gqlType);
assert(tsType);

const pgType = getPgForTs(tsType);
assert(pgType);

Expand All @@ -137,11 +138,13 @@ export class Entity {
})
);

const returnType = getBaseType(typeName);
assert(returnType);
const tsReturnType = getTsForSol(returnType);
assert(tsReturnType);
const baseType = getBaseType(typeName);
assert(baseType);

const gqlReturnType = getGqlForSol(baseType);
assert(gqlReturnType);
const tsReturnType = getTsForGql(gqlReturnType);
assert(tsReturnType);
const pgReturnType = getPgForTs(tsReturnType);
assert(pgReturnType);

Expand Down
10 changes: 7 additions & 3 deletions packages/codegen/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Handlebars from 'handlebars';
import { Writable } from 'stream';
import _ from 'lodash';

import { getTsForSol } from './utils/type-mappings';
import { getGqlForSol, getTsForGql } from './utils/type-mappings';
import { Param } from './utils/types';
import { MODE_ETH_CALL, MODE_STORAGE } from './utils/constants';
import { getFieldType } from './utils/subgraph';
Expand Down Expand Up @@ -46,7 +46,9 @@ export class Indexer {

const baseType = getBaseType(typeName);
assert(baseType);
let tsReturnType = getTsForSol(baseType);
const gqlReturnType = getGqlForSol(baseType);
assert(gqlReturnType);
let tsReturnType = getTsForGql(gqlReturnType);
assert(tsReturnType);

const isArray = isArrayType(typeName);
Expand Down Expand Up @@ -80,7 +82,9 @@ export class Indexer {
}

queryObject.params = queryObject.params.map((param) => {
const tsParamType = getTsForSol(param.type);
const gqlParamType = getGqlForSol(param.type);
assert(gqlParamType);
const tsParamType = getTsForGql(gqlParamType);
assert(tsParamType);
param.type = tsParamType;
return param;
Expand Down
6 changes: 4 additions & 2 deletions packages/codegen/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Handlebars from 'handlebars';
import assert from 'assert';
import _ from 'lodash';

import { getTsForSol } from './utils/type-mappings';
import { getGqlForSol, getTsForGql } from './utils/type-mappings';
import { Param } from './utils/types';
import { getBaseType } from './utils/helpers';

Expand Down Expand Up @@ -47,7 +47,9 @@ export class Resolvers {
};

queryObject.params = queryObject.params.map((param) => {
const tsParamType = getTsForSol(param.type);
const gqlParamType = getGqlForSol(param.type);
assert(gqlParamType);
const tsParamType = getTsForGql(gqlParamType);
assert(tsParamType);
param.type = tsParamType;
return param;
Expand Down
2 changes: 1 addition & 1 deletion packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class Indexer implements IndexerInterface {
{{#if (compare query.returnType 'bigint[]')}}
let value = await contract.{{query.name}}(
{{~#each query.params}}{{this.name}}, {{/each}}{ blockTag: blockHash });
value = value.map(val => val.toBigInt());
value = value.map((val: ethers.BigNumber) => val.toBigInt());
{{else}}
const value = await contract.{{query.name}}(
{{~#each query.params}}{{this.name}}, {{/each}}{ blockTag: blockHash });
Expand Down
112 changes: 0 additions & 112 deletions packages/codegen/src/utils/solToTs.ts

This file was deleted.

7 changes: 1 addition & 6 deletions packages/codegen/src/utils/type-mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

import { solToGql } from './solToGql';
import { solToTs } from './solToTs';

const _tsToGql: Map<string, string> = new Map();
const _tsToPg: Map<string, string> = new Map();
Expand All @@ -30,10 +29,6 @@ _gqlToTs.set('Boolean', 'boolean');
_gqlToTs.set('BigDecimal', 'Decimal');
_gqlToTs.set('Bytes', 'string');

function getTsForSol (solType: string): string | undefined {
return solToTs.get(solType);
}

function getGqlForSol (solType: string): string | undefined {
return solToGql.get(solType);
}
Expand All @@ -50,4 +45,4 @@ function getTsForGql (gqlType: string): string | undefined {
return _gqlToTs.get(gqlType);
}

export { getTsForSol, getGqlForTs, getGqlForSol, getPgForTs, getTsForGql };
export { getGqlForTs, getGqlForSol, getPgForTs, getTsForGql };

0 comments on commit dd2a55f

Please sign in to comment.