Skip to content

Instantly share code, notes, and snippets.

@ctataryn
Created August 28, 2017 20:22
Show Gist options
  • Save ctataryn/c4b492f7c710f74eae03da35dea669c2 to your computer and use it in GitHub Desktop.
Save ctataryn/c4b492f7c710f74eae03da35dea669c2 to your computer and use it in GitHub Desktop.
var schema = `
enum Gender {
MALE
FEMALE
}
type Movie {
id: ID!
title: String!
year: Int
actors: [Actor]
}
type Actor {
fname: String!
mname: String
lname: String!
dob: String!
gender: Gender
}
type Query {
movie(id: ID!): Movie
}
`;
/*
returns a hash like this:
{
id: 12345,
marquee: {
title: "The Matrix"
},
year: 1999
}
*/
function getMovie(movieId) {
//Get a movie from somewhere
}
/*
returns a hash like this:
{
name: {
first: "Keanu",
middle: "",
last: "Reeves"
},
dob: "9/2/1964",
gender: "M"
}
*/
function getActorsForMovie(id) {
//Get the actors for a movie
}
var resolvers = {
Query: {
movie(obj, { id: movieId }, ctx) {
//call an API that gets a movie
let movie = getMovie(movieId);
return movie;
}
},
Movie: {
id(movie, args, ctx) {
return movie.id;
},
title(movie, args, ctx) {
return movie.marquee.title;
},
//etc..
actors({ id: movieId }, args, ctx) {
// call an API to get all Actors for a Movie
let actors = getActorsforMovie(movieId);
return actors;
}
},
Actor: {
fname(actor, args, ctx) {
return obj.name.first;
},
//etc...
gender(actor, args, ctx) {
return obj.gender == 'M' ? "MALE" : "FEMALE";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment