Skip to content

Instantly share code, notes, and snippets.

@samselikoff
Created November 20, 2019 18:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samselikoff/36d40f2c3680de46305c7ca2a157c6b0 to your computer and use it in GitHub Desktop.
Save samselikoff/36d40f2c3680de46305c7ca2a157c6b0 to your computer and use it in GitHub Desktop.
mirage-graphql-example
import { buildSchema, graphql } from "graphql";
// Construct a schema, using GraphQL schema language
let graphqlSchema = buildSchema(`
type Query {
recipes: [Recipe]
recipes_by_pk(id: Int!): Recipe
}
type Recipe {
id: ID!
name: String!
imageUrl: String
recipeUrl: String
ingredients: String
instructions: String
}
type recipes_insert_input {
id: Int
imageUrl: String
ingredients: String
instructions: String
name: String
recipeUrl: String
}
type Mutation {
insert_recipes(objects: [recipes_insert_input!]!): Recipe!
}
`);
export default function() {
this.urlPrefix = "https://samselikoff-recipes-backend.herokuapp.com";
this.post("/v1/graphql", (schema, request) => {
let requestJson = JSON.parse(request.requestBody);
let query = requestJson.query;
let variables = requestJson.variables;
let resolver = {
recipes() {
return schema.db.recipes;
},
recipes_by_pk(args) {
return schema.db.recipes.find(args.id);
}
};
return graphql(graphqlSchema, query, resolver, null, variables).then(
response => {
return response;
}
);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment