2021-09-16 11:36:10 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 Vulcanize, Inc.
|
|
|
|
//
|
|
|
|
|
2021-09-23 11:25:46 +00:00
|
|
|
import assert from 'assert';
|
2021-11-12 12:39:03 +00:00
|
|
|
import { GraphQLSchema, parse, printSchema, print } from 'graphql';
|
2021-09-16 11:36:10 +00:00
|
|
|
import { SchemaComposer } from 'graphql-compose';
|
|
|
|
import { Writable } from 'stream';
|
2022-06-29 06:33:24 +00:00
|
|
|
import { utils } from 'ethers';
|
2021-09-16 11:36:10 +00:00
|
|
|
|
2021-09-23 11:25:46 +00:00
|
|
|
import { getTsForSol, getGqlForTs } from './utils/type-mappings';
|
|
|
|
import { Param } from './utils/types';
|
2021-09-16 11:36:10 +00:00
|
|
|
|
|
|
|
export class Schema {
|
|
|
|
_composer: SchemaComposer;
|
|
|
|
_events: Array<string>;
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
this._composer = new SchemaComposer();
|
|
|
|
this._events = [];
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
addQuery (name: string, params: Array<Param>, returnType: string): 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.
|
2021-09-23 11:25:46 +00:00
|
|
|
const tsReturnType = getTsForSol(returnType);
|
2021-12-21 08:20:22 +00:00
|
|
|
assert(tsReturnType, `ts type for sol type ${returnType} for ${name} not found`);
|
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.
|
2021-09-23 11:25:46 +00:00
|
|
|
type: this._composer.getOTC(`Result${getGqlForTs(tsReturnType)}`).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) => {
|
2021-09-23 11:25:46 +00:00
|
|
|
const tsCurrType = getTsForSol(curr.type);
|
2021-12-21 08:20:22 +00:00
|
|
|
assert(tsCurrType, `ts type for sol type ${curr.type} for ${curr.name} not found`);
|
2021-09-23 11:25:46 +00:00
|
|
|
acc[curr.name] = `${getGqlForTs(tsCurrType)}!`;
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:36:10 +00:00
|
|
|
const typeObject: any = {};
|
|
|
|
typeObject.name = name;
|
|
|
|
typeObject.fields = {};
|
|
|
|
|
|
|
|
if (params.length > 0) {
|
|
|
|
typeObject.fields = params.reduce((acc, curr) => {
|
2021-09-23 11:25:46 +00:00
|
|
|
const tsCurrType = getTsForSol(curr.type);
|
2021-12-21 08:20:22 +00:00
|
|
|
assert(tsCurrType, `ts type for sol type ${curr.type} for ${curr.name} not found`);
|
2021-09-23 11:25:46 +00:00
|
|
|
acc[curr.name] = `${getGqlForTs(tsCurrType)}!`;
|
2021-09-16 11:36:10 +00:00
|
|
|
return acc;
|
|
|
|
}, typeObject.fields);
|
2021-12-23 11:12:02 +00:00
|
|
|
} else {
|
|
|
|
// Types must define one or more fields.
|
|
|
|
typeObject.fields = {
|
|
|
|
dummy: 'String'
|
|
|
|
};
|
2021-09-16 11:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a type composer to add the required type in the schema composer.
|
|
|
|
this._composer.createObjectTC(typeObject);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
// Add IPLDBlock type and queries.
|
2021-10-12 10:32:56 +00:00
|
|
|
this._addIPLDType();
|
|
|
|
this._addIPLDQuery();
|
|
|
|
|
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-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createObjectTC({
|
2021-09-16 11:36:10 +00:00
|
|
|
name: 'ResultBoolean',
|
|
|
|
fields: {
|
|
|
|
value: 'Boolean!',
|
|
|
|
proof: () => this._composer.getOTC('Proof')
|
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-09-16 11:36:10 +00:00
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createObjectTC({
|
2021-09-16 11:36:10 +00:00
|
|
|
name: 'ResultString',
|
|
|
|
fields: {
|
|
|
|
value: 'String!',
|
|
|
|
proof: () => this._composer.getOTC('Proof')
|
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-09-16 11:36:10 +00:00
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createObjectTC({
|
2021-09-16 11:36:10 +00:00
|
|
|
name: 'ResultInt',
|
|
|
|
fields: {
|
|
|
|
value: () => 'Int!',
|
|
|
|
proof: () => this._composer.getOTC('Proof')
|
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
2021-09-16 11:36:10 +00:00
|
|
|
|
2021-11-12 12:39:03 +00:00
|
|
|
typeComposer = this._composer.createObjectTC({
|
2021-09-16 11:36:10 +00:00
|
|
|
name: 'ResultBigInt',
|
|
|
|
fields: {
|
|
|
|
// Get type composer object for BigInt scalar from the schema composer.
|
|
|
|
value: () => this._composer.getSTC('BigInt').NonNull,
|
|
|
|
proof: () => this._composer.getOTC('Proof')
|
|
|
|
}
|
|
|
|
});
|
2021-11-12 12:39:03 +00:00
|
|
|
this._composer.addSchemaMustHaveType(typeComposer);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2021-10-12 10:32:56 +00:00
|
|
|
_addIPLDType (): void {
|
2021-11-12 12:39:03 +00:00
|
|
|
const typeComposer = this._composer.createObjectTC({
|
2021-10-12 10:32:56 +00:00
|
|
|
name: 'ResultIPLDBlock',
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
_addIPLDQuery (): void {
|
|
|
|
this._composer.Query.addFields({
|
|
|
|
getStateByCID: {
|
|
|
|
type: this._composer.getOTC('ResultIPLDBlock'),
|
|
|
|
args: {
|
|
|
|
cid: 'String!'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._composer.Query.addFields({
|
|
|
|
getState: {
|
|
|
|
type: this._composer.getOTC('ResultIPLDBlock'),
|
|
|
|
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)) {
|
|
|
|
const tsCurrType = getTsForSol(param.type);
|
|
|
|
assert(tsCurrType, `ts type for sol type ${param.type} for ${param.name} not found`);
|
|
|
|
newFields[param.name] = `${getGqlForTs(tsCurrType)}`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add the new fields to the current type.
|
|
|
|
eventTC.addFields(newFields);
|
|
|
|
}
|
2021-09-16 11:36:10 +00:00
|
|
|
}
|