Created
April 9, 2018 17:27
-
-
Save stolinski/7cea67d470b4299828b1293aa8cbde0c to your computer and use it in GitHub Desktop.
Level Up Tutorials MockMang
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
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools'; | |
import { graphql } from 'graphql'; | |
import GraphQLMock from 'graphql-mock'; | |
import typeDefs from 'imports/startup/both/typeDefs'; | |
// Make a GraphQL schema with no resolvers | |
const schema = makeExecutableSchema({ typeDefs }); | |
// Creates random id | |
const revisedRandId = () => | |
Math.random() | |
.toString(36) | |
.replace(/[^a-z]+/g, '') | |
.substr(2, 10); | |
// Makes all ID types random ids instead of a hello world string | |
const mocks = { | |
ID: () => revisedRandId(), | |
}; | |
// Add mocks, modifies schema in place | |
addMockFunctionsToSchema({ schema, mocks }); | |
// Takes a query and args and returns mocked data | |
const mockMang = async (query, args = {}) => { | |
try { | |
const res = await graphql(schema, query.loc.source.body, null, null, args); | |
res.data.loading = false; | |
return res.data; | |
} catch (e) { | |
return console.log(e.message); | |
} | |
}; | |
// Creates a mocked client | |
export const mockClient = new GraphQLMock(typeDefs); | |
export default mockMang; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment