Skip to content

Commit

Permalink
Merge pull request #1 from authdog/util-remote-schema-introspect
Browse files Browse the repository at this point in the history
[Tools] adding introspect tools to extract remote schema from existing GraphQL endpoints
  • Loading branch information
dbrrt authored Nov 18, 2023
2 parents 3e9996e + f2de450 commit eeb1044
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
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) {
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.

0 comments on commit eeb1044

Please sign in to comment.