Created
June 20, 2018 15:28
-
-
Save jbaxleyiii/c63ad789d8b3472474bdcec4b58dbf84 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
/* | |
* | |
* Remote linked data (which is local state) | |
*/ | |
const linked = gql` | |
query GetDogs { | |
dog(id: 1) { | |
name | |
isLiked @client # local only state, not computed | |
} | |
} | |
`; | |
const schema = gql` | |
extend type Query { | |
isOnline: Boolean | |
} | |
extend type Dog { | |
isLiked: Boolean | |
localName: String | |
} | |
extend type Mutation { | |
likeDog(dogId: ID!, isLiked: Boolean): Dog | |
} | |
`; | |
const resolvers = { | |
Query: { | |
isOnline: () => NetworkStatus.isOnline(), | |
}, | |
Dog: {}, | |
Mutation: { | |
likeDog: (_, { dogId, isLiked }, { cache }) => { | |
return cache.updateData({ | |
fragment: gql` | |
...updateDog on Dog { | |
isLiked | |
} | |
`, | |
id: dogId, | |
__typename: "Dog", | |
data: { isLiked }, | |
}); | |
}, | |
}, | |
}; | |
client.mutate({ client: true }); | |
client.mutations.likeDog({ id: 1 }); | |
/* | |
* | |
* Computed fields | |
* | |
*/ | |
const computed = gql` | |
query GetDog { | |
dog(id: 1) { | |
name | |
localName @client # where this is based on lang | |
} | |
} | |
`; | |
/* | |
* | |
* Local only state | |
*/ | |
const local = gql` | |
query isOnline { | |
isOnline @client # root field query | |
} | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment