2021-09-16 11:36:10 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 Vulcanize, Inc.
|
|
|
|
//
|
|
|
|
|
2021-09-23 11:25:46 +00:00
|
|
|
import assert from 'assert';
|
2022-11-17 06:32:08 +00:00
|
|
|
import { GraphQLSchema, parse, printSchema, print, GraphQLDirective, GraphQLInt, GraphQLBoolean } from 'graphql';
|
2022-08-04 11:39:30 +00:00
|
|
|
import { ObjectTypeComposer, ObjectTypeComposerDefinition, ObjectTypeComposerFieldConfigMapDefinition, SchemaComposer } from 'graphql-compose';
|
2021-09-16 11:36:10 +00:00
|
|
|
import { Writable } from 'stream';
|
2022-06-29 06:33:24 +00:00
|
|
|
import { utils } from 'ethers';
|
2021-09-16 11:36:10 +00:00
|
|
|
|
2023-04-20 08:01:41 +00:00
|
|
|
import { getGqlForTs, getGqlForSol } from './utils/type-mappings';
|
2021-09-23 11:25:46 +00:00
|
|
|
import { Param } from './utils/types';
|
2023-04-20 08:01:41 +00:00
|
|
|
import { getBaseType, isArrayType } from './utils/helpers';
|
2021-09-16 11:36:10 +00:00
|
|
|
|
|
|
|
export class Schema {
|
|
|
|
_composer: SchemaComposer;
|
|
|
|
_events: Array<string>;
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
this._composer = new SchemaComposer();
|
|
|
|
this._events = [];
|
|
|
|
|
2022-11-17 06:32:08 +00:00
|
|
|
this._addGQLCacheTypes();
|
2021-09-16 11:36:10 +00:00
|
|
|
this._addBasicTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a query to the schema with the given parameters.
|
|
|
|
* @param name Name of the query.
|
|
|
|
* @param params Parameters to the query.
|
|
|
|
* @param returnType Return type for the query.
|
|
|
|
*/
|
2023-04-20 08:01:41 +00:00
|
|
|
addQuery (name: string, params: Array<Param>, typeName: any): void {
|
2021-11-12 12:39:03 +00:00
|
|
|
// Check if the query is already added.
|
|
|
|
if (this._composer.Query.hasField(name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:36:10 +00:00
|
|
|
// TODO: Handle cases where returnType/params type is an array.
|
2023-04-20 08:01:41 +00:00
|
|
|
const isReturnTypeArray = isArrayType(typeName);
|
|
|
|
const baseTypeName = getBaseType(typeName);
|
|
|
|
assert(baseTypeName);
|
|
|
|
|
|
|
|
const gqlReturnType = getGqlForSol(baseTypeName);
|
|
|
|
assert(gqlReturnType, `gql type for sol type ${baseTypeName} for ${name} not found`);
|
|
|
|
|
|
|
|
const objectTC = this._getOrCreateResultType(gqlReturnType, isReturnTypeArray);
|
2021-09-23 11:25:46 +00:00
|
|
|
|
2021-09-16 11:36:10 +00:00
|
|
|
const queryObject: { [key: string]: any; } = {};
|
|
|
|
queryObject[name] = {
|
|
|
|
// Get type composer object for return type from the schema composer.
|
2023-04-20 08:01:41 +00:00
|
|
|
type: objectTC.NonNull,
|
2021-09-16 11:36:10 +00:00
|
|
|
args: {
|
|
|
|
blockHash: 'String!',
|
|
|
|
contractAddress: 'String!'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (params.length > 0) {
|
|
|
|
queryObject[name].args = params.reduce((acc, curr) => {
|
2023-04-20 08:01:41 +00:00
|
|
|
acc[curr.name] = `${getGqlForSol(curr.type)}!`;
|
2021-09-16 11:36:10 +00:00
|
|
|
return acc;
|
|
|
|
}, queryObject[name].args);
|
|
|
|
}
|
|
|
|
// Add a query to the schema composer using queryObject.
|
|
|
|
this._composer.Query.addFields(queryObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a type to the schema for an event.
|
|
|
|
* @param name Event name.
|
|
|
|
* @param params Event parameters.
|
|
|
|
*/
|
2022-06-29 06:33:24 +00:00
|
|
|
addEventType (name: string, params: Array<utils.ParamType>): void {
|
2021-09-16 11:36:10 +00:00
|
|
|
name = `${name}Event`;
|
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
// Check if the type is already added.
|
|
|
|
if (this._composer.has(name)) {
|
2021-12-24 07:24:08 +00:00
|
|
|
this._resolveEventConflict(name, params);
|
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-04 11:39:30 +00:00
|
|
|
this._createObjectType(name, params);
|
2021-09-16 11:36:10 +00:00
|
|
|
this._events.push(name);
|
|
|
|
this._addToEventUnion(name);
|
|
|
|
|
|
|
|
if (this._events.length === 1) {
|
|
|
|
this._addEventsRelatedTypes();
|
|
|
|
this._addEventsQuery();
|
|
|
|
this._addEventSubscription();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the schema from the schema composer.
|
|
|
|
* @returns GraphQLSchema object.
|
|
|
|
*/
|
|
|
|
buildSchema (): GraphQLSchema {
|
2021-09-29 08:19:01 +00:00
|
|
|
// Add a mutation for watching a contract.
|
|
|
|
this._addWatchContractMutation();
|
|
|
|
|
2022-08-09 07:55:46 +00:00
|
|
|
// Add type and query for SyncStatus.
|
|
|
|
this._addSyncStatus();
|
|
|
|
|
2022-10-19 09:54:14 +00:00
|
|
|
// Add State type and queries.
|
|
|
|
this._addStateType();
|
|
|
|
this._addStateQuery();
|
2021-10-12 10:32:56 +00:00
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
// Build the schema.
|
2021-09-16 11:36:10 +00:00
|
|
|
return this._composer.buildSchema();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes schema to a stream.
|
|
|
|
* @param outStream A writable output stream to write the schema to.
|
2021-11-12 12:39:03 +00:00
|
|
|
* @returns The schema string.
|
2021-09-16 11:36:10 +00:00
|
|
|
*/
|
2021-10-04 05:34:06 +00:00
|
|
|
exportSchema (outStream: Writable): string {
|
2021-09-16 11:36:10 +00:00
|
|
|
// Get schema as a string from GraphQLSchema.
|
2021-09-23 11:25:46 +00:00
|
|
|
const schemaString = printSchema(this.buildSchema());
|
|
|
|
outStream.write(schemaString);
|
2021-10-04 05:34:06 +00:00
|
|
|
|
|
|
|
return schemaString;
|
2021-09-16 11:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
addSubgraphSchema (subgraphSchemaDocument: any): void {
|
|
|
|
// Generating the current types.
|
|
|
|
const schema = this._composer.buildSchema();
|
|
|
|
|
|
|
|
const schemaString = printSchema(schema);
|
|
|
|
|
|
|
|
// Parse the schema into a DocumentNode.
|
|
|
|
const schemaDocument = parse(schemaString);
|
|
|
|
|
|
|
|
// Get schema types.
|
|
|
|
const schemaTypes = schemaDocument.definitions.map((def: any) => {
|
|
|
|
return def.name.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Filtering out existing types from subgraph types.
|
|
|
|
const subgraphTypeDefs = subgraphSchemaDocument.definitions.filter((def: any) => {
|
|
|
|
return !schemaTypes.includes(def.name.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Re-assigning the typeDefs.
|
2022-01-06 13:02:08 +00:00
|
|
|
// Using JSON stringify and parse as lodash cloneDeep throws error.
|
|
|
|
const modifiedSchemaDocument = JSON.parse(JSON.stringify(subgraphSchemaDocument));
|
2021-11-12 12:39:03 +00:00
|
|
|
modifiedSchemaDocument.definitions = subgraphTypeDefs;
|
|
|
|
|
|
|
|
// Adding subgraph-schema types to the schema composer.
|
|
|
|
const subgraphTypeDefsString = print(modifiedSchemaDocument);
|
|
|
|
this._composer.addTypeDefs(subgraphTypeDefsString);
|
|
|
|
|
2021-12-23 05:39:17 +00:00
|
|
|
// Create the Block_height input needed in subgraph queries.
|
|
|
|
const typeComposer = this._composer.createInputTC({
|
|
|
|
name: 'Block_height',
|
|
|
|
fields: {
|
|
|
|
hash: 'Bytes',
|
|
|
|
number: 'Int'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
// Add subgraph-schema entity queries to the schema composer.
|
|
|
|
this._addSubgraphSchemaQueries(subgraphTypeDefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
_addSubgraphSchemaQueries (subgraphTypeDefs: any): void {
|
|
|
|
for (const subgraphTypeDef of subgraphTypeDefs) {
|
|
|
|
// Filtering out enums.
|
|
|
|
if (subgraphTypeDef.kind !== 'ObjectTypeDefinition') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const subgraphType = subgraphTypeDef.name.value;
|
|
|
|
|
|
|
|
// Lowercase first letter for query name.
|
|
|
|
const queryName = `${subgraphType.charAt(0).toLowerCase()}${subgraphType.slice(1)}`;
|
|
|
|
|
|
|
|
const queryObject: { [key: string]: any; } = {};
|
|
|
|
queryObject[queryName] = {
|
|
|
|
// Get type composer object for return type from the schema composer.
|
|
|
|
type: this._composer.getAnyTC(subgraphType).NonNull,
|
|
|
|
args: {
|
2022-01-17 09:48:44 +00:00
|
|
|
id: 'ID!',
|
2021-12-23 05:39:17 +00:00
|
|
|
block: 'Block_height'
|
2021-11-12 12:39:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this._composer.Query.addFields(queryObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:36:10 +00:00
|
|
|
/**
|
|
|
|
* Adds basic types to the schema and typemapping.
|
|
|
|
*/
|
|
|
|
_addBasicTypes (): void {
|
2021-11-12 12:39:03 +00:00
|
|
|
let typeComposer;
|
|
|
|
|
2021-09-16 11:36:10 +00:00
|
|
|
// Create a scalar type composer to add the scalar BigInt in the schema composer.
|
2021-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createScalarTC({
|
2021-09-16 11:36:10 +00:00
|
|
|
name: 'BigInt'
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-09-16 11:36:10 +00:00
|
|
|
|
2021-12-23 03:51:42 +00:00
|
|
|
// Create a scalar type composer to add the scalar BigDecimal in the schema composer.
|
|
|
|
typeComposer = this._composer.createScalarTC({
|
|
|
|
name: 'BigDecimal'
|
|
|
|
});
|
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
|
|
|
|
|
|
|
// Create a scalar type composer to add the scalar Bytes in the schema composer.
|
|
|
|
typeComposer = this._composer.createScalarTC({
|
|
|
|
name: 'Bytes'
|
|
|
|
});
|
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
|
|
|
|
2021-09-16 11:36:10 +00:00
|
|
|
// Create a type composer to add the type Proof in the schema composer.
|
2021-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createObjectTC({
|
2021-09-16 11:36:10 +00:00
|
|
|
name: 'Proof',
|
|
|
|
fields: {
|
|
|
|
data: 'String!'
|
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-09-16 11:36:10 +00:00
|
|
|
|
2021-09-17 11:40:08 +00:00
|
|
|
// Create the Block type.
|
2021-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createObjectTC({
|
2021-12-23 03:51:42 +00:00
|
|
|
name: '_Block_',
|
2021-09-16 11:36:10 +00:00
|
|
|
fields: {
|
2021-10-12 10:32:56 +00:00
|
|
|
cid: 'String!',
|
2021-09-17 11:40:08 +00:00
|
|
|
hash: 'String!',
|
|
|
|
number: 'Int!',
|
|
|
|
timestamp: 'Int!',
|
|
|
|
parentHash: 'String!'
|
2021-09-16 11:36:10 +00:00
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-12-23 03:51:42 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 08:01:41 +00:00
|
|
|
/**
|
|
|
|
* Adds Result types to the schema and typemapping.
|
|
|
|
*/
|
|
|
|
_getOrCreateResultType (typeName: string, isArray = false): ObjectTypeComposer<any, any> {
|
|
|
|
const value = `${typeName}!`;
|
|
|
|
|
|
|
|
let objectTCName = `Result${typeName}`;
|
|
|
|
if (isArray) {
|
|
|
|
objectTCName = objectTCName.concat('Array');
|
|
|
|
}
|
|
|
|
|
|
|
|
const typeComposer = this._composer.getOrCreateOTC(
|
|
|
|
objectTCName,
|
|
|
|
(tc) => {
|
|
|
|
tc.addFields({
|
|
|
|
value: (isArray) ? `[${value}]!` : value,
|
|
|
|
proof: () => this._composer.getOTC('Proof')
|
|
|
|
});
|
2023-04-24 06:34:24 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Using this to declare result types before queries
|
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
|
|
|
|
2023-04-20 08:01:41 +00:00
|
|
|
return typeComposer;
|
|
|
|
}
|
|
|
|
|
2022-11-17 06:32:08 +00:00
|
|
|
_addGQLCacheTypes (): void {
|
|
|
|
// Create a enum type composer to add enum CacheControlScope in the schema composer.
|
|
|
|
const enumTypeComposer = this._composer.createEnumTC(`
|
|
|
|
enum CacheControlScope {
|
|
|
|
PUBLIC
|
|
|
|
PRIVATE
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
this._composer.addSchemaMustHaveType(enumTypeComposer);
|
|
|
|
|
|
|
|
// Add the directive cacheControl in the schema composer.
|
|
|
|
this._composer.addDirective(new GraphQLDirective({
|
|
|
|
name: 'cacheControl',
|
|
|
|
locations: ['FIELD_DEFINITION', 'OBJECT', 'INTERFACE', 'UNION'],
|
|
|
|
args: {
|
|
|
|
maxAge: { type: GraphQLInt },
|
|
|
|
inheritMaxAge: { type: GraphQLBoolean },
|
|
|
|
scope: { type: enumTypeComposer.getType() }
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-12-23 03:51:42 +00:00
|
|
|
/**
|
|
|
|
* Adds types 'ResultEvent' and 'WatchedEvent' to the schema.
|
|
|
|
*/
|
|
|
|
_addEventsRelatedTypes (): void {
|
|
|
|
let typeComposer;
|
2021-09-16 11:36:10 +00:00
|
|
|
|
2021-12-23 03:51:42 +00:00
|
|
|
// Create Ethereum types.
|
2021-09-17 11:40:08 +00:00
|
|
|
// Create the Transaction type.
|
2021-12-23 03:51:42 +00:00
|
|
|
const transactionName = '_Transaction_';
|
2021-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createObjectTC({
|
2021-09-17 11:40:08 +00:00
|
|
|
name: transactionName,
|
2021-09-16 11:36:10 +00:00
|
|
|
fields: {
|
2021-09-17 11:40:08 +00:00
|
|
|
hash: 'String!',
|
|
|
|
index: 'Int!',
|
|
|
|
from: 'String!',
|
|
|
|
to: 'String!'
|
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-09-17 11:40:08 +00:00
|
|
|
|
|
|
|
// Create the ResultEvent type.
|
|
|
|
const resultEventName = 'ResultEvent';
|
2021-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createObjectTC({
|
2021-09-17 11:40:08 +00:00
|
|
|
name: resultEventName,
|
|
|
|
fields: {
|
|
|
|
// Get type composer object for 'blockName' type from the schema composer.
|
2021-12-23 03:51:42 +00:00
|
|
|
block: () => this._composer.getOTC('_Block_').NonNull,
|
2021-09-17 11:40:08 +00:00
|
|
|
tx: () => this._composer.getOTC(transactionName).NonNull,
|
|
|
|
contract: 'String!',
|
|
|
|
eventIndex: 'Int!',
|
|
|
|
event: () => this._composer.getUTC('Event').NonNull,
|
|
|
|
proof: () => this._composer.getOTC('Proof')
|
2021-09-16 11:36:10 +00:00
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-09-16 11:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a query for events to the schema.
|
|
|
|
*/
|
|
|
|
_addEventsQuery (): void {
|
|
|
|
this._composer.Query.addFields({
|
|
|
|
events: {
|
|
|
|
type: [this._composer.getOTC('ResultEvent').NonNull],
|
|
|
|
args: {
|
|
|
|
blockHash: 'String!',
|
|
|
|
contractAddress: 'String!',
|
|
|
|
name: 'String'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-09-27 04:43:50 +00:00
|
|
|
|
|
|
|
this._composer.Query.addFields({
|
|
|
|
eventsInRange: {
|
|
|
|
type: [this._composer.getOTC('ResultEvent').NonNull],
|
|
|
|
args: {
|
|
|
|
fromBlockNumber: 'Int!',
|
|
|
|
toBlockNumber: 'Int!'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-09-16 11:36:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 07:55:46 +00:00
|
|
|
_addSyncStatus (): void {
|
|
|
|
const typeComposer = this._composer.createObjectTC({
|
|
|
|
name: 'SyncStatus',
|
|
|
|
fields: {
|
|
|
|
latestIndexedBlockHash: 'String!',
|
|
|
|
latestIndexedBlockNumber: 'Int!',
|
|
|
|
latestCanonicalBlockHash: 'String!',
|
|
|
|
latestCanonicalBlockNumber: 'Int!'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
|
|
|
|
|
|
|
this._composer.Query.addFields({
|
|
|
|
getSyncStatus: {
|
|
|
|
type: this._composer.getOTC('SyncStatus')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-19 09:54:14 +00:00
|
|
|
_addStateType (): void {
|
2021-11-12 12:39:03 +00:00
|
|
|
const typeComposer = this._composer.createObjectTC({
|
2022-10-19 09:54:14 +00:00
|
|
|
name: 'ResultState',
|
2021-10-12 10:32:56 +00:00
|
|
|
fields: {
|
2021-12-23 03:51:42 +00:00
|
|
|
block: () => this._composer.getOTC('_Block_').NonNull,
|
2021-10-12 10:32:56 +00:00
|
|
|
contractAddress: 'String!',
|
|
|
|
cid: 'String!',
|
|
|
|
kind: 'String!',
|
|
|
|
data: 'String!'
|
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-10-12 10:32:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 09:54:14 +00:00
|
|
|
_addStateQuery (): void {
|
2021-10-12 10:32:56 +00:00
|
|
|
this._composer.Query.addFields({
|
|
|
|
getStateByCID: {
|
2022-10-19 09:54:14 +00:00
|
|
|
type: this._composer.getOTC('ResultState'),
|
2021-10-12 10:32:56 +00:00
|
|
|
args: {
|
|
|
|
cid: 'String!'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._composer.Query.addFields({
|
|
|
|
getState: {
|
2022-10-19 09:54:14 +00:00
|
|
|
type: this._composer.getOTC('ResultState'),
|
2021-10-12 10:32:56 +00:00
|
|
|
args: {
|
|
|
|
blockHash: 'String!',
|
2021-10-14 10:38:45 +00:00
|
|
|
contractAddress: 'String!',
|
|
|
|
kind: 'String'
|
2021-10-12 10:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:36:10 +00:00
|
|
|
/**
|
|
|
|
* Adds an event subscription to the schema.
|
|
|
|
*/
|
|
|
|
_addEventSubscription (): void {
|
|
|
|
// Add a subscription to the schema composer.
|
|
|
|
this._composer.Subscription.addFields({
|
2021-09-17 11:40:08 +00:00
|
|
|
onEvent: () => this._composer.getOTC('ResultEvent').NonNull
|
2021-09-16 11:36:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-29 08:19:01 +00:00
|
|
|
/**
|
|
|
|
* Adds a watchContract mutation to the schema.
|
|
|
|
*/
|
|
|
|
_addWatchContractMutation (): void {
|
|
|
|
// Add a mutation to the schema composer.
|
|
|
|
this._composer.Mutation.addFields({
|
|
|
|
watchContract: {
|
|
|
|
type: 'Boolean!',
|
|
|
|
args: {
|
2021-10-20 12:25:54 +00:00
|
|
|
address: 'String!',
|
2021-10-12 10:32:56 +00:00
|
|
|
kind: 'String!',
|
2021-10-20 12:25:54 +00:00
|
|
|
checkpoint: 'Boolean!',
|
2021-09-29 08:19:01 +00:00
|
|
|
startingBlock: 'Int'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:36:10 +00:00
|
|
|
/**
|
|
|
|
* Adds an 'Event' union (if doesn't exist) to the schema. Adds the specified event to the 'Event' union.
|
|
|
|
* @param event Event type name to add to the union.
|
|
|
|
*/
|
|
|
|
_addToEventUnion (event: string): void {
|
|
|
|
// Get (or create if doesn't exist) type composer object for Event union from the schema composer.
|
|
|
|
const eventUnion = this._composer.getOrCreateUTC('Event');
|
|
|
|
// Add a new type to the union.
|
|
|
|
eventUnion.addType(this._composer.getOTC(event));
|
|
|
|
}
|
2021-12-24 07:24:08 +00:00
|
|
|
|
|
|
|
_resolveEventConflict (name: string, params: Array<Param>): void {
|
|
|
|
const eventTC = this._composer.getOTC(name);
|
|
|
|
const currentFields = eventTC.getFieldNames();
|
|
|
|
|
|
|
|
// Get the common fields.
|
|
|
|
let commonFields: string[] = [];
|
|
|
|
commonFields = params.reduce((acc, curr) => {
|
|
|
|
if (currentFields.includes(curr.name)) {
|
|
|
|
acc.push(curr.name);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, commonFields);
|
|
|
|
|
|
|
|
// Make the current fields that are uncommon nullable.
|
|
|
|
currentFields.forEach((field: string) => {
|
|
|
|
if (!commonFields.includes(field)) {
|
|
|
|
eventTC.makeFieldNullable(field);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get the new fields.
|
|
|
|
const newFields: any = {};
|
|
|
|
params.forEach((param: Param) => {
|
|
|
|
if (!commonFields.includes(param.name)) {
|
2023-04-20 08:01:41 +00:00
|
|
|
newFields[param.name] = `${getGqlForTs(param.type)}`;
|
2021-12-24 07:24:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add the new fields to the current type.
|
|
|
|
eventTC.addFields(newFields);
|
|
|
|
}
|
2022-08-04 11:39:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create GraphQL schmea object type.
|
|
|
|
* @param name
|
|
|
|
* @param params
|
|
|
|
*/
|
|
|
|
_createObjectType (name: string, params: Array<utils.ParamType>): ObjectTypeComposer {
|
|
|
|
const typeObject: ObjectTypeComposerDefinition<any, any> = { name };
|
|
|
|
|
|
|
|
if (params.length > 0) {
|
|
|
|
typeObject.fields = params.reduce((acc: ObjectTypeComposerFieldConfigMapDefinition<any, any>, curr) => {
|
|
|
|
acc[curr.name] = this._getObjectTypeField(curr);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
} else {
|
|
|
|
// Types must define one or more fields.
|
|
|
|
typeObject.fields = {
|
|
|
|
dummy: 'String'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a type composer to add the required type in the schema composer.
|
|
|
|
return this._composer.createObjectTC(typeObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get type of field in GraphQL schema for object types.
|
|
|
|
* @param param
|
|
|
|
*/
|
|
|
|
_getObjectTypeField (param: utils.ParamType): ObjectTypeComposer | string | any[] {
|
|
|
|
if (param.indexed && ['string', 'bytes', 'tuple', 'array'].includes(param.baseType)) {
|
|
|
|
// Check for indexed reference type event params.
|
|
|
|
param = utils.ParamType.fromObject({ type: 'bytes32', name: param.name });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param.baseType === 'tuple') {
|
|
|
|
const typeName = param.name.charAt(0).toUpperCase() + param.name.slice(1);
|
|
|
|
return this._createObjectType(typeName, param.components);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param.baseType === 'array') {
|
|
|
|
return [this._getObjectTypeField(param.arrayChildren)];
|
|
|
|
}
|
|
|
|
|
2023-04-20 08:01:41 +00:00
|
|
|
return `${getGqlForSol(param.type)}!`;
|
2022-08-04 11:39:30 +00:00
|
|
|
}
|
2021-09-16 11:36:10 +00:00
|
|
|
}
|