Code Monkey home page Code Monkey logo

graphql-fpl-api's Introduction

graphql-fpl-api

A Rich GraphQL proxy layer on top of the official Fantasy Premier League REST API

โš ๏ธ This api does not support the two highly volatile data from the official api: gameweek live data and event status. This api relies heavily on caching to reduce the number of roundtrips to the official api and these two datasets are too dynamic to be cached without the risk of constantly serving stale data. An efficient cache invalidation strategy for this case is still being worked on.

Try the live demo here

Why graphql-fpl-api?

Consider the following query:

query Players {
	saka: player(id: 19) {
		...playerStats
	}
	salah: player(id: 308) {
		...playerStats
	}
}

fragment playerStats on Player {
	first_name
	second_name
	total_points

	upcoming_fixtures(first: 1) {
		is_home
		event
		difficulty
		team_h {
			name
		}
		team_a {
			name
		}
	}

	past_fixtures {
		goals_scored
		assists
		opponent_team {
			name
		}
	}
}

In order to get the same data using the official REST api, you would have to make 4 separate requests:

1. https://fantasy.premierleague.com/api/bootstrap-static/     - 1.36 MB
2. https://fantasy.premierleague.com/api/fixtures/             - 238.9 KB
3. https://fantasy.premierleague.com/api/element-summary/19/   - 15.1 KB
4. https://fantasy.premierleague.com/api/element-summary/308/  - 16.64 KB

These 4 requests would, as of the time of writing this - 2023/24 season, return a total of 1.63 MB of data transferred over the network. With graphql-fpl-api, you only need to make one request. This is a total of 763 B (gzipped) transferred over the network. That's a 99.96% reduction in data transferred over the network!

You might be worried that since graphql-fpl-api is a proxy layer on top of the official api, you would be making twice the number of roundtrips you would have made if you were to use the official api directly. This is not the case. As hinted earlier, graphql-fpl-api also caches the data from the official api. This means that you can make the same query multiple times and only the first request from the first client will hit the official api. Subsequent requests will be served from the cache. This is especially useful for data that doesn't change often like player data, game week data, and fixtures data.

Getting started

Installation

  1. Clone the repository to your local machine using git clone https://github.com/AustinMusiku/graphql-fpl-api
  2. Copy the contents of the .env.example file to a new file named .env and fill in the required fields.

using yarn

  1. Run the api using yarn dev

using docker

  1. Build the docker image using docker build -t graphql-fpl-api .
  2. Run the api using docker run -p 4500:4500 graphql-fpl-api

The api should now be test it out by visiting http://localhost:4500/graphql in your browser.

Usage

Example query for a getting a manager's data

query Manager {
	manager(id: 1528511) {
		player_first_name
		player_last_name
		leagues {
			classic {
				name
				entry_rank
			}
		}
		squad(gwId: 8) {
			picks {
				element {
					web_name
				}
				is_captain
			}
		}
	}
}

Example query for a getting a specific gameweek's data

query Gameweek6 {
	gameweek(id: 6) {
		id
		deadline_time
		finished
		highest_score
		avg_points
		chip_plays {
			chip_name
			num_played
		}
	}
}

Contributing

I could use some help with writing tests. If you're interested in helping out, please feel free to open a pull request.

graphql-fpl-api's People

Contributors

austinmusiku avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

laduygaga

graphql-fpl-api's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.