Created
July 20, 2019 22:37
-
-
Save davewasmer/25ebb3ca6fafd3d2073a72b2b1eb7d66 to your computer and use it in GitHub Desktop.
Mirage & GraphQL
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
import config from '../config/environment'; | |
import { makeExecutableSchema } from 'graphql-tools'; | |
import { graphql } from 'graphql'; | |
import schemaString from './schema.graphql'; | |
// Define your Mirage resolvers (we usually do separate files, import, and merge) | |
const resolvers = {}; | |
export default function() { | |
// Schema setup | |
let schema = makeExecutableSchema({ | |
typeDefs: schemaString, | |
resolvers | |
}); | |
this.post(config.api.endpoint, (mirageSchema, request) => { | |
let { query, variables } = JSON.parse(request.requestBody); | |
let context = { | |
mirage: mirageSchema, | |
server: this, | |
db: mirageSchema.db | |
}; | |
return graphql(schema, query, {}, context, variables) | |
.then((result) => { | |
if (result.errors) { | |
console.log('Error processing query:', query); | |
let error = result.errors[0]; | |
console.log(error.originalError && error.originalError.stack || error); | |
} | |
return result; | |
}); | |
}, 200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment