Skip to content

Commit 9a86db2

Browse files
committed
Deprecate passing function as 'introspection' property
1 parent e6df189 commit 9a86db2

File tree

3 files changed

+59
-60
lines changed

3 files changed

+59
-60
lines changed

README.md

+21-15
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module system it is exported as `GraphQLVoyager` global variable.
2929

3030
`Voyager` component accepts the following properties:
3131

32-
- `introspection` [`object` or function: `(query: string) => Promise`] - the server introspection response. If `function` is provided GraphQL Voyager will pass introspection query as a first function parameter. Function should return `Promise` which resolves to introspection response object.
32+
- `introspection` [`object`] - the server introspection response. If `function` is provided GraphQL Voyager will pass introspection query as a first function parameter. Function should return `Promise` which resolves to introspection response object.
3333
- `displayOptions` _(optional)_
3434
- `displayOptions.skipRelay` [`boolean`, default `true`] - skip relay-related entities
3535
- `displayOptions.skipDeprecated` [`boolean`, default `true`] - skip deprecated fields and entities that contain only deprecated fields.
@@ -79,14 +79,22 @@ You can get GraphQL Voyager bundle from the following places:
7979
<body>
8080
<div id="voyager">Loading...</div>
8181
<script>
82-
function introspectionProvider(introspectionQuery) {
83-
// ... do a call to server using introspectionQuery provided
84-
// or just return pre-fetched introspection
85-
}
82+
// do a call to server using voyagerIntrospectionQuery provided
83+
const query = GraphQLVoyager.voyagerIntrospectionQuery;
84+
const introspection = fetch('<server url>', {
85+
method: 'post',
86+
headers: {
87+
Accept: 'application/json',
88+
'Content-Type': 'application/json',
89+
},
90+
body: JSON.stringify({ query }),
91+
// ...
92+
}).then((response) => response.json());
93+
// or just return pre-fetched introspection
8694
8795
// Render <Voyager />
8896
GraphQLVoyager.init(document.getElementById('voyager'), {
89-
introspection: introspectionProvider,
97+
introspection: introspection,
9098
});
9199
</script>
92100
</body>
@@ -105,18 +113,16 @@ And then use it:
105113
```js
106114
import * as React from 'react';
107115
import * as ReactDOM from 'react-dom';
108-
import { Voyager } from 'graphql-voyager';
116+
import { Voyager, voyagerIntrospectionQuery } from 'graphql-voyager';
109117

110-
function introspectionProvider(query) {
111-
return fetch(window.location.origin + '/graphql', {
112-
method: 'post',
113-
headers: { 'Content-Type': 'application/json' },
114-
body: JSON.stringify({ query: query }),
115-
}).then((response) => response.json());
116-
}
118+
const introspection = fetch(window.location.origin + '/graphql', {
119+
method: 'post',
120+
headers: { 'Content-Type': 'application/json' },
121+
body: JSON.stringify({ query: voyagerIntrospectionQuery }),
122+
}).then((response) => response.json());
117123

118124
ReactDOM.render(
119-
<Voyager introspection={introspectionProvider} />,
125+
<Voyager introspection={introspection} />,
120126
document.getElementById('voyager'),
121127
);
122128
```

src/components/Voyager.tsx

+27-25
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ export default class Voyager extends Component<VoyagerProps> {
8383
const displayOptions = normalizeDisplayOptions(this.props.displayOptions);
8484

8585
if (typeof this.props.introspection !== 'function') {
86-
console.warn(
87-
'GraphQLVoyager: Passing function as "introspection" is deprecated.' +
88-
'To access introspection query, please use "voyagerIntrospectionQuery".',
89-
);
9086
this.updateIntrospection(this.props.introspection, displayOptions);
9187
return;
9288
}
9389

90+
console.warn(
91+
'GraphQLVoyager: Passing function as "introspection" is deprecated.' +
92+
'To access introspection query, please use "voyagerIntrospectionQuery".',
93+
);
94+
9495
const promise = this.props.introspection(getIntrospectionQuery());
9596
this.setState({
9697
introspectionData: null,
@@ -220,33 +221,34 @@ export default class Voyager extends Component<VoyagerProps> {
220221
displayOptions={displayOptions}
221222
selectedTypeID={selectedTypeID}
222223
selectedEdgeID={selectedEdgeID}
223-
onSelectNode={this.handleSelectNode}
224-
onSelectEdge={this.handleSelectEdge}
224+
onSelectNode={handleSelectNode}
225+
onSelectEdge={handleSelectEdge}
225226
ref={this.viewportRef}
226227
/>
227228
);
228-
}
229229

230-
handleDisplayOptionsChange = (delta) => {
231-
const displayOptions = { ...this.state.displayOptions, ...delta };
232-
this.updateIntrospection(this.state.introspectionData, displayOptions);
233-
};
230+
handleDisplayOptionsChange = (delta) => {
231+
const displayOptions = { ...this.state.displayOptions, ...delta };
232+
this.updateIntrospection(this.state.introspectionData, displayOptions);
233+
};
234234

235-
handleSelectNode = (selectedTypeID) => {
236-
if (selectedTypeID !== this.state.selectedTypeID) {
237-
this.setState({ selectedTypeID, selectedEdgeID: null });
238-
}
239-
};
235+
handleSelectNode = (selectedTypeID) => {
236+
if (selectedTypeID !== this.state.selectedTypeID) {
237+
this.setState({ selectedTypeID, selectedEdgeID: null });
238+
}
239+
};
240+
241+
handleSelectEdge = (selectedEdgeID) => {
242+
if (selectedEdgeID === this.state.selectedEdgeID) {
243+
// deselect if click again
244+
this.setState({ selectedEdgeID: null });
245+
} else {
246+
const selectedTypeID = extractTypeId(selectedEdgeID);
247+
this.setState({ selectedTypeID, selectedEdgeID });
248+
}
249+
};
250+
}
240251

241-
handleSelectEdge = (selectedEdgeID) => {
242-
if (selectedEdgeID === this.state.selectedEdgeID) {
243-
// deselect if click again
244-
this.setState({ selectedEdgeID: null });
245-
} else {
246-
const selectedTypeID = extractTypeId(selectedEdgeID);
247-
this.setState({ selectedTypeID, selectedEdgeID });
248-
}
249-
};
250252

251253
static PanelHeader = (props) => {
252254
return props.children || null;

src/middleware/render-voyager-page.ts

+11-20
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,19 @@ export default function renderVoyagerPage(options: MiddlewareOptions) {
4141
</main>
4242
<script>
4343
window.addEventListener('load', function(event) {
44-
function introspectionProvider(introspectionQuery) {
45-
return fetch('${endpointUrl}', {
46-
method: 'post',
47-
headers: Object.assign({}, {
48-
'Accept': 'application/json',
49-
'Content-Type': 'application/json',
50-
}, ${headersJS}),
51-
body: JSON.stringify({query: introspectionQuery }),
52-
credentials: 'include',
53-
}).then(function (response) {
54-
return response.text();
55-
}).then(function (responseBody) {
56-
try {
57-
return JSON.parse(responseBody);
58-
} catch (error) {
59-
return responseBody;
60-
}
61-
});
62-
}
44+
const query = GraphQLVoyager.voyagerIntrospectionQuery;
45+
const introspection = fetch('${endpointUrl}', {
46+
method: 'post',
47+
headers: Object.assign({}, {
48+
'Accept': 'application/json',
49+
'Content-Type': 'application/json',
50+
}, ${headersJS}),
51+
body: JSON.stringify({query}),
52+
credentials: 'include',
53+
}).then(response => response.json());
6354
6455
GraphQLVoyager.init(document.getElementById('voyager'), {
65-
introspection: introspectionProvider,
56+
introspection,
6657
displayOptions: ${JSON.stringify(displayOptions)},
6758
})
6859
})

0 commit comments

Comments
 (0)