Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created July 1, 2016 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/b1ea7e7c2b53b192b8d41435bd7e2b86 to your computer and use it in GitHub Desktop.
Save chuck0523/b1ea7e7c2b53b192b8d41435bd7e2b86 to your computer and use it in GitHub Desktop.
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} from 'graphql'
import {
connectionArgs,
connectionDefinitions,
connectionFromArray,
fromGlobalId,
globalIdField,
mutationWithClientMutationId,
nodeDefinitions,
} from 'graphql-relay'
import {
User,
Job,
getChuck,
getUsers,
getUser,
getJobs,
getJob,
} from './database'
var { nodeInterface, nodeField } = nodeDefinitions(
(globalId) => {
var {type, id} = fromGlobalId(globalId)
if(type === 'User') {
return getUser(id)
} else if(type === 'Job') {
return getJob(id)
} else {
return null
}
},
(obj) => {
if(obj instanceof User) {
return userType
} else if(obj instanceof Job) {
return jobType
} else {
return null
}
}
)
var userType = new GraphQLObjectType({
name: 'User',
description: 'A persion who use our app',
fields: () => ({
id: globalIdField('User'),
name: {
type: GraphQLString,
description: 'The name of the user',
resolve: (chuck) => chuck.name
},
jobs: {
type: jobConnection,
description: 'A person\s collection of jobs',
args: connectionArgs,
resolve: (_, args) => connectionFromArray(getJobs(), args)
}
}),
interfaces: [nodeInterface],
})
// console.log(userType['_typeConfig']['fields']()['jobs']['args'])
var jobType = new GraphQLObjectType({
name: 'Job',
description: 'A job',
fields: () => ({
id: globalIdField('Job'),
name: {
type: GraphQLString,
description: 'The name of the job',
resolve: (job) => job.name,
},
salary: {
type: GraphQLInt,
description: 'The salary of the job',
resolve: (job) => job.salary,
}
}),
interfaces: [nodeInterface],
})
var { connectionType: jobConnection } =
connectionDefinitions({name: 'Job', nodeType: jobType})
var queryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeField,
chuck: {
type: userType,
resolve: () => getChuck(),
}
})
})
export var Schema = new GraphQLSchema({
query: queryType,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment