Skip to content

Commit

Permalink
graphql boots
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFlicker committed Jul 13, 2023
1 parent b9320ee commit 1d4ff33
Show file tree
Hide file tree
Showing 26 changed files with 433 additions and 369 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["cblock", "Xverse"]
"cSpell.words": ["cblock", "tapkey", "Xverse"]
}
3 changes: 3 additions & 0 deletions apps/graphql-backend/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions apps/graphql-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
24 changes: 24 additions & 0 deletions apps/graphql-backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@0xflick/app-graphql-backend",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"license": "MIT",
"scripts": {
"start:dev": "node --loader ts-node/esm src/index.ts | bunyan",
"dev": "ts-node-dev --inspect -r dotenv/config -- src/index.ts | bunyan"
},
"devDependencies": {
"@types/node": "^20.4.0",
"bunyan": "^1.8.15",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
},
"dependencies": {
"@graphql-tools/load-files": "^7.0.0",
"apollo-server": "^3.12.0",
"apollo-server-core": "^3.12.0",
"dotenv": "^16.3.1",
"graphql": "^16.7.1"
}
}
58 changes: 58 additions & 0 deletions apps/graphql-backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ApolloServer } from "apollo-server";
import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
import {
createGraphqlApplication,
createContext,
} from "@0xflick/ordinals-graphql";
import {
deserializeSessionCookie,
expireSessionCookie,
serializeSessionCookie,
} from "@0xflick/ordinals-backend";

const app = await createGraphqlApplication();
const executor = app.createApolloExecutor();
const schema = app.schema;

const apolloServer = new ApolloServer({
schema,
executor,
context: ({ req, res }) => {
return {
...createContext(),
getToken: () => {
const cookieToken = deserializeSessionCookie(req.headers.cookie);
if (cookieToken) {
return cookieToken;
}
const authHeader = req.headers.authorization;
if (authHeader) {
const [type, token] = authHeader.split(" ");
if (type === "Bearer") {
return token;
}
}
return null;
},
setToken: (token: string) => {
res.setHeader("set-cookie", serializeSessionCookie(token, "/"));
},
clearToken: () => {
res.setHeader("set-cookie", expireSessionCookie("/"));
},
};
},
cors: {
origin: [
"http://localhost:3000",
"http://localhost:3001",
"http://127.0.0.1:3000",
],
credentials: true,
},
introspection: true,
plugins: [ApolloServerPluginLandingPageGraphQLPlayground],
});

const { url } = await apolloServer.listen();
console.log(`🚀 Server ready at ${url}`);
30 changes: 30 additions & 0 deletions apps/graphql-backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"extends": "@0xflick/tsconfig/tsconfig.base.json",
"compilerOptions": {
"target": "ES2022",
"lib": ["esnext"],
"module": "Node16",
"outDir": "./dist",
"composite": true,
"incremental": true,
"paths": {
"@0xflick/ordinals-graphql": ["../../packages/graphql/src/index"],
"@0xflick/ordinals-backend": ["../../packages/backend/src/index"],
"@0xflick/ordinals-models": ["../../packages/models/src/index"]
},
"plugins": []
},
"include": ["src/**/*.ts"],
"exclude": ["./node_modules"],
"references": [
{
"path": "../../packages/graphql"
},
{
"path": "../../packages/backend"
},
{
"path": "../../packages/models"
}
]
}
1 change: 1 addition & 0 deletions packages/backend/src/bitcoin/inscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ export async function createInscriptionTransaction({
totalFee,
status: "funding-wait",
writableInscriptions: inscriptionsToWrite,
tip,
};
}
14 changes: 3 additions & 11 deletions packages/backend/src/dao/funding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@ import {
} from "@0xflick/ordinals-models";

export interface IFundingDao<
ItemInputType extends Record<string, any> = {},
ItemMeta extends Record<string, any> = {},
ItemReturnType = any,
CollectionInputType extends Record<string, any> = {},
CollectionMeta extends Record<string, any> = {},
CollectionReturnType = any
CollectionMeta extends Record<string, any> = {}
> {
createFunding(
item: IAddressInscriptionModel<ItemInputType>
): Promise<ItemReturnType>;
createFunding(item: IAddressInscriptionModel<ItemMeta>): Promise<void>;
getFunding(id: string): Promise<IAddressInscriptionModel<ItemMeta>>;
deleteFunding(id: string): Promise<void>;
createCollection(
item: TCollectionModel<CollectionInputType>
): Promise<CollectionReturnType>;
createCollection(item: TCollectionModel<CollectionMeta>): Promise<void>;
getCollection(id: ID_Collection): Promise<TCollectionModel<CollectionMeta>>;
deleteCollection(id: ID_Collection): Promise<void>;
incrementCollectionTotalCount(id: ID_Collection): Promise<number>;
Expand Down
43 changes: 4 additions & 39 deletions packages/backend/src/dynamodb/factory.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,13 @@
import type { IFundingDao } from "../dao/funding.js";
import { tableNames } from "../utils/config.js";
import { createDb } from "./create.js";
import {
FundingDao,
TFundingCollectionReturner,
TFundingItemReturner,
} from "./funding.js";
import { FundingDao } from "./funding.js";

export function createDynamoDbFundingDao<
ItemInputType extends Record<string, any> = {},
ItemMeta extends Record<string, any> = {},
ItemReturnType = any,
CollectionInputType extends Record<string, any> = {},
CollectionMeta extends Record<string, any> = {},
CollectionReturnType = any
>({
itemFundingUpdater,
collectionFundingUpdater,
}: {
itemFundingUpdater: TFundingItemReturner<ItemInputType, ItemReturnType>;
collectionFundingUpdater: TFundingCollectionReturner<
CollectionInputType,
CollectionReturnType
>;
}): IFundingDao<
ItemInputType,
ItemMeta,
ItemReturnType,
CollectionInputType,
CollectionMeta,
CollectionReturnType
> {
CollectionMeta extends Record<string, any> = {}
>(): IFundingDao<ItemMeta, CollectionMeta> {
const allTableNames = tableNames.get();
FundingDao.TABLE_NAME = allTableNames.funding ?? FundingDao.TABLE_NAME;
return new FundingDao<
ItemInputType,
ItemMeta,
ItemReturnType,
CollectionInputType,
CollectionMeta,
CollectionReturnType
>({
client: createDb(),
itemFundingUpdater,
collectionFundingUpdater,
});
return new FundingDao<ItemMeta, CollectionMeta>(createDb());
}
71 changes: 12 additions & 59 deletions packages/backend/src/dynamodb/funding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,67 +42,27 @@ export type TFundingCollectionReturner<
> = (item: TCollectionModel<ItemInputType>) => Promise<ItemReturnType>;

export class FundingDao<
ItemInputType extends Record<string, any> = {},
ItemMeta extends Record<string, any> = {},
ItemReturnType = any,
CollectionInputType extends Record<string, any> = {},
CollectionMeta extends Record<string, any> = {},
CollectionReturnType = any
> implements
IFundingDao<
ItemInputType,
ItemMeta,
ItemReturnType,
CollectionInputType,
CollectionMeta,
CollectionReturnType
>
CollectionMeta extends Record<string, any> = {}
> implements IFundingDao<ItemMeta, CollectionMeta>
{
public static TABLE_NAME = "Funding";

private client: DynamoDBClient;
private itemFundingUpdater: TFundingItemReturner<
ItemInputType,
ItemReturnType
>;
private collectionFundingUpdater: TFundingCollectionReturner<
CollectionInputType,
CollectionReturnType
>;

constructor({
client,
itemFundingUpdater,
collectionFundingUpdater,
}: {
client: DynamoDBClient;
itemFundingUpdater: TFundingItemReturner<ItemInputType, ItemReturnType>;
collectionFundingUpdater: TFundingCollectionReturner<
CollectionInputType,
CollectionReturnType
>;
}) {
constructor(client: DynamoDBClient) {
this.client = client;
this.itemFundingUpdater = itemFundingUpdater;
this.collectionFundingUpdater = collectionFundingUpdater;
}

public async createFunding(item: IAddressInscriptionModel<ItemInputType>) {
public async createFunding(item: IAddressInscriptionModel<ItemMeta>) {
const db = this.toFundingDb(item);
const response = await this.client.send(
await this.client.send(
new PutCommand({
TableName: FundingDao.TABLE_NAME,
Item: db,
ReturnValues: "NONE",
})
);
if (response.Attributes === undefined) {
throw new Error("Failed to create funding");
}
return this.itemFundingUpdater(
this.fromFundingDb<ItemInputType>(
response.Attributes as TFundingDb<ItemInputType>
)
);
}

public async getFunding(id: string) {
Expand All @@ -128,22 +88,15 @@ export class FundingDao<
);
}

public async createCollection(item: TCollectionModel<CollectionInputType>) {
public async createCollection(item: TCollectionModel<CollectionMeta>) {
const db = this.toCollectionDb(item);
const response = await this.client.send(
await this.client.send(
new PutCommand({
TableName: FundingDao.TABLE_NAME,
Item: db,
ReturnValues: "NONE",
})
);
if (response.Attributes === undefined) {
throw new Error("Failed to create funding");
}
return this.collectionFundingUpdater(
this.fromCollectionDb<CollectionInputType>(
response.Attributes as TFundingCollectionDb<CollectionInputType>
)
);
}

public async getCollection(id: ID_Collection) {
Expand Down Expand Up @@ -226,14 +179,14 @@ export class FundingDao<
...acc,
[`:${key}`]: meta[key],
}),
{}
{} as Record<string, any>
);
const expressionAttributeNames = Object.keys(meta).reduce(
(acc, key) => ({
...acc,
[`#${key}`]: key,
}),
{}
{} as Record<string, string>
);
if (incrementCollectionTotalCount) {
updateExpression += " ADD #totalCount :one";
Expand Down Expand Up @@ -326,7 +279,7 @@ export class FundingDao<
contentIds,
id: toAddressInscriptionId(id),
network: toBitcoinNetworkName(network),
collectionId: toCollectionId(collectionId),
...(collectionId ? { collectionId: toCollectionId(collectionId) } : {}),
meta: meta as T,
};
}
Expand Down
Loading

0 comments on commit 1d4ff33

Please sign in to comment.