Skip to content

Commit

Permalink
Merge pull request #4 from smartweaver/crookse/refactor/graphql-changes
Browse files Browse the repository at this point in the history
refactor(modules/graphql): use GraphQL POST request body
  • Loading branch information
crookse authored Jun 25, 2024
2 parents 151906a + 24bbe4f commit e7dd1f1
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 280 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const client = new Client(aoconnect); // Create Slick Transacti
.data("some data") // Optionally, send data with this message
.post(); // Send the request (calls HTTP POST under the hood)

console.log({ newProcess: `https://www.ao.link/entity/${newProcessId}` });
console.log({ newProcess: `https://www.ao.link/#/entity/${newProcessId}` });
})();

```
2 changes: 1 addition & 1 deletion examples/aoconnect/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ const client = new Client(aoconnect); // Create Slick Transacti
.data("some data") // Optionally, send data with this message
.post(); // Send the request (calls HTTP POST under the hood)

console.log({ newProcess: `https://www.ao.link/entity/${newProcessId}` });
console.log({ newProcess: `https://www.ao.link/#/entity/${newProcessId}` });
})();
2 changes: 1 addition & 1 deletion src/modules/ao/v0/types/UnitInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type UnitInfo = {
* query block explorers. For example, the following link queries the
* aolink block explorer for the address:
*
* https://www.ao.link/entity/fcoN_xJeisVsPXA-trzVAuIiqO3ydLQxM-L4XbrQKzY
* https://www.ao.link/#/entity/fcoN_xJeisVsPXA-trzVAuIiqO3ydLQxM-L4XbrQKzY
*/
address: string;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { QueryBuilderOptions } from "../types/QueryBuilderOptions.ts";

export abstract class AbstractQueryBuilder {
protected return_schema = "{}";
export abstract class AbstractQueryBuilder<Variables> {
protected abstract query: string;

protected server_url: string;
protected variables: string;
protected operation_variables: Partial<Variables> = {};
protected return_schema: string;

constructor(options: QueryBuilderOptions) {
this.server_url = options?.server_url || "https://arweave.net/graphql";
this.variables = options?.variables || "";
}

variables(variables: Variables) {
this.operation_variables = {
...(this.operation_variables || {}),
...(variables || {}),
};

return this;
}

/**
* Build this query.
* @return The query as a string to send to the GraphQL server.
*/
abstract build(): string;
abstract build(): {
operationName: string;
query: string;
variables: Partial<Variables>;
};

/**
* Make a `fetch` request to the GraphQL server.
Expand All @@ -26,7 +40,7 @@ export abstract class AbstractQueryBuilder {
*/
post(
options: RequestInit & { url?: string } = {},
): Promise<Response & { graph<T = any>(): Promise<T> }> {
): Promise<Response> {
const query = this.build();

if (!options.url) {
Expand All @@ -40,11 +54,7 @@ export abstract class AbstractQueryBuilder {
"accpet": "application/json, text/plain, */*",
},
...(options || {}),
body: JSON.stringify({
query,
operationName: null,
variables: {},
}),
body: JSON.stringify(query),
method: "POST",
});

Expand Down Expand Up @@ -102,27 +112,14 @@ export abstract class AbstractQueryBuilder {
* ```
*/
returnSchema(schema?: string) {
this.return_schema = schema || "{}";
this.return_schema = schema;
return this;
}

protected _concat(values: string[]) {
let ret = null;

if (values && Array.isArray(values)) {
const concatted = values
.map((value) => {
return `"${value}"`;
})
.join("\n,");

ret = `[${concatted}]`;
}

return ret;
}

protected stringExists(str: unknown) {
return str && (typeof str === "string") && str.trim() !== "";
protected buildQuery() {
return this.query.replace(
/\{\{ return_schema \}\}/g,
this.return_schema || "{}",
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { QueryBuilderOptions } from "../types/QueryBuilderOptions.ts";
import { QueryTransactionArgs } from "../types/Schema.ts";
import { AbstractQueryBuilder } from "./AbstractQueryBuilder.ts";

const GetTransactionOperation = `query GetTransaction(
$id: ID!
) {
transaction(id: $id) {
{{ return_schema }}
}
}
`;

export class TransactionQueryBuilder
extends AbstractQueryBuilder<QueryTransactionArgs> {
protected query = GetTransactionOperation;

constructor(options?: QueryBuilderOptions) {
super(options);
this.returnSchema(`
id
owner {
address
}
block {
height
timestamp
}
tags {
name
value
}
`);
}

build() {
return {
operationName: `GetTransaction`,
query: this.buildQuery(),
variables: this.operation_variables,
};
}

id(id: string) {
this.operation_variables.id = id;
return this;
}
}
121 changes: 121 additions & 0 deletions src/modules/graphql/arweave.net/builders/TransactionsQueryBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { QueryBuilderOptions } from "../types/QueryBuilderOptions.ts";
import { QueryTransactionsArgs, TagFilter } from "../types/Schema.ts";
import { AbstractQueryBuilder } from "./AbstractQueryBuilder.ts";

const GetTransactionsOperations = `query GetTransactions(
$ids: [ID!]
$owners: [String!]
$recipients: [String!]
$tags: [TagFilter!]
$bundledIn: [ID!]
$block: BlockFilter
$first: Int = 10
$after: String
$sort: SortOrder = HEIGHT_DESC
) {
transactions(
ids: $ids
owners: $owners
recipients: $recipients
tags: $tags
bundledIn: $bundledIn
block: $block
first: $first
after: $after
sort: $sort
) {
{{ return_schema }}
}
}`;

export class TransactionsQueryBuilder
extends AbstractQueryBuilder<QueryTransactionsArgs> {
query = GetTransactionsOperations;

constructor(options?: QueryBuilderOptions) {
super(options);

this.returnSchema(`
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
owner {
address
}
block {
height
timestamp
}
tags {
name
value
}
}
}
`);
}

/**
* @returns `this` instance for further method chaining.
*/
first(first: number) {
this.operation_variables.first = first;
return this;
}

/**
* @returns `this` instance for further method chaining.
*/
after(after: string) {
this.operation_variables.after = after;
return this;
}

build() {
return {
operationName: `GetTransactions`,
query: this.buildQuery(),
variables: this.operation_variables,
};
}

/**
* @returns `this` instance for further method chaining.
*/
ids(ids: string[]) {
this.operation_variables.ids = ids;

return this;
}

/**
* @returns `this` instance for further method chaining.
*/
owners(owners: string[]) {
this.operation_variables.owners = owners;

return this;
}

/**
* @returns `this` instance for further method chaining.
*/
recipients(recipients: string[]) {
this.operation_variables.recipients = recipients;
return this;
}

/**
* Set the tags to send with the query.
* @param tags The tags in question.
* @returns `this` instance for further method chaining.
*/
tags(tags: TagFilter[]) {
this.operation_variables.tags = tags;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export type QueryBuilderOptions = {
server_url?: string;
/** GraphQL query variables */
variables?: any;
};
File renamed without changes.
51 changes: 0 additions & 51 deletions src/modules/graphql/builders/TransactionQueryBuilder.ts

This file was deleted.

Loading

0 comments on commit e7dd1f1

Please sign in to comment.