Skip to content

Instantly share code, notes, and snippets.

@samselikoff
Created December 4, 2019 17:19
Show Gist options
  • Save samselikoff/0e176a76e5be53cbb94e85020fc2b115 to your computer and use it in GitHub Desktop.
Save samselikoff/0e176a76e5be53cbb94e85020fc2b115 to your computer and use it in GitHub Desktop.

Revisions

  1. samselikoff created this gist Dec 4, 2019.
    54 changes: 54 additions & 0 deletions mirage-graphql-example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    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;
    }
    );
    });
    }