-
Notifications
You must be signed in to change notification settings - Fork 2
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
0 parents
commit 5c280f5
Showing
9 changed files
with
917 additions
and
0 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
IEX_PUBLIC=Your-public-api-key |
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,4 @@ | ||
.env | ||
.now | ||
www/.next | ||
**/node_modules |
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 @@ | ||
.env |
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,27 @@ | ||
# GraphQL + IEX Cloud | ||
|
||
## What is this? | ||
|
||
This is an Apollo GraphQL server that can be used to fetch financial data from IEX Cloud. It is built on top of IEX Cloud's REST API, and allows you to combine data from multiple REST endpoints with one query. Also, you can limit the fields returned for each query to that which is only needed by the client. It is deployed using [Now 2.0](https://zeit.co/blog/now-2). | ||
|
||
Here is an example query for getting data on `AMZN` from a variety of IEX Cloud API endpoints: | ||
|
||
``` | ||
query { | ||
security(ticker: "AMZN") { | ||
symbol | ||
volume | ||
lastSalePrice | ||
estimates { | ||
consensusEPS | ||
numberOfEstimates | ||
} | ||
earnings { | ||
actualEPS | ||
EPSReportDate | ||
} | ||
} | ||
} | ||
``` | ||
|
||
[Data provided by IEX Cloud](https://iexcloud.io) |
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,33 @@ | ||
const { RESTDataSource } = require('apollo-datasource-rest') | ||
|
||
class IexApi extends RESTDataSource { | ||
constructor() { | ||
super() | ||
} | ||
|
||
willSendRequest(request) { | ||
request.params.set('token', process.env.IEX_PUBLIC) | ||
} | ||
|
||
get baseURL() { | ||
return `https://cloud.iexapis.com/stable/` | ||
} | ||
|
||
async getEstimates(symbol) { | ||
return this.get( | ||
`stock/${symbol}/estimates`, | ||
) | ||
} | ||
|
||
async getEarnings(symbol) { | ||
return this.get( | ||
`stock/${symbol}/earnings/4` | ||
) | ||
} | ||
|
||
async getTops(symbol) { | ||
return this.get(`tops?symbols=${symbol}`) | ||
} | ||
} | ||
|
||
module.exports = IexApi |
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,77 @@ | ||
const { ApolloServer, gql } = require('apollo-server-micro') | ||
const IexApi = require('./iexApi') | ||
|
||
// The GraphQL schema | ||
const typeDefs = gql` | ||
type Security { | ||
lastSalePrice: Float | ||
volume: Int | ||
sector: String | ||
symbol: String! | ||
securityType: String | ||
bidPrice: Float | ||
bidSize: Int | ||
askPrice: Float | ||
lastUpdated: Int | ||
lastSaleSize: Int | ||
estimates: Estimate | ||
earnings: [Earnings] | ||
} | ||
type Estimate { | ||
consensusEPS: Float | ||
announceTime: String | ||
numberOfEstimates: Int | ||
reportDate: String | ||
fiscalPeriod: String | ||
fiscalEndDate: String | ||
} | ||
type Earnings { | ||
actualEPS: Float | ||
consensusEPS: Float | ||
announceTime: String | ||
numberOfEstimates: Int | ||
EPSSurpriseDollar: Float | ||
EPSReportDate: String | ||
fiscalPeriod: String | ||
fiscalEndDate: String | ||
yearAgo: Float | ||
yearAgoChangePercent: Float | ||
} | ||
type Query { | ||
security(ticker: String!): Security! | ||
} | ||
` | ||
|
||
// A map of functions which return data for the schema. | ||
const resolvers = { | ||
Query: { | ||
security: async (_source, { ticker }, { dataSources }) => { | ||
return (await dataSources.iexApi.getTops(ticker))[0] | ||
}, | ||
}, | ||
Security: { | ||
estimates: async (parent, args, { dataSources }) => { | ||
return (await dataSources.iexApi.getEstimates(parent.symbol)).estimates[0] | ||
}, | ||
earnings: async (parent, args, { dataSources }) => { | ||
return (await dataSources.iexApi.getEarnings(parent.symbol)).earnings | ||
}, | ||
} | ||
} | ||
|
||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
dataSources: () => { | ||
return { | ||
iexApi: new IexApi(), | ||
} | ||
}, | ||
introspection: true, | ||
playground: true, | ||
}) | ||
|
||
module.exports = server.createHandler({ path: '/api/gql' }) |
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,13 @@ | ||
{ | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "micro" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"apollo-datasource-rest": "^0.4.0", | ||
"apollo-server-micro": "^2.5.0", | ||
"graphql": "^14.3.0", | ||
"micro": "^9.3.4" | ||
} | ||
} |
Oops, something went wrong.
5c280f5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully aliased the URL https://iex-graphql-mv8ezje36.now.sh to the following aliases.