diff --git a/src/components/GraphQLVoyagerComponent/GraphQLVoyagerComponent.tsx b/src/components/GraphQLVoyagerComponent/GraphQLVoyagerComponent.tsx index 316f114..513161f 100644 --- a/src/components/GraphQLVoyagerComponent/GraphQLVoyagerComponent.tsx +++ b/src/components/GraphQLVoyagerComponent/GraphQLVoyagerComponent.tsx @@ -1,10 +1,12 @@ import React from 'react'; -import { Header, Page, Content, Progress } from '@backstage/core-components'; -import { Config } from '@backstage/config'; -import { configApiRef, useApi } from '@backstage/core-plugin-api'; +import { Content, Progress } from '@backstage/core-components'; import Alert from '@material-ui/lab/Alert'; import { useAsync } from 'react-use'; -import { getIntrospectionQuery } from 'graphql/utilities'; +import { + getIntrospectionQuery, + introspectionFromSchema, + buildSchema, +} from "graphql/utilities"; import { Voyager } from 'graphql-voyager'; import fetch from 'isomorphic-fetch'; import 'graphql-voyager/dist/voyager.css'; @@ -28,41 +30,43 @@ const displayOptions = { hideRoot: true }; -export const GraphQLVoyagerComponent = () => { - const config: Config = useApi(configApiRef); - const graphqlEndpoint = config.get<{ baseUrl: string }>('graphql').baseUrl; +export const GraphQLVoyagerComponent = ({ + endpoint, + sdl, +}: { + endpoint?: string; + sdl?: string; +}) => { const { value: result, loading, - error - } = useAsync(async () => await introspectionProvider(graphqlEndpoint), []); + error, + } = useAsync(async () => { + if (sdl) { + const data = introspectionFromSchema(buildSchema(sdl)); + return { data }; + } + if (endpoint) { + return await introspectionProvider(endpoint); + } + throw new Error("Must specify endpoint or schema SDL"); + }, [endpoint, sdl]); if (loading) { return ( - -
- - - - + + + ); } else if (error) { - return {error.message}; + return {error.message}; } + return ( - -
- - + ); }; diff --git a/src/components/GraphQLVoyagerComponent/index.ts b/src/components/GraphQLVoyagerComponent/index.ts deleted file mode 100644 index 28c607f..0000000 --- a/src/components/GraphQLVoyagerComponent/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { GraphQLVoyagerComponent } from './GraphQLVoyagerComponent'; diff --git a/src/components/GraphQLVoyagerComponent/GraphQLVoyagerComponent.test.tsx b/src/components/GraphQLVoyagerPageComponent/GraphQLVoyagerPageComponent.test.tsx similarity index 64% rename from src/components/GraphQLVoyagerComponent/GraphQLVoyagerComponent.test.tsx rename to src/components/GraphQLVoyagerPageComponent/GraphQLVoyagerPageComponent.test.tsx index cf9ecab..457d21a 100644 --- a/src/components/GraphQLVoyagerComponent/GraphQLVoyagerComponent.test.tsx +++ b/src/components/GraphQLVoyagerPageComponent/GraphQLVoyagerPageComponent.test.tsx @@ -1,5 +1,4 @@ import React from 'react'; -import { GraphQLVoyagerComponent } from './GraphQLVoyagerComponent'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; import { configApiRef } from '@backstage/core-plugin-api'; @@ -9,8 +8,9 @@ import { renderInTestApp, TestApiProvider } from '@backstage/test-utils'; +import { GraphQLVoyagerPageComponent } from './GraphQLVoyagerPageComponent'; -describe('GraphQLVoyagerComponent', () => { +describe("GraphQLVoyagerPageComponent", () => { const server = setupServer(); // Enable sane handlers for network requests setupRequestMockHandlers(server); @@ -18,21 +18,21 @@ describe('GraphQLVoyagerComponent', () => { // setup mock response beforeEach(() => { server.use( - rest.get('/*', (_, res, ctx) => res(ctx.status(200), ctx.json({}))), + rest.get("/*", (_, res, ctx) => res(ctx.status(200), ctx.json({}))) ); }); - it('should render', async () => { + it("should render", async () => { const mockConfig = new MockConfigApi({ - graphql: { baseUrl: 'https://example.com' } + graphql: { baseUrl: "https://example.com" }, }); const rendered = await renderInTestApp( - + ); expect( - rendered.getByText('A visual representation of the schema.') + rendered.getByText("A visual representation of the schema.") ).toBeInTheDocument(); }); }); diff --git a/src/components/GraphQLVoyagerPageComponent/GraphQLVoyagerPageComponent.tsx b/src/components/GraphQLVoyagerPageComponent/GraphQLVoyagerPageComponent.tsx new file mode 100644 index 0000000..cec760d --- /dev/null +++ b/src/components/GraphQLVoyagerPageComponent/GraphQLVoyagerPageComponent.tsx @@ -0,0 +1,20 @@ +import React from "react"; +import { Header, Page } from "@backstage/core-components"; +import { Config } from "@backstage/config"; +import { configApiRef, useApi } from "@backstage/core-plugin-api"; +import { GraphQLVoyagerComponent } from "../GraphQLVoyagerComponent/GraphQLVoyagerComponent"; + +export const GraphQLVoyagerPageComponent = () => { + const config: Config = useApi(configApiRef); + const graphqlEndpoint = config.get<{ baseUrl: string }>("graphql").baseUrl; + + return ( + +
+ + + ); +}; diff --git a/src/index.ts b/src/index.ts index a9dcdeb..dfcbfa8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export { graphqlVoyagerPlugin, GraphqlVoyagerPage } from './plugin'; +export { GraphQLVoyagerComponent } from "./components/GraphQLVoyagerComponent/GraphQLVoyagerComponent"; \ No newline at end of file diff --git a/src/plugin.ts b/src/plugin.ts index 7f4da38..bafdc9c 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -14,10 +14,11 @@ export const graphqlVoyagerPlugin = createPlugin({ export const GraphqlVoyagerPage = graphqlVoyagerPlugin.provide( createRoutableExtension({ + name: "graphql-voyager", component: () => - import('./components/GraphQLVoyagerComponent').then( - (m) => m.GraphQLVoyagerComponent - ), - mountPoint: rootRouteRef + import( + "./components/GraphQLVoyagerPageComponent/GraphQLVoyagerPageComponent" + ).then((m) => m.GraphQLVoyagerPageComponent), + mountPoint: rootRouteRef, }) );