Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created Jul 1, 2016
Embed
What would you like to do?
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