-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
519555f
commit 1af4799
Showing
7 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"author": "Clay Allsopp <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"fb": "1.1.0-alpha1", | ||
"giphy-api": "1.1.14", | ||
"github-api": "https://github.com/clayallsopp/github/tarball/fork", | ||
"graphql-relay": "0.3.6", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {Facebook, FacebookApiException} from 'fb'; | ||
|
||
const createClient = () => { | ||
const client = new Facebook(); | ||
client.apiAsync = (...args) => { | ||
let promise = new Promise((resolve, reject) => { | ||
return client.api(...args, (res) => { | ||
if(!res || res.error) { | ||
reject(res && res.error); | ||
return; | ||
} | ||
resolve(res); | ||
}); | ||
}); | ||
return promise; | ||
}; | ||
return client; | ||
} | ||
|
||
export const getId = (id, token) => { | ||
const client = createClient(); | ||
client.setAccessToken(token); | ||
return client.apiAsync(id, { metadata: '1' }); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// https://developers.facebook.com/tools/explorer | ||
|
||
import { | ||
getId as getNode | ||
} from './apis/facebook'; | ||
|
||
import { | ||
graphql, | ||
GraphQLSchema, | ||
GraphQLObjectType, | ||
GraphQLString, | ||
GraphQLNonNull, | ||
GraphQLInt, | ||
GraphQLEnumType, | ||
GraphQLBoolean, | ||
GraphQLList, | ||
GraphQLInterfaceType, | ||
GraphQLID | ||
} from 'graphql'; | ||
|
||
import { | ||
nodeDefinitions, | ||
fromGlobalId, | ||
|
||
connectionDefinitions, | ||
connectionArgs, | ||
connectionFromArray, | ||
} from 'graphql-relay'; | ||
|
||
const getTokenFromAST = (ast) => { | ||
return ast.variableValues.facebookToken; | ||
}; | ||
|
||
const getTypeFromFBGraphType = (node) => { | ||
return { | ||
user: userType, | ||
page: pageType, | ||
}[node.metadata.type]; | ||
} | ||
|
||
const {nodeInterface, nodeField} = nodeDefinitions( | ||
(globalId, ast) => { | ||
//const { id } = fromGlobalId(globalId); | ||
return getNode(globalId, getTokenFromAST(ast)); | ||
}, | ||
(obj) => { | ||
return getTypeFromFBGraphType(obj); | ||
//return obj.ships ? factionType : shipType; | ||
} | ||
); | ||
|
||
const profileFields = () => { | ||
return { | ||
id : { | ||
type : new GraphQLNonNull(GraphQLID), | ||
}, | ||
metadataType : { | ||
type : new GraphQLNonNull(GraphQLString), | ||
resolve(profile) { | ||
return profile.metadata.type; | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
const profileInterface = new GraphQLInterfaceType({ | ||
name: 'FacebookProfile', | ||
resolveType: getTypeFromFBGraphType, | ||
fields: { | ||
...profileFields() | ||
} | ||
}); | ||
|
||
const pageType = new GraphQLObjectType({ | ||
name: 'FacebookPage', | ||
interfaces: [nodeInterface, profileInterface], | ||
fields: () => { | ||
return { | ||
...profileFields(), | ||
likes: { | ||
type : GraphQLInt | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
const CURSOR_NOT_SUPPORTED = 'NOT SUPPORTED'; | ||
|
||
const userType = new GraphQLObjectType({ | ||
name: 'FacebookUser', | ||
interfaces: [nodeInterface, profileInterface], | ||
fields: () => { | ||
return { | ||
...profileFields(), | ||
name : { | ||
type : GraphQLString, | ||
resolve(user, args, ast) { | ||
return getNode(user.id, getTokenFromAST(ast)).then((res) => { | ||
return res.name; | ||
}) | ||
} | ||
}, | ||
likes : { | ||
type : likesConnectionDefinitions.connectionType, | ||
args : connectionArgs, | ||
resolve(user, args, ast) { | ||
return getNode(user.id + `/likes`, getTokenFromAST(ast)).then((response) => { | ||
|
||
const { data, paging, summary} = response; | ||
const pages = data; | ||
|
||
console.log(response) | ||
|
||
const edges = pages.map((page) => { | ||
return { | ||
cursor : CURSOR_NOT_SUPPORTED, | ||
node : page | ||
}; | ||
}); | ||
const pageInfo = { | ||
startCursor : paging.cursors.before, | ||
endCursor : paging.cursors.after, | ||
hasPreviousPage : (!!paging.previous), | ||
hasNextPage : (!!paging.next) | ||
}; | ||
|
||
return { | ||
edges, | ||
pageInfo | ||
}; | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const likesConnectionDefinitions = connectionDefinitions({ nodeType : pageType }); | ||
|
||
|
||
const fbType = new GraphQLObjectType({ | ||
name : 'FacebookAPI', | ||
fields : { | ||
viewer : { | ||
type : userType, | ||
resolve(root, args, ast) { | ||
return getNode('me', getTokenFromAST(ast)); | ||
} | ||
}, | ||
node: nodeField | ||
} | ||
}) | ||
|
||
export const QueryObjectType = fbType; | ||
|
||
export const QueryArgsType = { | ||
token : { | ||
type : new GraphQLNonNull(GraphQLString) | ||
} | ||
}; |
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
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
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
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