Skip to content

Instantly share code, notes, and snippets.

@entyo

entyo/index.js Secret

Created Sep 28, 2018
Embed
What would you like to do?
Apollo GraphQL API
// Welcome to Launchpad!
// Log in to edit and save pads, run queries in GraphiQL on the right.
// Click "Download" above to get a zip with a standalone Node.js server.
// See docs and examples at https://github.com/apollographql/awesome-launchpad
// graphql-tools combines a schema string with resolvers.
import { makeExecutableSchema } from 'graphql-tools'
// Construct a schema, using GraphQL schema language
const typeDefs = `
type Mutation {
addWhatIWantToList(input: WhatIWantInput!): WishList!
createNewList(input: ListInput!): [WishList!]!
}
input WhatIWantInput {
name: String!
description: String
nHowMany: Int!
listId: Int!
}
input ListInput {
title: String!
description: String
}
type WhatIWant {
id: Int!
name: String!
description: String
nHowMany: Int!
}
type WishList {
id: Int!
title: String!
description: String
things: [WhatIWant!]!
}
type Query {
# All wishlists
myWishLists: [WishList!]!
whatIWant(id: Int!): WhatIWant!
wishList(id: Int!): WishList!
wishLists(id: Int!): [WishList!]!
}
`
// 余裕があればこういうのも使ってみたい https://developers.google.com/books/
// i.e.) https://www.googleapis.com/books/v1/volumes?q=isbn:4774176982
const BooksIWant = [
{
id: 1,
name: 'Haskellによる並列・並行プログラミング',
description: '難しそう',
nHowMany: 1
},
{
id: 2,
name: 'Coq/SSReflect/MathCompによる定理証明:フリーソフトではじめる数学の形式化',
description: '最近出たっぽい',
nHowMany: 1
}
]
const FoodsIWannaEat = [
{
id: 3,
name: 'うな重',
description: '食べて応援',
nHowMany: 10
},
{
id: 4,
name: 'エビフライ',
description: '普通に好き',
nHowMany: 5
}
]
const whatIWants = [...BooksIWant, ...FoodsIWannaEat]
const wishLists = [
{
id: 1,
title: 'ほしい本',
description: 'ほしいけれど高くて買えていない本のリスト',
things: BooksIWant
},
{
id: 2,
title: '食べたいもの',
description: '高級食材',
things: FoodsIWannaEat
}
]
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
myWishLists: () => wishLists,
whatIWant: (_, { id }) => whatIWants.find(v => id === v.id),
wishList: (_, { id }) => wishLists.find(v => id === v.id),
},
Mutation: {
addWhatIWantToList: (_, { input: {nHowMany, name, description, listId} }) => {
const wl = wishLists.find(v => v.id === listId)
const { things } = wl
const id = whatIWants[whatIWants.length - 1].id + 1
const newItem = { id, nHowMany, name, description }
things.push(newItem)
// Update API side datastore
whatIWants.push(newItem)
const newList = {
...wl,
things
}
wishLists.filter(v => v.id !== listId).push(newList)
return newList
},
createNewList: (_, {input: {title, description}}) => {
const id = wishLists[wishLists.length - 1].id + 1
const newList = {
id,
title,
description,
things: []
}
wishLists.push(newList)
return wishLists
}
}
}
// Required: Export the GraphQL.js schema object as "schema"
export const schema = makeExecutableSchema({
typeDefs,
resolvers,
})
// Optional: Export a function to get context from the request. It accepts two
// parameters - headers (lowercased http headers) and secrets (secrets defined
// in secrets section). It must return an object (or a promise resolving to it).
export function context(headers, secrets) {
return {
headers,
secrets,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment