Skip to content

Instantly share code, notes, and snippets.

@entyo entyo/index.js Secret

Last active Sep 22, 2018
Embed
What would you like to do?
GraphQL Wishlist Example With User System(https://launchpad.graphql.com/3kvzv77m1v)
// 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(name: String!, description: String, nHowMany: Int!, listId: Int!): WishList!
createNewList(title: String!, description: String): [WishList!]!
}
type WhatIWant {
id: Int!
name: String!
description: String
nHowManyWant: Int!
}
type WishList {
id: Int!
# user: User!
title: String!
description: String
things: [WhatIWant]!
}
# type User {
# id: Int!
# name: String!
# wishLists: [WishList]!
# }
type Query {
# All wishlists
myWishLists: [WishList!]!
whatIWant(id: Int!): WhatIWant!
wishList(id: Int!): WishList!
wishLists(id: Int!): [WishList]!
# wishListsByUserId(id :ID!): [WishList]!
# me: User!
# user(id: Int!): User!
}
`
// 余裕があればこういうのも使ってみたい https://developers.google.com/books/
// i.e.) https://www.googleapis.com/books/v1/volumes?q=isbn:4774176982
const BooksIWant = [
{
id: 1,
name: 'Haskellによる並列・並行プログラミング',
description: '難しそう',
nHowManyWant: 1
},
{
id: 2,
name: 'Coq/SSReflect/MathCompによる定理証明:フリーソフトではじめる数学の形式化',
description: '最近出たっぽい',
nHowManyWant: 1
}
]
const FoodsIWannaEat = [
{
id: 3,
name: 'うな重',
description: '食べて応援',
nHowManyWant: 10
},
{
id: 4,
name: 'エビフライ',
description: '普通に好き',
nHowManyWant: 8000
}
]
const whatIWants = [...BooksIWant, ...FoodsIWannaEat]
// const myId = 0;
const wishLists = [
{
id: 1,
// userId: myId,
title: 'ほしい本',
description: 'ほしいけれど高くて買えていない本のリスト',
things: BooksIWant
},
{
id: 2,
// userId: myId,
title: '食べたいもの',
description: '高級食材',
things: FoodsIWannaEat
}
]
// User
// const me = {
// id: myId,
// name: 'e_ntyo'
// };
// const user1 = {
// id: 1,
// name: 'potato4d'
// };
// const user2 = {
// id: 2,
// name: 'masaakikunsan'
// }
// const users = [me, user1, user2]
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
// me: () => me,
myWishLists: () => wishLists,
whatIWant: (_, { id }) => whatIWants.find(v => id === v.id),
wishList: (_, { id }) => wishLists.find(v => id === v.id),
// user: (_, { id }) => users.find(v => id === v.id)
},
Mutation: {
addWhatIWantToList: (_, { nHowMany, name, description, listId }) => {
const wl = wishLists.find(v => v.id === listId)
const { things } = wl
const { id } = things[things.length - 1]
things.push({ id: id + 1, nHowMany, name, description })
const newList = {
...wl,
things
}
wishLists.filter(v => v.id !== listId).push(newList)
return newList
},
createNewList: (_, {title, description}) => {
const id = wishLists[wishLists.length - 1].id + 1
const newList = {
id,
title,
description
}
wishLists.push(newList)
return wishLists
}
}
// User: {
// wishLists: user => wishLists.filter(v => v.userId === user.id)
// },
// WishList: {
// user: wishList => users.find(v => v.id === wishList.userId)
// }
}
// 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
You can’t perform that action at this time.