Last active
April 5, 2017 06:12
-
-
Save faizalpribadi/7efaa8394005d77ef1ac39fe81f7f23e to your computer and use it in GitHub Desktop.
graphql-server
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
import { | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLList, | |
GraphQLString, | |
GraphQLID, | |
GraphQLBoolean, | |
GraphQLNonNull, | |
GraphQLInt | |
} from 'graphql'; | |
const users = [ | |
{ | |
id: 0, name: 'ical', active: false | |
}, | |
{ | |
id: 1, name: 'noom', active: false | |
}, | |
{ | |
id: 2, name: 'marissa', active: false | |
}, | |
{ | |
id: 3, name: 'john', active: true | |
}, | |
{ | |
id: 4, name: 'algebra', active: false | |
} | |
]; | |
const followers = [ | |
{ | |
id: 103, | |
name: 'anonymous' | |
} | |
]; | |
const likers = [ | |
{ | |
total: 123 | |
} | |
] | |
const LikerType = new GraphQLObjectType({ | |
name: 'LikerType', | |
fields: () => ({ | |
total: { | |
type: GraphQLInt | |
} | |
}) | |
}) | |
const FollowerType = new GraphQLObjectType({ | |
name: 'FollowerType', | |
fields: () => ({ | |
id: { | |
type: GraphQLID | |
}, | |
name: { | |
type: GraphQLString | |
}, | |
likers: { | |
type: new GraphQLList(LikerType), | |
resolve: () => { | |
return new Promise((resolve, reject) => { | |
resolve(likers) | |
}) | |
} | |
} | |
}) | |
}); | |
const UserType = new GraphQLObjectType({ | |
name: 'UserTypeQL', | |
description: 'A users type', | |
fields: () => ({ | |
id: { | |
type: GraphQLID, | |
description: 'ID' | |
}, | |
name: { | |
type: GraphQLString, | |
description: 'A field name' | |
}, | |
active: { | |
type: GraphQLBoolean, | |
description: 'A field of active' | |
}, | |
followers: { | |
type: new GraphQLList(FollowerType), | |
resolve: () => { | |
return new Promise((resolve, reject) => { | |
resolve(followers) | |
}) | |
} | |
} | |
}) | |
}); | |
const UsersQuery = new GraphQLObjectType({ | |
name: 'UsersQuery', | |
description: 'A users query', | |
fields: () => ({ | |
users: { | |
type: new GraphQLList(UserType), | |
description: 'A list of users', | |
resolve: () => { | |
return users | |
} | |
} | |
}) | |
}); | |
const UsersMutation = new GraphQLObjectType({ | |
name: 'UsersMutation', | |
description: 'A users mutation', | |
fields: () => ({ | |
addUser: { | |
type: UserType, | |
args: { | |
id: { | |
type: new GraphQLNonNull(GraphQLID) | |
}, | |
name: { | |
type: new GraphQLNonNull(GraphQLString) | |
} | |
}, | |
resolve: (root, {id, name}) => { | |
let merge = { | |
id: id, | |
name: name, | |
active: true | |
} | |
let result = Object.assign(users, merge); | |
return new Promise((resolve, reject) => { | |
resolve(result) | |
}) | |
} | |
} | |
}) | |
}) | |
const schema = new GraphQLSchema({ | |
query: UsersQuery, | |
mutation: UsersMutation | |
}); | |
export default schema; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment