Skip to content

Commit

Permalink
refactor logic to define call arguments so they are automatically ord…
Browse files Browse the repository at this point in the history
…ered correctly
  • Loading branch information
therealemjy committed Jul 3, 2023
1 parent 0a6fac2 commit 8b30030
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 128 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-pans-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eth-graphql": patch
---

Fix issue with argument order
42 changes: 21 additions & 21 deletions packages/e2e-tests/test/finalBossCallFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ export const FINAL_BOSS_CALL_FRAGMENT = /* GraphQL */ `
}
movies(arg0: 0) {
id
title
status
director {
name
walletAddress
}
status
title
}
getAllMovies {
id
title
status
director {
name
walletAddress
}
title
}
getMultipleValues {
value0
value1
value2 {
name
walletAddress
}
value1
}
getNothing
getString
Expand All @@ -44,10 +44,10 @@ export const FINAL_BOSS_CALL_FRAGMENT = /* GraphQL */ `
value1
}
overloadedFn1 {
value0
value1
value0
}
overloadedFn2(arg0: "some-string", arg1: "10000000000000000000", arg2: "") {
overloadedFn2(arg1: "10000000000000000000", arg0: "some-string", arg2: "") {
value0
value1
}
Expand All @@ -64,12 +64,12 @@ export const FINAL_BOSS_CALL_FRAGMENT = /* GraphQL */ `
passMovie(
someMovie: {
id: "0"
title: "fake movie"
status: "1"
director: {
name: "fake director"
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
status: "1"
title: "fake movie"
}
) {
id
Expand Down Expand Up @@ -134,8 +134,8 @@ export const FINAL_BOSS_CALL_FRAGMENT = /* GraphQL */ `
}
mayhem_MULT(args: [{
arg0: "fake string 0"
someUint: 671432189
arg2: ["1000000000000000000", "200000000000000000"]
someUint: 671432189
someMovies: [
{
id: "0"
Expand All @@ -156,6 +156,7 @@ export const FINAL_BOSS_CALL_FRAGMENT = /* GraphQL */ `
}
}
]
someStatuses: [0, 1, 1, 1]
someDirectors: [
{
name: "fake director 0"
Expand All @@ -166,11 +167,20 @@ export const FINAL_BOSS_CALL_FRAGMENT = /* GraphQL */ `
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
]
someStatuses: [0, 1, 1, 1]
}, {
arg0: "fake string 1"
someUint: 671432189
arg2: ["3000000000000000000", "400000000000000000"]
arg0: "fake string 1"
someDirectors: [
{
name: "fake director 2"
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
{
name: "fake director 3"
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
]
someMovies: [
{
id: "2"
Expand All @@ -191,16 +201,6 @@ export const FINAL_BOSS_CALL_FRAGMENT = /* GraphQL */ `
}
}
]
someDirectors: [
{
name: "fake director 2"
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
{
name: "fake director 3"
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
]
someStatuses: [1, 0, 1, 0]
}]) {
passedMovies {
Expand Down
20 changes: 10 additions & 10 deletions packages/e2e-tests/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ describe('end-to-end tests', function () {
value0
value1
}
overloadedFn2(arg0: "some-string", arg1: "10000000000000000000", arg2: "") {
overloadedFn2(arg0: "some-string", arg2: "", arg1: "10000000000000000000") {
value0
value1
}
overloadedFn2_MULT(
args: [
{ arg0: "some-string-0", arg1: "10000000000000000000", arg2: "1" }
{ arg0: "some-string-1", arg1: "20000000000000000000", arg2: "8" }
{ arg1: "10000000000000000000", arg0: "some-string-0", arg2: "1" }
{ arg2: "8", arg0: "some-string-1", arg1: "20000000000000000000" }
]
) {
value0
Expand All @@ -325,13 +325,13 @@ describe('end-to-end tests', function () {
const data = await makeQuery(/* GraphQL */ `
passMovie(
someMovie: {
id: "0"
title: "fake movie"
status: "1"
id: "0"
director: {
name: "fake director"
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
title: "fake movie"
}
) {
id
Expand All @@ -346,24 +346,24 @@ describe('end-to-end tests', function () {
args: [
{
someMovie: {
id: "0"
title: "fake movie 0"
status: "1"
director: {
name: "fake director 0"
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
id: "0"
status: "1"
title: "fake movie 0"
}
}
{
someMovie: {
id: "1"
title: "fake movie 1"
status: "0"
id: "1"
director: {
name: "fake director 1"
walletAddress: "0xA5ae0b2386De51Aba852551A1EE828BfD598E111"
}
status: "0"
}
}
]
Expand Down
4 changes: 4 additions & 0 deletions packages/eth-graphql/src/createSchema/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ROOT_FIELD_NAME = 'contracts';
export const ROOT_FIELD_SINGLE_ARGUMENT_NAME = 'addresses';
export const MULT_FIELD_SUFFIX = '_MULT';
export const MULT_FIELD_SINGLE_ARGUMENT_NAME = 'args';
14 changes: 9 additions & 5 deletions packages/eth-graphql/src/createSchema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {
import EthGraphQlError from '../EthGraphQlError';
import { Config, SolidityValue } from '../types';
import filterAbiItems from '../utilities/filterAbiItems';
import {
MULT_FIELD_SINGLE_ARGUMENT_NAME,
MULT_FIELD_SUFFIX,
ROOT_FIELD_NAME,
ROOT_FIELD_SINGLE_ARGUMENT_NAME,
} from './constants';
import createGraphQlInputTypes from './createGraphQlInputTypes';
import createGraphQlMultInputTypes from './createGraphQlMultInputTypes';
import createGraphQlMultOutputType from './createGraphQlMultOutputType';
Expand All @@ -23,8 +29,6 @@ import resolve from './resolve';
import { FieldNameMapping, SharedGraphQlTypes } from './types';
import validateConfig from './validateConfig';

const ROOT_FIELD_NAME = 'contracts';
const MULT_CONTRACT_FIELD_SUFFIX = '_MULT';
const addressesGraphQlInputType = new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(GraphQLString)),
);
Expand Down Expand Up @@ -102,15 +106,15 @@ const createSchema = (config: Config) => {

// Handle argument types
if (abiInputs.length > 0) {
contractField.args = createGraphQlInputTypes({
contractField[MULT_FIELD_SINGLE_ARGUMENT_NAME] = createGraphQlInputTypes({
components: abiInputs,
contractName: contract.name,
sharedGraphQlTypes,
});

// Create a MULT field that can be used to call the same method
// with multiple sets of arguments
const multContractFieldName = `${contractFieldName}${MULT_CONTRACT_FIELD_SUFFIX}`;
const multContractFieldName = `${contractFieldName}${MULT_FIELD_SUFFIX}`;

// Add MULT field to field mapping
fieldMapping[contract.name][multContractFieldName] = {
Expand Down Expand Up @@ -154,7 +158,7 @@ const createSchema = (config: Config) => {
// wish to put the addresses of inside the config.
if (!contract.address) {
contractType.args = {
addresses: { type: addressesGraphQlInputType },
[ROOT_FIELD_SINGLE_ARGUMENT_NAME]: { type: addressesGraphQlInputType },
};

// Transform type into a list to reflect the fact an array of results
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createSchema/makeCalls/formatArgumentNodes converts argument nodes to valid arguments 1`] = `
{
"fake name 0": [
[
"fake string 0",
],
],
"fake name 1": {
"fake name 2": {
"fake name 3": "fake string 1",
},
},
"fake name 4": "fake string 2",
"fake name 5": true,
"fake name 6": "100",
"fake name 7": "0",
"fake name 8": "fake variable value",
}
`;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ArgumentNode, Kind } from 'graphql';

import formatGraphQlArgs from '../formatGraphQlArgs';
import formatArgumentNodes from '../formatArgumentNodes';

describe('createSchema/makeCalls/formatGraphQlArgs', () => {
describe('createSchema/makeCalls/formatArgumentNodes', () => {
it('converts argument nodes to valid arguments', () => {
const fakeVariableName = 'fakeVariable';
const argumentNodes: ArgumentNode[] = [
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('createSchema/makeCalls/formatGraphQlArgs', () => {
},
];

const res = formatGraphQlArgs({
const res = formatArgumentNodes({
argumentNodes,
variableValues: {
[fakeVariableName]: 'fake variable value',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ArgumentNode, GraphQLResolveInfo, Kind, ValueNode } from 'graphql';

import { SolidityValue } from '../../types';
import { ContractCallArgs } from './types';

// Extract a node's value
const getNodeValue = ({
valueNode,
variableValues,
}: {
valueNode: ValueNode;
variableValues: GraphQLResolveInfo['variableValues'];
}): SolidityValue => {
if (valueNode.kind === Kind.NULL) {
return null;
}

// Get variable node value
if (valueNode.kind === Kind.VARIABLE) {
const variableName = valueNode.name.value;
return variableValues[variableName] as SolidityValue;
}

// Convert list to array
if (valueNode.kind === Kind.LIST) {
return valueNode.values.map(node =>
getNodeValue({
valueNode: node,
variableValues,
}),
);
}

// Convert object to object
if (valueNode.kind === Kind.OBJECT) {
return valueNode.fields.reduce(
(accObject, field) => ({
...accObject,
[field.name.value]: getNodeValue({
valueNode: field.value,
variableValues,
}),
}),
{},
);
}

return valueNode.value;
};

const formatArgumentNodes = ({
argumentNodes,
variableValues,
}: {
argumentNodes: ReadonlyArray<ArgumentNode>;
variableValues: GraphQLResolveInfo['variableValues'];
}) =>
argumentNodes.reduce<ContractCallArgs>(
(accArguments, argument) => ({
...accArguments,
[argument.name.value]: getNodeValue({
valueNode: argument.value,
variableValues,
}),
}),
{},
);

export default formatArgumentNodes;
Loading

0 comments on commit 8b30030

Please sign in to comment.