Created
January 16, 2017 16:31
-
-
Save idkjs/29b6682b9357b2233908200d887e7c8a to your computer and use it in GitHub Desktop.
refactoring main.js from https://github.com/jscomplete/learning-graphql-and-relay/blob/chapter7/schema/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-use-before-define */ | |
/*eslint no-case-declarations: "error"*/ | |
/*eslint-env es6*/ | |
/* /schema/main.js: refactor of https://github.com/jscomplete/learning-graphql-and-relay/blob/chapter7/schema/main.js */ | |
const { | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLString, | |
GraphQLInt, | |
} = require('graphql') | |
const { | |
mutationWithClientMutationId, | |
globalIdField, | |
fromGlobalId, | |
nodeDefinitions, | |
connectionDefinitions, | |
connectionArgs, | |
connectionFromPromisedArray, | |
} = require('graphql-relay') | |
const { | |
User, | |
getUser, | |
getViewer, | |
} = require('../queries/Viewer') | |
const { ObjectID } = require('mongodb') | |
// Implement Node Interface. See: <https://facebook.github.io/relay/docs/graphql-object-identification.html> | |
const globalIdFetcher = (globalId, { db }) => { | |
const { type, id } = fromGlobalId(globalId) | |
switch (type) { | |
case 'User': | |
return getUser(id); | |
case 'Quote': | |
return db.collection('quotes').findOne(ObjectID(id)) | |
default: | |
return null | |
} | |
} | |
const globalTypeResolver = obj => obj.type || QuoteType | |
// const globalTypeResolver = obj => { | |
// if (obj instanceof Quote) { | |
// return QuoteType; | |
// } else if (obj instanceof User) { | |
// return UserType; | |
// } | |
// return null; | |
// } | |
const { nodeInterface, nodeField } = nodeDefinitions( | |
globalIdFetcher, | |
globalTypeResolver | |
) | |
// // use relay connection helpers to set relay related graphql types. | |
// // see: [graphql-relay-js](https://github.com/graphql/graphql-relay-js#connections). | |
// const { connectionType: QuotesConnectionType } = connectionDefinitions({ | |
// name: 'Quote', | |
// nodeType: QuoteType, | |
// }) | |
const QuoteType = new GraphQLObjectType({ | |
name: 'Quote', | |
fields: { | |
id: globalIdField('Quote', obj => obj._id), | |
text: { type: GraphQLString }, | |
author: { type: GraphQLString }, | |
likesCount: { | |
type: GraphQLInt, | |
resolve: obj => obj.likesCount || 0, | |
}, | |
}, | |
interfaces: [nodeInterface], | |
}) | |
// use relay connection helpers to set relay related graphql types. | |
// see: [graphql-relay-js](https://github.com/graphql/graphql-relay-js#connections). | |
const { connectionType: QuotesConnectionType } = connectionDefinitions({ | |
name: 'Quote', | |
nodeType: QuoteType, | |
}) | |
let connectionArgsWithSearch = connectionArgs | |
connectionArgsWithSearch.searchTerm = { type: GraphQLString } | |
const UserType = new GraphQLObjectType({ | |
name: 'User', | |
fields: { | |
id: globalIdField('User'), | |
quotesConnection: { | |
type: QuotesConnectionType, | |
description: 'A list of the quotes in the database', | |
args: connectionArgsWithSearch, | |
resolve: (_, args, { db }) => { | |
let findParams = {} | |
if (args.searchTerm) { | |
findParams.text = new RegExp(args.searchTerm, 'i') | |
} | |
return connectionFromPromisedArray( | |
db.collection('quotes').find(findParams).toArray(), | |
args | |
) | |
}, | |
}, | |
}, | |
interfaces: [nodeInterface], | |
}); | |
const queryType = new GraphQLObjectType({ | |
name: 'RootQuery', | |
fields: { | |
viewer: { | |
type: UserType, | |
resolve: () => getViewer(), | |
}, | |
node: nodeField, | |
}, | |
}); | |
const thumbsUpMutation = mutationWithClientMutationId({ | |
name: 'ThumbsUpMutation', | |
inputFields: { | |
quoteId: { type: GraphQLString }, | |
}, | |
outputFields: { | |
quote: { | |
type: QuoteType, | |
resolve: obj => obj, | |
}, | |
}, | |
mutateAndGetPayload: (params, { db }) => { | |
const { id } = fromGlobalId(params.quoteId) | |
return Promise.resolve( | |
db.collection('quotes').updateOne({ _id: ObjectID(id) }, | |
{ $inc: { likesCount: 1 } } | |
) | |
).then(result => | |
db.collection('quotes').findOne(ObjectID(id))) | |
}, | |
}) | |
const mutationType = new GraphQLObjectType({ | |
name: 'RootMutation', | |
fields: { | |
thumbsUp: thumbsUpMutation, | |
}, | |
}) | |
const mySchema = new GraphQLSchema({ | |
query: queryType, | |
mutation: mutationType, | |
}) | |
module.exports = mySchema |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment