Last active
January 22, 2018 00:33
-
-
Save brianlovin/21d14ec88db85fb215b0fbbe0d8d4c5c to your computer and use it in GitHub Desktop.
Type checking client-side GraphQL with Apollo
This file contains hidden or 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
// @flow | |
import * as React from 'react'; | |
import compose from 'recompose/compose'; | |
import getThreadById from './queries' | |
import type { GetThreadType } from './queries'; | |
type Props = { | |
data: { | |
thread: GetThreadType, | |
... | |
}, | |
... | |
}; | |
class Thread extends React.Component<Props> { | |
render() { | |
const { data: { thread }} = this.props | |
// field not found on GetThreadType | |
const shouldThrowFlowError = thread && thread.badField | |
// can't call map on this type | |
const shouldAlsoThrowFlowError = thread && thread.content.map(a => a.body) | |
if (thread) { | |
return ( | |
... | |
) | |
} | |
... | |
} | |
} | |
export default compose(getThreadById)(Thread); |
This file contains hidden or 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
// @flow | |
import gql from 'graphql-tag'; | |
export type ThreadInfoType = { | |
id: string, | |
messageCount: number, | |
createdAt: Date, | |
modifiedAt: ?Date, | |
lastActive: ?Date, | |
content: { | |
title: string, | |
body: string, | |
}, | |
}; | |
export default gql` | |
fragment threadInfo on Thread { | |
id: string, | |
messageCount: number, | |
createdAt: Date, | |
modifiedAt: ?Date, | |
lastActive: ?Date, | |
content: { | |
title: string, | |
body: string, | |
} | |
} | |
`; |
This file contains hidden or 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
// @flow | |
import { graphql } from 'react-apollo'; | |
import gql from 'graphql-tag'; | |
import threadInfoFragment from './fragment'; | |
import type { ThreadInfoType } from './fragment'; | |
export type GetThreadType = { | |
...$Exact<ThreadInfoType>, | |
}; | |
export const getThreadByIdQuery = gql` | |
query getThread($id: ID!) { | |
thread(id: $id) { | |
...threadInfo | |
} | |
} | |
${threadInfoFragment} | |
`; | |
const getThreadByIdOptions = { | |
options: ({ id }) => ({ | |
variables: { | |
id, | |
}, | |
}), | |
}; | |
export deafult graphql(getThreadByIdQuery, getThreadByIdOptions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment