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.
apollo-magic-refetch
Advanced tools
Handling Apollo cache updates after creating and deleting objects remains a
poorly solved problem.
update
and refetchQueries
props on Mutation
s couple different areas of
your app in a way you probably don't want, and they don't scale well as you add
more queries over objects you may create/delete.
Truly solving the problem will probably require changes to the apollo client and cache code.
Until that happens, this is probably your best bet!
After you delete an object, you tell apollo-magic-refetch
what typename
and
id
was deleted, and it refetches all active queries that contain that object
anywhere within their current data!
Similarly, after you create an object, you tell it the typename
of the created
object and it refetches all active queries that contain an object of that type
in their selections. This is a bit less efficient than handling deletes, but
way easier than anything else at the time of writing.
Since only active queries can be refetched, data in the cache for inactive
queries will remain out-of-date. For that reason, I would recommend using the
cache-and-network
policy on all queries you're not planning to update
after
all pertinent mutations.
Interfaces and union types are not supported yet. This means if they are anywhere in your results, this library may fail to refetch when it should.
Also, lists of lists are not supported, if for whatever reason you are using lists of lists in your schema (I haven't even checked if this is possible).
npm install --save apollo-magic-refetch
In this example, the __typename
of the object being deleted is Device
and it
uses the standard id
field. If instead the field were called tag
, for
instance, you would pass 'tag'
after deviceId
in the call to refetch
.
You may pass an array or Set
of ids in place of a single deviceId
.
import * as React from 'react'
import gql from 'graphql-tag'
import refetch from 'apollo-magic-refetch'
import {Mutation, ApolloConsumer} from 'react-apollo'
const mutation = gql`
mutation destroyDevice($deviceId: Int!) {
destroyDevice(deviceId: $deviceId)
}
`
const DestroyDeviceButton = ({deviceId}) => (
<ApolloConsumer>
{client => (
<Mutation
mutation={mutation}
update={() => refetch(client, 'Device', deviceId)}
/>
{destroyDevice => (
<button onClick={destroyDevice({variables: {deviceId}})}
)}
</Mutation>
)}
</ApolloConsumer>
)
When you omit the id parameter, refetch
refetches all active queries that
contain the requested __typename
in their selections, regardless of what ids
are actually in their results. This can be used after creating an object.
In this example, the __typename
of the object being created is Device
.
import * as React from 'react'
import gql from 'graphql-tag'
import refetch from 'apollo-magic-refetch'
import {Mutation, ApolloConsumer} from 'react-apollo'
import CreateDeviceForm from './CreateDeviceForm'
const mutation = gql`
mutation createDevice($values: CreateDevice!) {
createDevice(values: $values) {
id
}
}
`
const CreateDeviceFormContainer = () => (
<ApolloConsumer>
{client => (
<Mutation
mutation={mutation}
update={() => refetch(client, 'Device')}
/>
{createDevice => (
<CreateDeviceForm
onSubmit={(values) => createDevice({variables: {values}})}
/>
)}
</Mutation>
)}
</ApolloConsumer>
)
refetch(client, typename, [ids], [idField])
client: ApolloClient
The ApolloClient
in which to scan active queries.
typename: string
The __typename
of the GraphQL type that was created or deleted.
ids: any (*optional*)
A single id, an array of ids, or a Set
of ids that were deleted. If given,
only active queries whose current result contains an object with the given
typename
and id
will be refetched.
idField: string (*optional, default*: 'id')
The name of the id field in the type that was deleted.
FAQs
magically refetches relevant queries after creates and deletes
The npm package apollo-magic-refetch receives a total of 268 weekly downloads. As such, apollo-magic-refetch popularity was classified as not popular.
We found that apollo-magic-refetch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.