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
|
|
|
|