Last active
November 24, 2019 23:58
-
-
Save aleccool213/3cec5fc7d337975543e0a546b5da2675 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gql from "graphql-tag"; | |
import { IFieldResolver } from "graphql-tools"; | |
import { ISSUE_PARTS } from "../issues"; | |
import { | |
IssueParts, | |
UpdateIssueNameVariables, | |
} from "../../graphql-types"; | |
import { ResolverContext } from "."; | |
export const UPDATE_ISSUE_NAME = gql` | |
mutation UpdateIssueName($input: UpdateIssueNameInput!) { | |
updateIssueName(input: $input) @client | |
} | |
`; | |
const ISSUE_FRAGMENT = gql` | |
${ISSUE_PARTS} | |
fragment IssueParts on Issue { | |
id @client | |
name @client | |
} | |
`; | |
/** | |
* Updates the name of a Github issue. | |
**/ | |
const updateIssuename: IFieldResolver< | |
void, | |
ResolverContext, | |
any | |
> = (_obj, args: UpdateIssueNameVariables, context) => { | |
const { input } = args; | |
const { cache, getCacheKey } = context; | |
// 1. Get the id of the object in the cache using the actual issue id | |
const id = getCacheKey({ | |
__typename: "Issue", | |
id: input.id, | |
}); | |
// 2. Get the data from the cache | |
const issue: IssueParts | null = cache.readFragment({ | |
fragment: ISSUE_FRAGMENT, | |
fragmentName: "IssueParts", | |
id, | |
}); | |
if (!issue) { | |
return null; | |
} | |
// 3. Update the data locally | |
const updatedIssue = { | |
...issue, | |
name: input.name, | |
}; | |
// 4. Write the data back to the cache | |
cache.writeFragment({ | |
fragment: ISSUE_FRAGMENT, | |
fragmentName: "IssueParts", | |
id, | |
data: updatedIssue, | |
}); | |
return null; | |
}; | |
export default updateIssuename; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment