Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@shopify/react-graphql
Advanced tools
Tools for creating type-safe and asynchronous GraphQL components for React.
@shopify/react-graphql
Tools for creating type-safe and asynchronous GraphQL components for React.
$ yarn add @shopify/react-graphql
This library builds on top of react-apollo to provide asynchronously-loaded query components and more strongly-typed queries when used with graphql-typescript-definitions
.
Query
react-apollo’s Query
component is great, but does not have any built-in understanding of the connection between a GraphQL operation (provided in the query
prop) and the data types of the resulting query. This library re-exports a Query
component with improved typings. It will automatically read from from the types embedded in the query by graphql-typescript-definitions
and use these as appropriate for the rest of the Query
component’s props.
import {Query} from '@shopify/react-graphql';
import myQuery from './graphql/MyQuery.graphql';
// Assuming the following GraphQL API:
//
// type Shop = {
// id: String!
// name: String!
// }
// type Query = {
// shop: Shop!
// }
//
// and the following query:
//
// query MyQuery {
// shop { id }
// }
// Type error because no variables are allowed
<Query query={query} variables={{}}>{() => null}</Query>
// Type error because name was not queried
<Query query={query}>
{({data}) => {
return data ? <div>{data.shop.name}</div> : null;
}}
</Query>
createAsyncQueryComponent()
Another problem with the Query
component is that it does not work well when trying to preload GraphQL data for another page that is in a different JavaScript bundle. Because the query
must be provided directly, there is no easy way to keep it from "leaking" into unrelated bundles.
The createAsyncQueryComponent
function is an equally strong-typed alternative to Query
that supports asynchronously-loading GraphQL queries. The resulting component also exposes useful Preload
, Prefetch
, and KeepFresh
components built from the query. Best of all, it uses @shopify/react-async
under the hood, so you get the same server rendering benefits described in that package.
This function takes an options object with a load
property that returns a promise for a GraphQL query:
import {createAsyncQueryComponent} from '@shopify/react-graphql';
const ProductDetailsQuery = createAsyncQueryComponent({
load: () => import('./graphql/ProductDetailsQuery.graphql'),
});
As with @shopify/react-async
, you can also pass a defer
prop that is a member of the DeferTiming
enum to force the GraphQL query to resolve later on in its lifecycle:
import {createAsyncQueryComponent, DeferTiming} from '@shopify/react-graphql';
const ProductDetailsQuery = createAsyncQueryComponent({
load: () => import('./graphql/ProductDetailsQuery.graphql'),
defer: DeferTiming.Idle,
});
This component can now be used just like a regular Query
component. It accepts all the same props, except that the query (and associated types) are already embedded in it, so those do not need to be provided.
// Assuming the following GraphQL API:
//
// type Shop = {
// id: String!
// name: String!
// }
// type Query = {
// shop: Shop!
// }
//
// and the following query:
//
// query MyQuery {
// shop { id }
// }
import {createAsyncQueryComponent} from '@shopify/react-graphql';
const MyQuery = createAsyncQueryComponent({
load: () => import('./graphql/MyQuery.graphql'),
});
// Will complain if you try to pass any variables, because they aren’t needed.
// Will also complain if you try to reference properties on `data` that are not
// available.
<MyQuery>
{({data}) => {
return data ? <div>{data.shop.id}</div> : null;
}}
</MyQuery>;
As with components created by @shopify/react-async
’s createAsyncComponent()
function, these queries also have static Preload
, Prefetch
, and KeepFresh
components. Preload
will simply load the JavaScript bundle associated with the query. Prefetch
will load the JavaScript bundle and load the data (so, if there are any mandatory variables for your query, they will be required when rendering Prefetch
). KeepFresh
will do the same as Prefetch
, but will also poll for the query (you can customize the interval with the pollInterval
prop).
const MyQuery = createAsyncQueryComponent({
load: () => import('./graphql/MyQuery.graphql'),
});
<MyQuery.Preload />
<MyQuery.Prefetch />
<MyQuery.KeepFresh pollInterval={20_000} />
All components created by this library also reserve an async
prop (that is, you can’t have any props on these components also named async
). This prop can be used to pass custom instructions to the underlying async loading component.
Currently, this prop is an object with a defer?: DeferTiming
property, which changes the default defer
behaviour of the component (by default, the Query
/ "root" component is not deferred, Preload
and KeepFresh
are deferred until idle, and Prefetch
is deferred until mount).
const MyQuery = createAsyncQueryComponent({
load: () => import('./graphql/MyQuery.graphql'),
});
<MyQuery async={{defer: DeferTiming.Mount}} />
// This will force all of these components not to be deferred at all
<MyQuery.Preload async={{defer: undefined}} />
<MyQuery.Prefetch async={{defer: undefined}} />
<MyQuery.KeepFresh pollInterval={20_000} async={{defer: undefined}} />
Using Apollo Hooks assume the usage of react-apollo
)
ApolloProvider
Before using the individual hooks, you will need to wrap your application with ApolloProvider
at root of your React component tree.
You can it instead of react-apollo
's ApolloProvider
.
import React from 'react';
import {render} from 'react-dom';
import ApolloClient from 'apollo-client';
import {ApolloProvider} from 'react-graphql';
const client = new ApolloClient();
function App() {
return (
<ApolloProvider client={client}>
<MyRootComponent />
</ApolloProvider>
);
}
render(<App />, document.getElementById('root'));
useMutation
This hook accepts two arguments: the mutation document, and optionally, a set of options to pass to the underlying mutation. It will return a function that will trigger the mutation when invoked.
Note the set of options can be pass directly into the hook, or pass in while triggering the mutation function. If options exist in both places, they will be shallowly merge together with per-mutate options being the priority.
import React from 'react';
import {Form, TextField, Button, Banner} from '@shopify/polaris';
import {useQuery} from '@shopify/react-graphql';
import createCustomerMutation from './graphql/CreateCustomerMutation.graphql';
function CustomerDetail() {
const [name, setName] = React.useState('');
const createCustomer = useMutation(createCustomerMutation, {
fetchPolicy: 'network-only',
});
async function handleFormSubmit() {
try {
await createCustomer({
variables: {name},
});
// do something when the mutation is successful
} catch (error) {
// do something when the mutation fails
}
}
return (
<Form onSubmit={handleCreateCustomer}>
<TextField label="Name" value={name} onChange={(value) => {
setName(value);
}}>
<Button submit>
Create Customer
</Button>
</Form>
);
}
FAQs
Tools for creating type-safe and asynchronous GraphQL components for React
The npm package @shopify/react-graphql receives a total of 13,451 weekly downloads. As such, @shopify/react-graphql popularity was classified as popular.
We found that @shopify/react-graphql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.