Last active
January 8, 2019 22:03
-
-
Save estrada9166/3cc30cece4d159228439298af7b41408 to your computer and use it in GitHub Desktop.
How to use easygraphql-tester with relay
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
// @flow | |
/* global __DEV__ */ | |
import fs from'fs'; | |
import path from'path'; | |
import EasyGraphQLTester from 'easygraphql-tester'; | |
import { installRelayDevTools } from 'relay-devtools'; | |
import { Environment, Network, RecordSource, Store } from 'relay-runtime'; | |
import RelayNetworkLogger from 'relay-runtime/lib/RelayNetworkLogger'; | |
import cacheHandler from './cacheHandler'; | |
import { fixtures } from './fixture' | |
const schema = fs.readFileSync(path.join(__dirname, '..', '..', 'data', 'schema.graphql'), 'utf8'); | |
const tester = new EasyGraphQLTester(schema); | |
let networkQuery = cacheHandler | |
if (__DEV__) { | |
installRelayDevTools(); | |
networkQuery = RelayNetworkLogger.wrapFetch(cacheHandler) | |
} | |
// If it is a test set this variable or process.env.NODE_ENV === 'test' | |
if (__TEST__) { | |
networkQuery = fetchQuery | |
} | |
function fetchQuery(operation, variables) { | |
const mockedQuery = tester.mock({ | |
query: operation.text, | |
variables, | |
fixture: fixtures[operation.name] | |
}) | |
console.log('Mocked Query: --->', mockedQuery); | |
return mockedQuery.json() | |
} | |
const network = Network.create(networkQuery); | |
const source = new RecordSource(); | |
const store = new Store(source); | |
// export const inspector = new RecordSourceInspector(source); | |
const env = new Environment({ | |
network, | |
store, | |
}); | |
export default env; |
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
export const fixtures = { | |
UserListQuery: { | |
pageInfo: { | |
hasNextPage: false, | |
endCursor: 'hshdwocj=' | |
}, | |
edges: [{ | |
node: { | |
name: 'yay' | |
} | |
}] | |
} | |
} |
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
// This will be the result of the query using easygraphql-tester and the fixtures | |
{ | |
"users": { | |
"pageInfo": { | |
"hasNextPage": false, | |
"endCursor": "hshdwocj=" | |
}, | |
"edges": [ | |
{ | |
"node": { | |
"id": "51", | |
"name": "yay", | |
"__typename": "User" | |
} | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment