Skip to content

Commit 3058ea0

Browse files
author
denisp22
committed
Replace “variables” and “query” props with “operation”.
1 parent b5fb194 commit 3058ea0

File tree

4 files changed

+82
-83
lines changed

4 files changed

+82
-83
lines changed

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Next
44

5+
### Major
6+
7+
- The `Query` (and the internal `GraphQLQuery`) component take an `operation` prop instead of separate `variables` and `query` props. This makes the implementation a little more elegant, is more consistent with the `GraphQL.query` API and allows sending custom GraphQL operation fields.
8+
59
### Minor
610

711
- Improved `Provider` and `Consumer` component display names in React dev tools:

readme.md

+20-17
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ A React component to manage a GraphQL query or mutation.
297297
| Parameter | Type | Description |
298298
| :--------------------------- | :-------------------------------------------------- | :----------------------------------------------------------------------------------------- |
299299
| `props` | [Object](https://mdn.io/object) | Component props. |
300-
| `props.variables` | [Object](https://mdn.io/object)? | GraphQL query variables. |
301-
| `props.query` | [string](https://mdn.io/string) | GraphQL query. |
300+
| `props.operation` | [GraphQLOperation](#type-graphqloperation) | GraphQL operation. |
302301
| `props.fetchOptionsOverride` | [FetchOptionsOverride](#type-fetchoptionsoverride)? | Overrides default GraphQL request [fetch options](#type-fetchoptions). |
303302
| `props.loadOnMount` | [boolean](https://mdn.io/boolean)? = `false` | Should the query load when the component mounts. |
304303
| `props.loadOnReset` | [boolean](https://mdn.io/boolean)? = `false` | Should the query load when the [GraphQL cache](#graphql-instance-property-cache) is reset. |
@@ -319,16 +318,18 @@ _A query to display a user profile._
319318
> loadOnMount
320319
> loadOnReset
321320
> fetchOptionsOverride={options => {
322-
> options.url = 'https://api.example.com/graphql'
321+
> options.url = 'https://api.example.com/graphql'
323322
> }}
324-
> variables={{ userId }}
325-
> query={`
326-
> query user($userId: ID!) {
327-
> user(userId: $userId) {
328-
> name
323+
> operation={
324+
> variables: { userId },
325+
> query: `
326+
> query user($userId: ID!) {
327+
> user(userId: $userId) {
328+
> name
329+
> }
329330
> }
330-
> }
331-
> `}
331+
> `
332+
> }
332333
> >
333334
> {({
334335
> load,
@@ -363,14 +364,16 @@ _A mutation to clap an article._
363364
> fetchOptionsOverride={options => {
364365
> options.url = 'https://api.example.com/graphql'
365366
> }}
366-
> variables={{ articleId }}
367-
> query={`
368-
> mutation clapArticle($articleId: ID!) {
369-
> clapArticle(articleId: $articleId) {
370-
> clapCount
367+
> operation={
368+
> variables: { articleId },
369+
> query: `
370+
> mutation clapArticle($articleId: ID!) {
371+
> clapArticle(articleId: $articleId) {
372+
> clapCount
373+
> }
371374
> }
372-
> }
373-
> `}
375+
> `
376+
> }
374377
> >
375378
> {({
376379
> load,

src/components.mjs

+28-38
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ export const {
5858
* @name GraphQLQuery
5959
* @param {Object} props Component props.
6060
* @param {GraphQL} props.graphql [`GraphQL`]{@link GraphQL} instance.
61-
* @param {Object} [props.variables] GraphQL query variables.
62-
* @param {string} props.query GraphQL query.
61+
* @param {GraphQLOperation} props.operation GraphQL operation.
6362
* @param {FetchOptionsOverride} [props.fetchOptionsOverride] Overrides default fetch options for the GraphQL request.
6463
* @param {boolean} [props.loadOnMount=false] Should the query load when the component mounts.
6564
* @param {boolean} [props.loadOnReset=false] Should the query load when its [GraphQL cache]{@link GraphQL#cache} entry is reset.
@@ -71,8 +70,7 @@ class GraphQLQuery extends React.Component {
7170
static propTypes = {
7271
graphql: propTypes.instanceOf(GraphQL).isRequired,
7372
fetchOptionsOverride: propTypes.func,
74-
variables: propTypes.object,
75-
query: propTypes.string.isRequired,
73+
operation: propTypes.object.isRequired,
7674
loadOnMount: propTypes.bool,
7775
loadOnReset: propTypes.bool,
7876
resetOnLoad: propTypes.bool,
@@ -87,7 +85,7 @@ class GraphQLQuery extends React.Component {
8785

8886
if (props.loadOnMount) {
8987
const fetchOptions = props.graphql.constructor.fetchOptions(
90-
this.operation()
88+
props.operation
9189
)
9290

9391
if (props.fetchOptionsOverride) props.fetchOptionsOverride(fetchOptions)
@@ -124,18 +122,6 @@ class GraphQLQuery extends React.Component {
124122
else this.setState({ requestCache })
125123
}
126124

127-
/**
128-
* Derives the GraphQL operation.
129-
* @kind function
130-
* @name GraphQLQuery#operation
131-
* @returns {Operation} GraphQL operation object.
132-
* @ignore
133-
*/
134-
operation = () => ({
135-
variables: this.props.variables,
136-
query: this.props.query
137-
})
138-
139125
/**
140126
* Loads the query, updating cache.
141127
* @kind function
@@ -146,7 +132,7 @@ class GraphQLQuery extends React.Component {
146132
load = () => {
147133
const stateUpdate = { loading: true }
148134
const { fetchOptionsHash, cache, request } = this.props.graphql.query({
149-
operation: this.operation(),
135+
operation: this.props.operation,
150136
fetchOptionsOverride: this.props.fetchOptionsOverride,
151137
resetOnLoad: this.props.resetOnLoad
152138
})
@@ -199,16 +185,18 @@ class GraphQLQuery extends React.Component {
199185
* Invoked after the React component updates.
200186
* @kind function
201187
* @name GraphQLQuery#componentDidUpdate
188+
* @param {Object} props New props.
189+
* @param {GraphQLOperation} props.operation GraphQL operation.
202190
* @ignore
203191
*/
204-
componentDidUpdate({ query, variables }) {
192+
componentDidUpdate({ operation }) {
205193
if (
206194
// Load on cache reset enabled and…
207195
this.props.loadOnReset &&
208196
// …a load has happened before and…
209197
this.state.fetchOptionsHash &&
210198
// …props that may affect the cache have changed.
211-
(query !== this.props.query || !equal(variables, this.props.variables))
199+
!equal(operation, this.props.operation)
212200
)
213201
this.load()
214202
}
@@ -248,8 +236,7 @@ class GraphQLQuery extends React.Component {
248236
* @kind function
249237
* @name Query
250238
* @param {Object} props Component props.
251-
* @param {Object} [props.variables] GraphQL query variables.
252-
* @param {string} props.query GraphQL query.
239+
* @param {GraphQLOperation} props.operation GraphQL operation.
253240
* @param {FetchOptionsOverride} [props.fetchOptionsOverride] Overrides default GraphQL request [fetch options]{@link FetchOptions}.
254241
* @param {boolean} [props.loadOnMount=false] Should the query load when the component mounts.
255242
* @param {boolean} [props.loadOnReset=false] Should the query load when the [GraphQL cache]{@link GraphQL#cache} is reset.
@@ -267,14 +254,16 @@ class GraphQLQuery extends React.Component {
267254
* fetchOptionsOverride={options => {
268255
* options.url = 'https://api.example.com/graphql'
269256
* }}
270-
* variables={{ userId }}
271-
* query={`
272-
* query user($userId: ID!) {
273-
* user(userId: $userId) {
274-
* name
257+
* operation={
258+
* variables: { userId },
259+
* query: `
260+
* query user($userId: ID!) {
261+
* user(userId: $userId) {
262+
* name
263+
* }
275264
* }
276-
* }
277-
* `}
265+
* `
266+
* }
278267
* >
279268
* {({
280269
* load,
@@ -307,14 +296,16 @@ class GraphQLQuery extends React.Component {
307296
* fetchOptionsOverride={options => {
308297
* options.url = 'https://api.example.com/graphql'
309298
* }}
310-
* variables={{ articleId }}
311-
* query={`
312-
* mutation clapArticle($articleId: ID!) {
313-
* clapArticle(articleId: $articleId) {
314-
* clapCount
299+
* operation={
300+
* variables: { articleId },
301+
* query: `
302+
* mutation clapArticle($articleId: ID!) {
303+
* clapArticle(articleId: $articleId) {
304+
* clapCount
305+
* }
315306
* }
316-
* }
317-
* `}
307+
* `
308+
* }
318309
* >
319310
* {({
320311
* load,
@@ -347,8 +338,7 @@ export const Query = props => (
347338

348339
Query.propTypes = {
349340
fetchOptionsOverride: propTypes.func,
350-
variables: propTypes.object,
351-
query: propTypes.string.isRequired,
341+
operation: propTypes.object.isRequired,
352342
loadOnMount: propTypes.bool,
353343
loadOnReset: propTypes.bool,
354344
resetOnLoad: propTypes.bool,

0 commit comments

Comments
 (0)