Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tools] adding introspect tools to extract remote schema from existing GraphQL endpoints #1

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"name": "hydra",
"dependencies": {
"@graphql-tools/schema": "^10.0.0",
"graphql": "^16.8.1",
"graphql-yoga": "^5.0.0",
"itty-durable": "^2.1.0",
"jest": "^29.7.0",
Expand Down
86 changes: 86 additions & 0 deletions packages/core/src/utils/introspect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import fetch from "node-fetch";
import { getIntrospectionQuery, buildClientSchema, printSchema } from "graphql";
const fs = require("fs");

interface IntrospectionResponse {
data?: {
__schema: any; // This should be a more specific type representing the introspected schema
};
}

async function introspectRemoteSchema(endpointUrl: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be worth adding tests for this one

try {
// Send an introspection query to the GraphQL endpoint
const response = await fetch(endpointUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ query: getIntrospectionQuery() }),
});

if (!response.ok) {
throw new Error(
`Failed to fetch introspection query: ${response.status}`,
);
}

// Parse response as JSON
const introspectionResult: IntrospectionResponse = await response.json();

// Check for network-related errors
if (!introspectionResult || !introspectionResult.data) {
throw new Error("Invalid introspection result");
}

// Build a client schema from the introspection result
const clientSchema = buildClientSchema(introspectionResult.data);
const schemaString = printSchema(clientSchema);

// Create a multi-line string enclosed within backticks
return `${schemaString}`;
} catch (error) {
console.error(`Error: ${error.message}`);
return null; // Return null or handle the error appropriately
}
}

/**
*
* @param schemas - remote GraphQL schemas to introspect, typically from hydra.config.ts
* @param outputPath - path to write the introspected schemas to
*/
export const buildSchemaIntrospection = async (
schemas: { id: string; uri: string }[],
outputPath: string,
) => {
let schemaWithIntrospection = [];

for (const schema of schemas) {
const introspected = await introspectRemoteSchema(schema.uri);
const output = {
name: schema.id,
url: schema.uri,
introspected,
};
schemaWithIntrospection.push(output);
}

const exportStatements = schemaWithIntrospection.map(
(s) =>
`export const ${s.name} = {
name: "${s.name}",
url: "${s.url}",
introspected: ${JSON.stringify(s.introspected, null, 2)}
};`,
);

const fileContent = `${exportStatements.join("\n")}\n`;

try {
fs.writeFileSync(outputPath, fileContent);
console.log(`${outputPath} written`);
} catch (err) {
console.error(err);
}
};
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.