Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
brettsmentek committed May 29, 2019
0 parents commit 5c280f5
Show file tree
Hide file tree
Showing 9 changed files with 917 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IEX_PUBLIC=Your-public-api-key
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
.now
www/.next
**/node_modules
1 change: 1 addition & 0 deletions .nowignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
27 changes: 27 additions & 0 deletions README.md
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)
33 changes: 33 additions & 0 deletions api/gql/iexApi.js
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
77 changes: 77 additions & 0 deletions api/gql/index.js
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' })
13 changes: 13 additions & 0 deletions api/gql/package.json
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"
}
}
Loading

1 comment on commit 5c280f5

@vercel
Copy link

@vercel vercel bot commented on 5c280f5 May 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.