Skip to content

Commit

Permalink
completes full graphql query support
Browse files Browse the repository at this point in the history
# Why
* The mocking is not working correctly.
* Tests are failing.
* Typings are off.

# What
* Adds a new `mockApollo` Provider that is a complete stand-in
replacement for `ApolloProvider` and `client`. It more or less creates
a mock networkInterface for Apollo, then uses a schema generated from
an introspection query against the JSON schema generated from the
server in order to provide Apollo with an executable schema it can
auto-mock against types (or use custom mocks passed in). It also has
support for passing in initial state, else it defers to the state
derived from the reducers.
* Fixes bug with test execution - `apolli-client` has an optional
dependency specified for react-dom 16.x. Removing it from `yarn.lock`
and reinstalling the packages solved the problem. [Open
Issue](apollographql/react-apollo#826)
* Fixes mock queries for `viewer`.
* Adds better type support for Graphql. Part of this includes removing
the `withData` higher order component for the time being until we can
figure out how to deal with some of the more complex type errors it was
producing. A complete example of the way to wrap a component with a
query can be found in `routes/docs/examples/grapqhl/root.tsx` with a
corresponding test in the same location.
* Adds a `mockGraphql` function for returning a mocked query outside of
a test - like if you want to mock some data out without making changes
to the server.
* Schema generation and type generation from the schema / used queries
is now fully automated. It triggers with `yarn run start` (Dev) and
`yarn run build` (Prod). This prevents the client from ever getting out
of sync with the server and enforces strict type checking on every
query, subscription, or mutation.
  • Loading branch information
Gabe Weaver committed Jul 14, 2017
1 parent de5ea37 commit ba76f2b
Show file tree
Hide file tree
Showing 34 changed files with 18,950 additions and 194 deletions.
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@
"license": "MIT",
"dependencies": {
"@types/jest": "^20.0.2",
"@types/react": "^15.0.35",
"@types/react": "^15.0.38",
"@types/react-dom": "^15.5.1",
"@types/react-helmet": "^5.0.3",
"@types/react-hot-loader": "^3.0.3",
"@types/react-redux": "^4.4.45",
"@types/react-router-dom": "^4.0.5",
"@types/redux-form": "^6.6.6",
"@types/react-redux": "^4.4.46",
"@types/react-router-dom": "^4.0.7",
"@types/redux-form": "^6.6.7",
"@types/redux-thunk": "^2.1.0",
"@types/reselect": "^2.2.0",
"@types/shortid": "^0.0.29",
"@types/webpack": "^3.0.1",
"@types/webpack": "^3.0.4",
"@types/webpack-env": "^1.13.0",
"apollo-client": "^1.8.0",
"apollo-codegen": "^0.15.0",
Expand All @@ -103,7 +103,7 @@
"es6-promise": "^4.1.1",
"graphql-tag": "^2.4.2",
"gsap": "^1.20.2",
"hoist-non-react-statics": "^2.0.0",
"hoist-non-react-statics": "^2.1.1",
"localforage": "^1.5.0",
"moment": "^2.18.1",
"phoenixjs": "^1.0.5",
Expand All @@ -116,8 +116,8 @@
"react-redux": "^5.0.5",
"react-router-dom": "^4.1.1",
"react-transition-group": "^2.0.2",
"redux": "^3.7.1",
"redux-form": "^6.8.0",
"redux": "^3.7.2",
"redux-form": "^7.0.0",
"redux-next": "reactjs/redux#next",
"redux-persist": "^4.8.2",
"redux-persist-memory-storage": "^0.0.3",
Expand All @@ -134,6 +134,7 @@
"@kadira/storybook-addon-knobs": "^1.7.1",
"@kadira/storybook-addon-notes": "^1.0.1",
"@kadira/storybook-addon-options": "^1.0.2",
"apollo-test-utils": "^0.3.2",
"assets-webpack-plugin": "^3.5.1",
"awesome-typescript-loader": "^3.2.1",
"babel-cli": "^6.24.1",
Expand Down Expand Up @@ -162,20 +163,21 @@
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.28.4",
"enzyme": "^2.9.1",
"extract-text-webpack-plugin": "^2.1.2",
"extract-text-webpack-plugin": "^3.0.0",
"faker": "^4.1.0",
"file-loader": "^0.11.2",
"fs-extra": "^3.0.1",
"git-revision-webpack-plugin": "^2.5.1",
"graphql": "^0.10.3",
"graphql-api": "^0.1.7",
"graphql-tools": "^1.1.0",
"highlight.js": "^9.12.0",
"html-webpack-plugin": "^2.29.0",
"imports-loader": "^0.7.1",
"inline-manifest-webpack-plugin": "^3.0.1",
"ip": "^1.1.2",
"jest": "^20.0.4",
"jest-enzyme": "^3.4.0",
"jest-enzyme": "^3.5.3",
"json-loader": "^0.5.4",
"markdown-it": "^8.3.1",
"markdown-loader": "^2.0.1",
Expand All @@ -195,7 +197,7 @@
"typescript": "^2.4.1",
"typings-for-css-modules-loader": "^1.5.0",
"url-loader": "^0.5.9",
"webpack": "^3.1.0",
"webpack": "^3.2.0",
"webpack-dev-server": "^2.5.1",
"webpack-manifest-plugin": "^1.1.2",
"webpack-md5-hash": "^0.0.5",
Expand Down
6 changes: 3 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import * as React from 'react'
import { ApolloProvider } from 'react-apollo'
import { BrowserRouter } from 'react-router-dom'

import client from './lib/graphql'
import { StoreWithState } from './lib/types'
import client from './lib/graphql/client'
import { Store } from './lib/types'
import Routes from './routes'

interface RootProps {
store: StoreWithState
store: Store<{}>
}

const Root: React.SFC<RootProps> = ({ store }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Counter/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Counter: React.SFC<CounterProps> = ({
}) => (
<Box>
<H2>
Counter: <Span color='lightGray'>{counter}</Span>
Counter: <Span id='count' color='lightGray'>{counter}</Span>
</H2>
<Button
id='incrementCounter'
Expand Down
4 changes: 0 additions & 4 deletions src/hoc/withData/index.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/hoc/withData/withData.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/lib/graphql/graphql.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.gql' {
const value: any
export = value
}
11 changes: 3 additions & 8 deletions src/lib/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import client from './client'
import mutate, { Mutations, MutationTypes } from './mutations'
import query, { Queries, QueryTypes } from './queries'
import mutate, { Mutations } from './mutations'
import query, { Queries } from './queries'

export {
mutate,
Mutations,
MutationTypes,
query,
Queries,
QueryTypes
Queries
}

export default client
1 change: 1 addition & 0 deletions src/lib/graphql/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ addMockFunctionsToSchema({
schema: mockSchema
})

export { mocks }
export default mockSchema
6 changes: 4 additions & 2 deletions src/lib/graphql/mocks/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import getAllPagesMocks from './getAllPages'
import viewer from './viewer'

export default {
...getAllPagesMocks
Query: () => ({
...viewer
})
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import faker from 'faker'
import * as faker from 'faker'
import { MockList } from 'graphql-tools'
import S from 'string'

const mockPath = () => {
const mockPages = () => {
const id = faker.random.alphaNumeric(15)
const words = (c) => faker.random.words(c)
const slug = (c) => S(words(c)).slugify().s
Expand All @@ -16,13 +16,11 @@ const mockPath = () => {
}

export default {
GetAllPages: () => ({
viewer: () => ({
__typename: 'Viewer',
allPages: () => ({
__typename: 'PageConnection',
edges: () => new MockList([2, 12], mockPath)
})
viewer: () => ({
__typename: 'Viewer',
allPages: () => ({
__typename: 'PageConnection',
edges: () => new MockList([2, 12], mockPages)
})
})
}
2 changes: 1 addition & 1 deletion src/lib/graphql/mocks/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildClientSchema } from 'graphql'
import * as introspectionResult from '../schema.json'
import * as introspectionResult from '../scaphold.schema.json'

export const schema = buildClientSchema(introspectionResult.data)
6 changes: 1 addition & 5 deletions src/lib/graphql/mutations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import {

import * as createPage from './createPage.gql'

export type MutationTypes = CreatePageMutation

export interface Mutations {
createPage: CreatePageMutation
}
export type Mutations = CreatePageMutation // & NewMutationA & NewMutationB

export default {
createPage
Expand Down
10 changes: 3 additions & 7 deletions src/lib/graphql/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ import {
GetAllPagesQuery
} from '../schema'

import * as getAllPages from './getAllPages.gql'
import * as GetAllPages from './GetAllPages.gql'

export type QueryTypes = GetAllPagesQuery

export interface Queries {
getAllPages: GetAllPagesQuery
}
export type Queries = GetAllPagesQuery // & NewQueryA & NewQueryB

export default {
getAllPages
GetAllPages
}
Loading

0 comments on commit ba76f2b

Please sign in to comment.