assets/explorer/src/index.js   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 41
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 30
c 0
b 0
f 0
dl 0
loc 41
rs 10
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 0
1
import React from 'react';
2
import ReactDOM from 'react-dom';
3
import Drupal from 'drupal';
4
import jQuery from 'jquery';
5
import { buildClientSchema } from 'graphql';
6
import Explorer from './Explorer';
7
8
/**
9
 * Behavior for rendering the GraphiQL interface.
10
 */
11
Drupal.behaviors.graphQLRenderExplorer = {
12
  attach: (context, settings) => {
13
    const container = jQuery(once('graphql-explorer', '#graphql-explorer', context))[0] || undefined;
14
15
    if (typeof container === 'undefined') {
16
      return;
17
    }
18
19
    // Build a schema from the passed introspection data.
20
    const graphQLSchema = buildClientSchema(settings.graphqlIntrospectionData.data);
21
22
    // Defines a GraphQL fetcher using the fetch API.
23
    const graphQLFetcher = graphQLParams => fetch(settings.graphqlRequestUrl, {
24
      method: 'post',
25
      credentials: 'same-origin',
26
      body: JSON.stringify(graphQLParams),
27
      headers: {
28
        'Content-Type': 'application/json',
29
      },
30
    }).then(response => response.json());
31
32
    // Render <Explorer /> into the container.
33
    ReactDOM.render(React.createElement(Explorer, {
34
      fetcher: graphQLFetcher,
35
      schema: graphQLSchema,
36
      query: settings.graphqlQuery || undefined,
37
      variables: settings.graphqlVariables || undefined,
38
    }), container);
39
  },
40
};
41