Skip to content

Commit 665c418

Browse files
committed
prepare for persist query tokens on the app
1 parent d9bcd68 commit 665c418

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

scripts/persistQuery.js

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ async function persistQuery(queryText) {
147147
},
148148
});
149149

150+
const appId =
151+
process.env.NEXT_PUBLIC_ONEGRAPH_APP_ID ||
152+
// Backwards compat with older apps that started with razzle
153+
process.env.RAZZLE_ONEGRAPH_APP_ID;
154+
150155
const variables = {
151156
query: print(transformedAst),
152157
// This is your app's app id, edit `/.env` to change it
153-
appId:
154-
process.env.NEXT_PUBLIC_ONEGRAPH_APP_ID ||
155-
// Backwards compat with older apps that started with razzle
156-
process.env.RAZZLE_ONEGRAPH_APP_ID,
158+
appId,
157159
accessToken: accessToken || null,
158160
freeVariables: [...freeVariables],
159161
fixedVariables: fixedVariables,
@@ -168,43 +170,52 @@ async function persistQuery(queryText) {
168170
query: PERSIST_QUERY_MUTATION,
169171
variables,
170172
});
171-
return new Promise((resolve, reject) => {
172-
let data = '';
173-
const req = https.request(
174-
{
175-
hostname: 'serve.onegraph.com',
176-
port: 443,
177-
// This is the app id for the OneGraph dashbaord. If you followed the
178-
// instructions in the README to create the `OG_DASHBOARD_ACCESS_TOKEN`,
179-
// then this is the app id associated with the token that lets you persist
180-
// queries. Don't change this to your app id.
181-
path: '/graphql?app_id=0b066ba6-ed39-4db8-a497-ba0be34d5b2a',
182-
method: 'POST',
183-
headers: {
184-
'Content-Type': 'application/json',
185-
'Content-Length': body.length,
186-
Authorization: 'Bearer ' + process.env.OG_DASHBOARD_ACCESS_TOKEN,
173+
174+
function persist(appId) {
175+
return new Promise((resolve, reject) => {
176+
let data = '';
177+
const req = https.request(
178+
{
179+
hostname: 'serve.onegraph.com',
180+
port: 443,
181+
path: `/graphql?app_id=${appId}`,
182+
method: 'POST',
183+
headers: {
184+
'Content-Type': 'application/json',
185+
'Content-Length': body.length,
186+
Authorization: 'Bearer ' + process.env.OG_DASHBOARD_ACCESS_TOKEN,
187+
},
187188
},
188-
},
189-
res => {
190-
res.on('data', chunk => {
191-
data += chunk;
192-
});
193-
res.on('end', () => {
194-
const resp = JSON.parse(data);
195-
if (resp.errors) {
196-
throw new Error(
197-
'Error persisting query, errors=' + JSON.stringify(resp.errors),
198-
);
199-
} else {
200-
resolve(resp.data.oneGraph.createPersistedQuery.persistedQuery.id);
201-
}
202-
});
203-
},
204-
);
205-
req.write(body);
206-
req.end();
207-
});
189+
(res) => {
190+
res.on('data', (chunk) => {
191+
data += chunk;
192+
});
193+
res.on('end', () => {
194+
const resp = JSON.parse(data);
195+
if (resp.errors) {
196+
reject(
197+
new Error(
198+
'Error persisting query, errors=' +
199+
JSON.stringify(resp.errors),
200+
),
201+
);
202+
} else {
203+
resolve(
204+
resp.data.oneGraph.createPersistedQuery.persistedQuery.id,
205+
);
206+
}
207+
});
208+
},
209+
);
210+
req.write(body);
211+
req.end();
212+
});
213+
}
214+
return persist(appId).catch(() =>
215+
// This is the app id for the OneGraph dashboard. Some older persist query tokens will require
216+
// you to use this id. If persisting with the oneblog app id fails, then try with the dashboard id.
217+
persist('0b066ba6-ed39-4db8-a497-ba0be34d5b2a'),
218+
);
208219
}
209220

210221
exports.default = persistQuery;

0 commit comments

Comments
 (0)