Skip to content

Instantly share code, notes, and snippets.

@davewasmer
Created July 20, 2019 22:37
Show Gist options
  • Save davewasmer/25ebb3ca6fafd3d2073a72b2b1eb7d66 to your computer and use it in GitHub Desktop.
Save davewasmer/25ebb3ca6fafd3d2073a72b2b1eb7d66 to your computer and use it in GitHub Desktop.
Mirage & GraphQL
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