mirror of
https://github.com/HSLdevcom/digitransit-ui
synced 2025-09-23 16:32:54 +02:00
69 lines
2 KiB
JavaScript
69 lines
2 KiB
JavaScript
/* eslint-disable import/no-extraneous-dependencies, no-console */
|
|
const fs = require('fs');
|
|
const fetch = require('node-fetch');
|
|
const http = require('https');
|
|
const { getIntrospectionQuery } = require('graphql');
|
|
|
|
const introspectionQuery = getIntrospectionQuery();
|
|
const outputJsonFilename = 'schema.json';
|
|
const graphqlSchemaSource =
|
|
process.env.SCHEMA_SRC ||
|
|
'https://raw.githubusercontent.com/HSLdevcom/OpenTripPlanner/dev-2.x/src/main/resources/org/opentripplanner/apis/gtfs/schema.graphqls';
|
|
const outputGraphQLFilename = 'schema.graphql';
|
|
const outputGraphQLFileCopy = `../digitransit-search-util/packages/digitransit-search-util-query-utils/schema/${outputGraphQLFilename}`;
|
|
|
|
const copySchema = (src, dest) => {
|
|
fs.copyFile(src, dest, err => {
|
|
if (err) {
|
|
throw err;
|
|
}
|
|
console.log(`${src} was copied to ${dest}`);
|
|
});
|
|
};
|
|
|
|
fetch(
|
|
process.env.OTP_URL ||
|
|
'https://dev-api.digitransit.fi/routing/v2/routers/hsl/index/graphql',
|
|
{
|
|
method: 'post',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
query: introspectionQuery,
|
|
}),
|
|
},
|
|
)
|
|
.then(response => {
|
|
console.log(response.headers);
|
|
return response.json();
|
|
})
|
|
.then(json => {
|
|
fs.writeFile(outputJsonFilename, JSON.stringify(json, null, 4), err => {
|
|
if (err) {
|
|
console.log(err);
|
|
} else {
|
|
console.log(`JSON saved to ${outputJsonFilename}`);
|
|
}
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.log(err);
|
|
});
|
|
|
|
if (graphqlSchemaSource.includes('http')) {
|
|
const file = fs.createWriteStream(outputGraphQLFilename);
|
|
http.get(graphqlSchemaSource, response => {
|
|
response.pipe(file);
|
|
|
|
file.on('finish', () => {
|
|
file.close();
|
|
console.log(`GraphQL schema saved to ${outputGraphQLFilename}`);
|
|
copySchema(outputGraphQLFilename, outputGraphQLFileCopy);
|
|
});
|
|
});
|
|
} else {
|
|
copySchema(graphqlSchemaSource, outputGraphQLFilename);
|
|
copySchema(graphqlSchemaSource, outputGraphQLFileCopy);
|
|
}
|