Skip to content

Instantly share code, notes, and snippets.

@collingo
Last active July 3, 2017 10:01
Show Gist options
  • Save collingo/5d60a63a0aba103361e28a09470d3d3b to your computer and use it in GitHub Desktop.
Save collingo/5d60a63a0aba103361e28a09470d3d3b to your computer and use it in GitHub Desktop.
GraphCool schema mocking
const fs = require('fs')
const path = require('path')
const casual = require('casual')
const { makeExecutableSchema, addMockFunctionsToSchema } = require('graphql-tools')
const { graphql } = require('graphql')
fs.readFile(path.join(__dirname, './schema.graphql'), (error, schemaBuffer) => {
const schemaString = schemaBuffer.toString()
const schema = makeExecutableSchema({
typeDefs: schemaString
})
const mocks = {
Int: () => casual.integer(1, 99),
Float: () => casual.double(1, 99),
String: () => casual.string,
DateTime: () => "2017-06-28T15:33:23.000Z"
}
addMockFunctionsToSchema({
schema,
mocks
})
const query = `
query {
Job(id: 1) {
id
title
company {
createdAt
name
location
}
}
}
`
graphql(schema, query).then((result) => console.log('Got result', JSON.stringify(result, null, 2)))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment