-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
282 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["plugin:react/jsx-runtime"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
types | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @graphiql/plugin-code-exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# GraphiQL Code Exporter Plugin | ||
|
||
This package provides a plugin that integrated the [`GraphiQL Code Exporter`](https://github.com/OneGraph/graphiql-code-exporter) into the GraphiQL UI. | ||
|
||
## Install | ||
|
||
Use your favorite package manager to install the package: | ||
|
||
```sh | ||
npm i -S @graphiql/plugin-code-exporter | ||
``` | ||
|
||
The following packages are peer dependencies, so make sure you have them installed as well: | ||
|
||
```sh | ||
npm i -S react react-dom graphql | ||
``` | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useCodeExporterPlugin } from '@graphiql/plugin-code-exporter'; | ||
import { createGraphiQLFetcher } from '@graphiql/toolkit'; | ||
import { GraphiQL } from 'graphiql'; | ||
import { useState } from 'react'; | ||
|
||
import 'graphiql/graphiql.css'; | ||
import '@graphiql/plugin-explorer/dist/style.css'; | ||
|
||
const fetcher = createGraphiQLFetcher({ | ||
url: 'https://swapi-graphql.netlify.app/.netlify/functions/index', | ||
}); | ||
|
||
const snippets = [`your-custom-snippets`]; | ||
|
||
function GraphiQLWithExplorer() { | ||
const [query, setQuery] = useState(DEFAULT_QUERY); | ||
const exporterPlugin = useCodeExporterPlugin({ | ||
query, | ||
snippets, | ||
}); | ||
return ( | ||
<GraphiQL | ||
fetcher={fetcher} | ||
query={query} | ||
onEditQuery={setQuery} | ||
plugins={[exporterPlugin]} | ||
/> | ||
); | ||
} | ||
``` | ||
|
||
## CDN bundles | ||
|
||
TODO! | ||
|
||
You can also use add this plugin when using the [CDN bundle](../../examples/graphiql-cdn) to render GraphiQL. Check out the [example HTML file](examples/index.html) that shows how you can do this. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@graphiql/plugin-code-exporter", | ||
"version": "0.0.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/graphql/graphiql", | ||
"directory": "packages/graphiql-plugin-code-exporter" | ||
}, | ||
"author": "LekoArts", | ||
"main": "dist/graphiql-plugin-code-exporter.cjs.js", | ||
"module": "dist/graphiql-plugin-code-exporter.es.js", | ||
"types": "types/index.d.ts", | ||
"license": "MIT", | ||
"keywords": [ | ||
"react", | ||
"graphql", | ||
"graphiql", | ||
"plugin", | ||
"explorer" | ||
], | ||
"files": [ | ||
"dist", | ||
"src", | ||
"types" | ||
], | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc --emitDeclarationOnly && node resources/copy-types.mjs && vite build", | ||
"preview": "vite preview" | ||
}, | ||
"dependencies": { | ||
"@graphiql/react": "^0.13.0", | ||
"graphiql-code-exporter": "^3.0.3" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^15.5.0 || ^16.0.0", | ||
"react": "^16.8.0 || ^17.0.0 || ^18.0.0", | ||
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0" | ||
}, | ||
"devDependencies": { | ||
"@vitejs/plugin-react": "^1.3.0", | ||
"typescript": "^4.6.3", | ||
"vite": "^2.9.13" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/graphiql-plugin-code-exporter/resources/copy-types.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const base = path.resolve(path.dirname(__filename), '..'); | ||
|
||
fs.copyFileSync( | ||
path.resolve(base, 'src', 'graphiql-code-exporter.d.ts'), | ||
path.resolve(base, 'types', 'graphiql-code-exporter.d.ts'), | ||
); |
14 changes: 14 additions & 0 deletions
14
packages/graphiql-plugin-code-exporter/src/graphiql-code-exporter.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
declare module 'graphiql-code-exporter' { | ||
import { GraphQLLeafType, ValueNode } from 'graphql'; | ||
import { ComponentType } from 'react'; | ||
|
||
export type GraphiQLCodeExporterProps = { | ||
query: string; | ||
}; | ||
|
||
const GraphiQLCodeExporter: ComponentType<GraphiQLCodeExporterProps> & { | ||
defaultValue: (arg: GraphQLLeafType) => ValueNode; | ||
}; | ||
|
||
export default GraphiQLCodeExporter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* TODO */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { GraphiQLPlugin } from '@graphiql/react'; | ||
import { useMemo, useRef } from 'react'; | ||
import GraphiQLCodeExporter, { | ||
GraphiQLCodeExporterProps, | ||
} from 'graphiql-code-exporter'; | ||
|
||
import './graphiql-code-exporter.d.ts'; | ||
import './index.css'; | ||
|
||
function ExporterPlugin(props: GraphiQLCodeExporterProps) { | ||
return <GraphiQLCodeExporter {...props} />; | ||
} | ||
|
||
export function useExplorerPlugin(props: GraphiQLCodeExporterProps) { | ||
const propsRef = useRef(props); | ||
propsRef.current = props; | ||
return useMemo<GraphiQLPlugin>( | ||
() => ({ | ||
title: 'GraphiQL Code Exporter', | ||
icon: () => ( | ||
<svg height="1em" strokeWidth="1.5" viewBox="0 0 24 24" fill="none"> | ||
<path | ||
d="M18 6H20M22 6H20M20 6V4M20 6V8" | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
/> | ||
<path | ||
d="M21.4 20H2.6C2.26863 20 2 19.7314 2 19.4V11H21.4C21.7314 11 22 11.2686 22 11.6V19.4C22 19.7314 21.7314 20 21.4 20Z" | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
/> | ||
<path | ||
d="M2 11V4.6C2 4.26863 2.26863 4 2.6 4H8.77805C8.92127 4 9.05977 4.05124 9.16852 4.14445L12.3315 6.85555C12.4402 6.94876 12.5787 7 12.722 7H14" | ||
stroke="currentColor" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
/> | ||
</svg> | ||
), | ||
content: () => <ExporterPlugin {...propsRef.current} />, | ||
}), | ||
[], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// / <reference types="vite/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
"allowJs": false, | ||
"skipLibCheck": true, | ||
"esModuleInterop": false, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"declaration": true, | ||
"declarationDir": "types", | ||
"jsx": "react-jsx" | ||
}, | ||
"include": ["src"], | ||
"references": [{ "path": "./tsconfig.node.json" }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"composite": true, | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
"include": ["vite.config.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { defineConfig } from 'vite'; | ||
import react from '@vitejs/plugin-react'; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()], | ||
build: { | ||
lib: { | ||
entry: 'src/index.tsx', | ||
fileName: 'graphiql-plugin-code-exporter', | ||
name: 'GraphiQLPluginCodeExporter', | ||
formats: ['cjs', 'es', 'umd'], | ||
}, | ||
rollupOptions: { | ||
external: ['@graphiql/react', 'graphql', 'react', 'react-dom'], | ||
output: { | ||
chunkFileNames: '[name].[format].js', | ||
globals: { | ||
'@graphiql/react': 'GraphiQL.React', | ||
graphql: 'GraphiQL.GraphQL', | ||
react: 'React', | ||
'react-dom': 'ReactDOM', | ||
}, | ||
}, | ||
}, | ||
commonjsOptions: { | ||
esmExternals: true, | ||
requireReturnsDefault: 'auto', | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters