Skip to content

Instantly share code, notes, and snippets.

@baer
Last active August 23, 2017 00:47
Show Gist options
  • Save baer/93235c4c397de0c63d91478528d6305e to your computer and use it in GitHub Desktop.
Save baer/93235c4c397de0c63d91478528d6305e to your computer and use it in GitHub Desktop.
// ------------
// REST
// ------------
// This is approxamately the function you would run to fetch the "Customer"
// resource for a REST endpoint. The HTTP stuff is normally be left to your
// framework so I'll leave that out.
app.serve("customer/:id", (urlParams) => {
return myORM.queryById(Customer, urlParams.id);
})
// ------------
// GQL Option 1
// ------------
const RootQuery = new GraphQLObjectType({
name: "RootQuery",
fields: {
customer: { type: Customer }
}
});
// If you write a GraphQL schema like this, you'll end up hammering your DB.
// This can be okay so long as you know to expect it and can handle it accordingly
const Customer = new GraphQLObjectType({
name: "Customer",
fields: {
firstName: {
type: GraphQLString
resolve: () => {
return myORM.queryById(Customer, urlParams.id)
.get("firstName");
}
}
lastName: {
type: GraphQLString,
resolve: () => {
return myORM.queryById(Customer, urlParams.id)
.get("firstName");
}
}
}
});
// ------------
// GQL Option 2
// ------------
const RootQuery = new GraphQLObjectType({
name: "RootQuery",
fields: {
customer: {
type: Customer,
resolve: () => {
return myORM.queryById(Customer, urlParams.id);
// or
// request.get(`${config.domain}/user/${urlParams.id}`)
}
}
}
});
// If you write a GraphQL schema like this, you only make a single call to
// your DB or service and the "query" is just selecting fields
const Customer = new GraphQLObjectType({
name: "Customer",
fields: {
firstName: {
type: GraphQLString
}
lastName: {
type: GraphQLString,
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment