Skip to content

Commit c1c9b91

Browse files
author
Nathan Booker
committed
add freeform gql widget just for laughs
1 parent 3d6d2b9 commit c1c9b91

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

core/lib/makeswift/components.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import './components/card/register';
44
import './components/card-carousel/register';
55
import './components/carousel/register';
66
import './components/customer-group-slot/register';
7+
import './components/graphql-query/register';
78
import './components/product-card/register';
89
import './components/product-detail/register';
910
import './components/products-carousel/register';
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
'use client';
2+
3+
import { useState, useEffect } from 'react';
4+
5+
interface Props {
6+
className?: string;
7+
storeHash?: string;
8+
token?: string;
9+
query?: string;
10+
variables?: string;
11+
}
12+
13+
14+
15+
export function MakeswiftGraphQLQuery({
16+
className,
17+
storeHash = '',
18+
token = '',
19+
query = '',
20+
variables = '{}'
21+
}: Props) {
22+
const [data, setData] = useState<any>(null);
23+
const [loading, setLoading] = useState(false);
24+
const [error, setError] = useState<string | null>(null);
25+
26+
useEffect(() => {
27+
if (!storeHash || !token || !query) {
28+
setData(null);
29+
setError(null);
30+
return;
31+
}
32+
33+
const fetchData = async () => {
34+
setLoading(true);
35+
setError(null);
36+
37+
try {
38+
// Parse variables JSON
39+
let parsedVariables = {};
40+
try {
41+
parsedVariables = JSON.parse(variables);
42+
} catch (e) {
43+
throw new Error('Invalid JSON in variables field');
44+
}
45+
46+
// Create custom fetch function with actual storeHash
47+
const customFetch = async (token: string, query: string, variables = {}) => {
48+
const apiUrl = `https://store-${storeHash}.mybigcommerce.com/graphql`;
49+
50+
const response = await fetch(apiUrl, {
51+
method: 'POST',
52+
headers: {
53+
'Content-Type': 'application/json',
54+
'Authorization': `Bearer ${token}`,
55+
},
56+
body: JSON.stringify({
57+
query: query,
58+
variables: variables,
59+
}),
60+
});
61+
62+
if (!response.ok) {
63+
const errorData = await response.json();
64+
throw new Error(`HTTP error! status: ${response.status}, message: ${JSON.stringify(errorData)}`);
65+
}
66+
67+
return response.json();
68+
};
69+
70+
const result = await customFetch(token, query, parsedVariables);
71+
setData(result);
72+
} catch (err) {
73+
setError(err instanceof Error ? err.message : 'An unknown error occurred');
74+
} finally {
75+
setLoading(false);
76+
}
77+
};
78+
79+
fetchData();
80+
}, [storeHash, token, query, variables]);
81+
82+
return (
83+
<div className={className}>
84+
<div style={{ padding: '16px', border: '1px solid #ddd', borderRadius: '4px' }}>
85+
<h3 style={{ margin: '0 0 16px 0', fontSize: '18px', fontWeight: 'bold' }}>
86+
GraphQL Query Result
87+
</h3>
88+
89+
{!storeHash || !token || !query ? (
90+
<div style={{ color: '#666', fontStyle: 'italic' }}>
91+
Please configure Store Hash, Token, and Query to see results.
92+
</div>
93+
) : loading ? (
94+
<div style={{ color: '#666' }}>Loading...</div>
95+
) : error ? (
96+
<div style={{ color: '#d32f2f', backgroundColor: '#ffebee', padding: '8px', borderRadius: '4px' }}>
97+
<strong>Error:</strong> {error}
98+
</div>
99+
) : data ? (
100+
<pre style={{
101+
backgroundColor: '#f5f5f5',
102+
padding: '12px',
103+
borderRadius: '4px',
104+
overflow: 'auto',
105+
fontSize: '12px',
106+
margin: 0
107+
}}>
108+
{JSON.stringify(data, null, 2)}
109+
</pre>
110+
) : (
111+
<div style={{ color: '#666', fontStyle: 'italic' }}>
112+
No data received.
113+
</div>
114+
)}
115+
</div>
116+
</div>
117+
);
118+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Style, TextInput } from '@makeswift/runtime/controls';
2+
3+
import { runtime } from '~/lib/makeswift/runtime';
4+
5+
import { MakeswiftGraphQLQuery } from './client';
6+
7+
runtime.registerComponent(MakeswiftGraphQLQuery, {
8+
type: 'graphql-query',
9+
label: 'GraphQL Query',
10+
props: {
11+
className: Style(),
12+
storeHash: TextInput({
13+
label: 'Store Hash',
14+
defaultValue: '',
15+
placeholder: 'Enter your BigCommerce store hash',
16+
}),
17+
token: TextInput({
18+
label: 'Storefront API Token',
19+
defaultValue: '',
20+
placeholder: 'Enter your storefront API token',
21+
}),
22+
query: TextInput({
23+
label: 'GraphQL Query',
24+
defaultValue: `query {
25+
site {
26+
products(first: 5) {
27+
edges {
28+
node {
29+
entityId
30+
name
31+
prices {
32+
price {
33+
value
34+
currencyCode
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}`,
42+
multiline: true,
43+
placeholder: 'Enter your GraphQL query',
44+
}),
45+
variables: TextInput({
46+
label: 'Variables (JSON)',
47+
defaultValue: '{}',
48+
multiline: true,
49+
placeholder: 'Enter variables as JSON object',
50+
}),
51+
},
52+
});

0 commit comments

Comments
 (0)