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.
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