2.0.0 • Published 22 days ago

@graphiql/plugin-code-exporter v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
22 days ago

GraphiQL Code Exporter Plugin

This package provides a plugin that integrates the GraphiQL Code Exporter into the GraphiQL UI.

Install

Use your favorite package manager to install the package:

npm i -S @graphiql/plugin-code-exporter

The following packages are peer dependencies, so make sure you have them installed as well:

npm i -S react react-dom graphql

Usage

See GraphiQL Code Exporter README for all details on available props and how to create snippets.

import { codeExporterPlugin } 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-code-exporter/dist/style.css';

const fetcher = createGraphiQLFetcher({
  url: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
});

/*
Example code for snippets. See https://github.com/OneGraph/graphiql-code-exporter#snippets for details
*/

const removeQueryName = query =>
  query.replace(
    /^[^{(]+([{(])/,
    (_match, openingCurlyBracketsOrParenthesis) =>
      `query ${openingCurlyBracketsOrParenthesis}`,
  );

const getQuery = (arg, spaceCount) => {
  const { operationDataList } = arg;
  const { query } = operationDataList[0];
  const anonymousQuery = removeQueryName(query);
  return (
    ' '.repeat(spaceCount) +
    anonymousQuery.replaceAll('\n', '\n' + ' '.repeat(spaceCount))
  );
};

const exampleSnippetOne = {
  name: 'Example One',
  language: 'JavaScript',
  codeMirrorMode: 'jsx',
  options: [],
  generate: arg => `export const query = graphql\`
${getQuery(arg, 2)}
\`
`,
};

const exampleSnippetTwo = {
  name: 'Example Two',
  language: 'JavaScript',
  codeMirrorMode: 'jsx',
  options: [],
  generate: arg => `import { graphql } from 'graphql'

export const query = graphql\`
${getQuery(arg, 2)}
\`
`,
};

const snippets = [exampleSnippetOne, exampleSnippetTwo];

const exporter = codeExporterPlugin({
  snippets,
  codeMirrorTheme: 'graphiql',
});

function GraphiQLWithExplorer() {
  return (
    <GraphiQL fetcher={fetcher} defaultQuery={query} plugins={[exporter]} />
  );
}

CDN bundles

You can also use this plugin with unpkg, jsdelivr, and other CDNs.

See the example HTML file for this plugin

2.0.0

22 days ago

1.0.5

25 days ago

1.0.4

3 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

10 months ago

1.0.3

5 months ago

0.3.0

10 months ago

0.3.5

7 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.3.4

9 months ago

0.3.3

9 months ago

0.2.0

10 months ago

0.1.3-alpha.0

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago