Skip to content

Commit

Permalink
Codegen map Solidity types directly to GraphQL types
Browse files Browse the repository at this point in the history
  • Loading branch information
dafaqdhruv committed Apr 14, 2023
1 parent 050f12d commit d474ecb
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 19 deletions.
26 changes: 8 additions & 18 deletions packages/codegen/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import assert from 'assert';
import { GraphQLSchema, parse, printSchema, print, GraphQLDirective, GraphQLInt, GraphQLBoolean } from 'graphql';
import { NonNullComposer, ObjectTypeComposer, ObjectTypeComposerDefinition, ObjectTypeComposerFieldConfigMapDefinition, ScalarTypeComposer, SchemaComposer } from 'graphql-compose';
import { ObjectTypeComposer, ObjectTypeComposerDefinition, ObjectTypeComposerFieldConfigMapDefinition, SchemaComposer } from 'graphql-compose';
import { Writable } from 'stream';
import { utils } from 'ethers';

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

Expand Down Expand Up @@ -41,10 +41,8 @@ export class Schema {
const baseTypeName = getBaseType(typeName);
assert(baseTypeName);

const tsReturnType = getTsForSol(baseTypeName);
assert(tsReturnType, `ts type for sol type ${baseTypeName} for ${name} not found`);
const gqlReturnType = getGqlForTs(tsReturnType);
assert(gqlReturnType, `gql type for ts type ${tsReturnType} for ${name} not found`);
const gqlReturnType = getGqlForSol(baseTypeName);
assert(gqlReturnType, `gql type for sol type ${baseTypeName} for ${name} not found`);

const objectTC = this._getOrCreateResultType(gqlReturnType, isReturnTypeArray);

Expand All @@ -60,9 +58,7 @@ export class Schema {

if (params.length > 0) {
queryObject[name].args = params.reduce((acc, curr) => {
const tsCurrType = getTsForSol(curr.type);
assert(tsCurrType, `ts type for sol type ${curr.type} for ${curr.name} not found`);
acc[curr.name] = `${getGqlForTs(tsCurrType)}!`;
acc[curr.name] = `${getGqlForSol(curr.type)}!`;
return acc;
}, queryObject[name].args);
}
Expand Down Expand Up @@ -247,9 +243,7 @@ export class Schema {
* Adds Result types to the schema and typemapping.
*/
_getOrCreateResultType (typeName: string, isArray = false): ObjectTypeComposer<any, any> {
const value: string | (() => NonNullComposer<ScalarTypeComposer<any>>) = (typeName === 'BigInt')
? () => this._composer.getSTC('BigInt').NonNull
: `${typeName}!`;
const value = `${typeName}!`;

let objectTCName = `Result${typeName}`;
if (isArray) {
Expand Down Expand Up @@ -471,9 +465,7 @@ export class Schema {
const newFields: any = {};
params.forEach((param: Param) => {
if (!commonFields.includes(param.name)) {
const tsCurrType = getTsForSol(param.type);
assert(tsCurrType, `ts type for sol type ${param.type} for ${param.name} not found`);
newFields[param.name] = `${getGqlForTs(tsCurrType)}`;
newFields[param.name] = `${getGqlForTs(param.type)}`;
}
});

Expand Down Expand Up @@ -525,8 +517,6 @@ export class Schema {
return [this._getObjectTypeField(param.arrayChildren)];
}

const tsCurrType = getTsForSol(param.type);
assert(tsCurrType, `ts type for sol type ${param.type} for ${param.name} not found`);
return `${getGqlForTs(tsCurrType)}!`;
return `${getGqlForSol(param.type)}!`;
}
}
112 changes: 112 additions & 0 deletions packages/codegen/src/utils/solToGql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// Copyright 2022 Vulcanize, Inc.
//

const solToGql: Map<string, string> = new Map();

// Solidity to GraphQL type-mapping.
solToGql.set('string', 'String');
solToGql.set('address', 'String');
solToGql.set('bool', 'Boolean');

solToGql.set('int8', 'Int');
solToGql.set('int16', 'Int');
solToGql.set('int24', 'Int');
solToGql.set('int32', 'Int');
solToGql.set('int48', 'BigInt');
solToGql.set('int56', 'BigInt');
solToGql.set('int64', 'BigInt');
solToGql.set('int72', 'BigInt');
solToGql.set('int80', 'BigInt');
solToGql.set('int88', 'BigInt');
solToGql.set('int96', 'BigInt');
solToGql.set('int104', 'BigInt');
solToGql.set('int112', 'BigInt');
solToGql.set('int120', 'BigInt');
solToGql.set('int128', 'BigInt');
solToGql.set('int136', 'BigInt');
solToGql.set('int144', 'BigInt');
solToGql.set('int152', 'BigInt');
solToGql.set('int160', 'BigInt');
solToGql.set('int168', 'BigInt');
solToGql.set('int176', 'BigInt');
solToGql.set('int184', 'BigInt');
solToGql.set('int192', 'BigInt');
solToGql.set('int200', 'BigInt');
solToGql.set('int208', 'BigInt');
solToGql.set('int216', 'BigInt');
solToGql.set('int224', 'BigInt');
solToGql.set('int232', 'BigInt');
solToGql.set('int240', 'BigInt');
solToGql.set('int248', 'BigInt');
solToGql.set('int256', 'BigInt');
solToGql.set('int', 'BigInt');

solToGql.set('uint8', 'Int');
solToGql.set('uint16', 'Int');
solToGql.set('uint24', 'Int');
solToGql.set('uint32', 'BigInt');
solToGql.set('uint48', 'BigInt');
solToGql.set('uint56', 'BigInt');
solToGql.set('uint64', 'BigInt');
solToGql.set('uint72', 'BigInt');
solToGql.set('uint80', 'BigInt');
solToGql.set('uint88', 'BigInt');
solToGql.set('uint96', 'BigInt');
solToGql.set('uint104', 'BigInt');
solToGql.set('uint112', 'BigInt');
solToGql.set('uint120', 'BigInt');
solToGql.set('uint128', 'BigInt');
solToGql.set('uint136', 'BigInt');
solToGql.set('uint144', 'BigInt');
solToGql.set('uint152', 'BigInt');
solToGql.set('uint160', 'BigInt');
solToGql.set('uint168', 'BigInt');
solToGql.set('uint176', 'BigInt');
solToGql.set('uint184', 'BigInt');
solToGql.set('uint192', 'BigInt');
solToGql.set('uint200', 'BigInt');
solToGql.set('uint208', 'BigInt');
solToGql.set('uint216', 'BigInt');
solToGql.set('uint224', 'BigInt');
solToGql.set('uint232', 'BigInt');
solToGql.set('uint240', 'BigInt');
solToGql.set('uint248', 'BigInt');
solToGql.set('uint256', 'BigInt');
solToGql.set('uint', 'BigInt');

solToGql.set('bytes', 'String');
solToGql.set('bytes1', 'String');
solToGql.set('bytes2', 'String');
solToGql.set('bytes3', 'String');
solToGql.set('bytes4', 'String');
solToGql.set('bytes5', 'String');
solToGql.set('bytes6', 'String');
solToGql.set('bytes7', 'String');
solToGql.set('bytes8', 'String');
solToGql.set('bytes9', 'String');
solToGql.set('bytes10', 'String');
solToGql.set('bytes11', 'String');
solToGql.set('bytes12', 'String');
solToGql.set('bytes13', 'String');
solToGql.set('bytes14', 'String');
solToGql.set('bytes15', 'String');
solToGql.set('bytes16', 'String');
solToGql.set('bytes17', 'String');
solToGql.set('bytes18', 'String');
solToGql.set('bytes19', 'String');
solToGql.set('bytes20', 'String');
solToGql.set('bytes21', 'String');
solToGql.set('bytes22', 'String');
solToGql.set('bytes23', 'String');
solToGql.set('bytes24', 'String');
solToGql.set('bytes25', 'String');
solToGql.set('bytes26', 'String');
solToGql.set('bytes27', 'String');
solToGql.set('bytes28', 'String');
solToGql.set('bytes29', 'String');
solToGql.set('bytes30', 'String');
solToGql.set('bytes31', 'String');
solToGql.set('bytes32', 'String');

export { solToGql };
7 changes: 6 additions & 1 deletion packages/codegen/src/utils/type-mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2021 Vulcanize, Inc.
//

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

const _tsToGql: Map<string, string> = new Map();
Expand Down Expand Up @@ -33,6 +34,10 @@ function getTsForSol (solType: string): string | undefined {
return solToTs.get(solType);
}

function getGqlForSol (solType: string): string | undefined {
return solToGql.get(solType);
}

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

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

0 comments on commit d474ecb

Please sign in to comment.