Skip to content

Instantly share code, notes, and snippets.

@EricMcRay
Last active January 18, 2019 10:49
Show Gist options
  • Save EricMcRay/b4cde2672706175e0ea92ccfc5616a16 to your computer and use it in GitHub Desktop.
Save EricMcRay/b4cde2672706175e0ea92ccfc5616a16 to your computer and use it in GitHub Desktop.
Missing type in fragment matcher json

What I have done

I download schema.json from server with apollo-tooling cli and give it to apollo cache with fragment matcher. That time server did not have CommentNotification type, just LikeNotification type which implements Notification interface.

I have notification components in react-native for LikeNotification type and Notification interface. I put a switch case which fallbacks unknown notifications which implements Notification interface to default notifications component.

My issue with Apollo client

My react-native app which is in app store right now has old version of schema.json and It dont know CommentNotification implements Notification interface and apollo client returns a empty object just with typename.

My question

How can I solve this kind a situation and fallback to interfaces for not implemented types?

// This is what apollo returned as query result
{
"notifications": [{
"id": "1",
"text": "User liked your post",
"sendAt": "2019-01-01",
"post": {
"id": "2",
"name": "Apollo Client",
"__typename":"Post"
},
"__typename": "LikeNotification"
},{
"__typename": "CommentNotification"
}]
}
# This is the query which I send to server via apollo client
query GetNotifications {
notifications {
... on Notification {
id
text
sendAt
}
... on LikeNotification {
user {
id
name
}
article {
id
name
}
}
}
}
// Server returns this object as query result.
{
"notifications": [{
"id": "1",
"text": "User liked your post",
"sendAt": "2019-01-01",
"post": {
"id": "2",
"name": "Apollo Client",
"__typename":"Post"
},
"__typename": "LikeNotification"
},{
"id": "1",
"text": "User liked your post",
"sendAt": "2019-01-01",
"post": {
"id": "2",
"name": "Apollo Client",
"__typename":"Post"
},
"comment": "This is a comment for post",
"__typename": "CommentNotification"
}]
}
# This is server graphql schema (simplified)
interface Notification {
id: ID!
text: String!
sendAt: DATESCALAR!
}
type LikeNotification implements Notification {
id: ID!
text: String!
sendAt: DATESCALAR!
post: Post!
}
# This type is new and It is not in the schema.json generated from old schema which deployed with mobile app.
type CommentNotification implements Notification {
id: ID!
text: String!
sendAt: DATESCALAR!
post: Post!
comment: String!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment