Skip to content

Commit a7f2266

Browse files
authored
chore: add graphql internally on nextjs (#53)
1 parent 026dcba commit a7f2266

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1223
-6927
lines changed

.lintstagedrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
'**/*.(md|mdx|json|yml|yaml|html|css)': ['prettier --write'],
2+
'**/*.(md|mdx|json|html|css)': ['prettier --write'],
33
'*.{js,jsx,ts,tsx}': ['prettier --write', 'eslint --fix'],
44
'.{ts,tsx}': ['tsc --noEmit'],
55
};

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ storybook-static
1919
pnpm-lock.yaml
2020
.next
2121
graphql.schema.json
22+
heml

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@
6060
},
6161
"devDependencies": {
6262
"@fuels/tsup-config": "^0.0.11",
63+
"@next/eslint-plugin-next": "^13.5.3",
6364
"@swc/core": "1.3.89",
6465
"@swc/jest": "0.2.29",
6566
"@types/jest": "29.5.5",
6667
"@types/node": "20.7.0",
67-
"deepmerge-json": "1.5.0",
68-
"@next/eslint-plugin-next": "^13.5.3",
6968
"@typescript-eslint/eslint-plugin": "^6.7.3",
7069
"@typescript-eslint/parser": "^6.7.3",
7170
"eslint": "^8.50.0",
@@ -83,6 +82,7 @@
8382
"husky": "^8.0.3",
8483
"npm-run-all": "^4.1.5",
8584
"prettier": "^3.0.3",
85+
"ts-node": "10.9.1",
8686
"tsup": "7.2.0",
8787
"turbo": "^1.10.14",
8888
"typescript": "5.2.2",

packages/app/.env.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
GRAPHQL_API=http://beta-4.fuel.network/graphql
1+
FUEL_PROVIDER_URL=http://beta-4.fuel.network/graphql
2+
23

packages/app/.env.production

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FUEL_PROVIDER_URL=http://beta-4.fuel.network/graphql
2+

packages/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
public/storybook
22
public/ui
3+
src/graphql/generated

packages/app/next-env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3+
/// <reference types="next/navigation-types/compat/navigation" />
34

45
// NOTE: This file should not be edited
56
// see https://nextjs.org/docs/basic-features/typescript for more information.

packages/app/next.config.mjs

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22
const config = {
33
reactStrictMode: true,
44
swcMinify: true,
5+
transpilePackages: ['@fuel-explorer/graphql'],
56
experimental: {
67
externalDir: true,
7-
serverComponentsExternalPackages: ['bcryptjs'],
8+
serverComponentsExternalPackages: [
9+
'bcryptjs',
10+
'@graphql-tools/delegate',
11+
'@graphql-tools/load',
12+
'@graphql-tools/schema',
13+
'@graphql-tools/stitch',
14+
'@graphql-tools/url-loader',
15+
'@graphql-tools/utils',
16+
],
817
serverActions: true,
18+
esmExternals: true,
919
},
1020
/** We run eslint as a separate task in CI */
1121
eslint: {
@@ -19,7 +29,7 @@ const config = {
1929
? [
2030
{
2131
source: '/graphql',
22-
destination: process.env.GRAPHQL_API,
32+
destination: '/api/graphql',
2333
},
2434
]
2535
: [];
@@ -38,6 +48,15 @@ const config = {
3848
},
3949
];
4050
},
51+
webpack: (config) => {
52+
config.module.rules.push({
53+
test: /\.(graphql|gql)/,
54+
exclude: /node_modules/,
55+
loader: 'graphql-tag/loader',
56+
});
57+
58+
return config;
59+
},
4160
};
4261

4362
export default config;

packages/app/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
"dayjs": "1.11.10",
2828
"framer-motion": "10.16.4",
2929
"fuels": "0.59.0",
30+
"graphql": "16.8.1",
3031
"graphql-request": "6.1.0",
32+
"graphql-tag": "2.12.6",
33+
"graphql-yoga": "4.0.4",
3134
"next": "13.5.3",
3235
"react": "18.2.0",
3336
"react-dom": "18.2.0",
37+
"tai64": "1.0.0",
3438
"tailwind-merge": "1.14.0",
3539
"tailwind-variants": "0.1.14",
3640
"zod": "3.22.2",

packages/app/src/graphql.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module '*.graphql' {
2+
import type { DocumentNode } from 'graphql';
3+
const Schema: DocumentNode;
4+
export = Schema;
5+
}

packages/app/src/pages/api/graphql.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createSchema } from '@fuel-explorer/graphql';
2+
import { createYoga } from 'graphql-yoga';
3+
4+
export const config = {
5+
api: {
6+
// Disable body parsing (required for file uploads)
7+
bodyParser: false,
8+
},
9+
};
10+
11+
const schema = createSchema(process.env.FUEL_PROVIDER_URL!);
12+
13+
export default createYoga({
14+
schema,
15+
// Needed to be defined explicitly because our endpoint lives at a different path other than `/graphql`
16+
graphqlEndpoint: '/api/graphql',
17+
});
+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import { getSdk } from '@fuel-explorer/graphql';
22
import { GraphQLClient } from 'graphql-request';
3+
import { resolve } from 'url';
34

4-
const graphql = new GraphQLClient(process.env.GRAPHQL_API!);
5-
export const sdk = getSdk(graphql);
5+
const VERCEL_URL = process.env.VERCEL_URL || process.env.NEXT_PUBLIC_VERCEL_URL;
6+
const VERCEL_ENV =
7+
process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV || 'development';
8+
9+
const getBaseUrl = () => {
10+
if (VERCEL_ENV !== 'development') return `https://${VERCEL_URL}`;
11+
return 'http://localhost:3000';
12+
};
13+
14+
const API_URL = resolve(getBaseUrl(), '/api/graphql');
15+
const client = new GraphQLClient(API_URL);
16+
export const sdk = getSdk(client);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { mocks } from '@fuel-explorer/graphql';
2+
import { bn } from '@fuel-ts/math';
3+
import { dayjs } from '~/systems/Core/utils/dayjs';
4+
5+
const status = mocks.aSuccessStatus({
6+
__typename: 'SuccessStatus',
7+
block: mocks.aBlock({
8+
transactions: [],
9+
}),
10+
});
11+
12+
const date = dayjs().subtract(1, 'day');
13+
14+
export const TX_MOCK = mocks.aTransaction({
15+
id: '0x78d13f111bf301324f34f2a7eaffc546d39598d156af38e7c4ef9fe61ea2c46a',
16+
time: {
17+
__typename: 'ParsedTime',
18+
fromNow: date.fromNow(),
19+
full: dayjs().format('DD MMM YYYY - HH:mm:ss A'),
20+
},
21+
totalAccounts: 2,
22+
totalAssets: 3,
23+
totalOperations: 4,
24+
gasUsed: bn(1),
25+
status,
26+
});

packages/app/tsconfig.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
4-
"plugins": [{ "name": "next" }],
4+
"plugins": [
5+
{
6+
"name": "next"
7+
}
8+
],
59
"allowJs": true,
610
"noEmit": true,
711
"lib": ["dom"],
@@ -17,11 +21,13 @@
1721
"@react-aria/interactions": [
1822
"../../node_modules/@react-aria/interactions"
1923
]
20-
}
24+
},
25+
"strictNullChecks": true
2126
},
2227
"include": [
2328
"next-env.d.ts",
2429
".next/types/**/*.ts",
30+
"./src/generated",
2531
"src",
2632
".storybook",
2733
"*.config.*"

packages/graphql/.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-typescript"],
3+
"plugins": ["import-graphql"]
4+
}

packages/graphql/.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
FUEL_PROVIDER_URL=http://beta-4.fuel.network/graphql
1+
FUEL_PROVIDER_URL=http://beta-4.fuel.network/graphql

packages/graphql/codegen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const config: CodegenConfig = {
55
schema: './src/schemas/fullschema.graphql',
66
documents: ['./src/queries/**.graphql'],
77
generates: {
8-
'src/generated/graphql.ts': {
8+
'src/generated/types.ts': {
99
plugins: [
1010
'typescript',
1111
'typescript-operations',
@@ -21,7 +21,7 @@ const config: CodegenConfig = {
2121
plugins: ['typescript-mock-data'],
2222
config: {
2323
addTypename: true,
24-
typesFile: './graphql.ts',
24+
typesFile: './types.ts',
2525
typesNames: 'keep',
2626
scalars: {
2727
Tai64Timestamp: 'unix_time',

0 commit comments

Comments
 (0)