You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
GraphQL layer on top of the official fantasy premier league api.
4
-
This is a the first of a two-part upgrade of my [fplfriend](https://fplfriend.up.railway.app/) app(You seriously need to check it out, it's awesome, I promise😉) where I focus on writing a decoupled backend using typescript and graphql as compared to the initial app version which was all written in javascript and used nodejs + ejs for templating. Although this api adds support for new data offered by the official fantasy premier league e.g., xG, xA, xGI, xGC, etc, it doesn't include all the data from the official api. I'll be adding more data as I need it for the frontend. If you need more data, feel free to open an issue and I'll add it.
3
+
A Rich GraphQL proxy layer on top of the official Fantasy Premier League REST API
4
+
5
+
⚠️ 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.
6
+
7
+
[Try the live demo here](https://graphql-fpl-api.up.railway.app/graphql)
8
+
9
+
## Why graphql-fpl-api?
10
+
11
+
Consider the following query:
12
+
13
+
```graphql
14
+
queryPlayers {
15
+
saka: player(id: 19) {
16
+
...playerStats
17
+
}
18
+
salah: player(id: 308) {
19
+
...playerStats
20
+
}
21
+
}
22
+
23
+
fragmentplayerStatsonPlayer {
24
+
first_name
25
+
second_name
26
+
total_points
27
+
28
+
upcoming_fixtures(first: 1) {
29
+
is_home
30
+
event
31
+
difficulty
32
+
team_h {
33
+
name
34
+
}
35
+
team_a {
36
+
name
37
+
}
38
+
}
39
+
40
+
past_fixtures {
41
+
goals_scored
42
+
assists
43
+
opponent_team {
44
+
name
45
+
}
46
+
}
47
+
}
48
+
```
49
+
50
+
In order to get the same data using the official REST api, you would have to make 4 separate requests:
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!
60
+
61
+
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.
5
62
6
63
## Getting started
7
64
8
65
### Installation
9
66
10
-
#### prerequisites
11
-
12
67
1. Clone the repository to your local machine using `git clone https://github.com/AustinMusiku/graphql-fpl-api`
13
68
2. Copy the contents of the `.env.example` file to a new file named `.env` and fill in the required fields.
14
69
@@ -25,89 +80,85 @@ The api should now be test it out by visiting [http://localhost:4500/graphql](ht
25
80
26
81
## Usage
27
82
28
-
### Example query for a getting a player's data
83
+
### Example query for a getting a manager's data
29
84
30
85
```graphql
31
-
{
32
-
saka: player(id: 13) {
33
-
...arsenalPlayer
34
-
}
35
-
odegaard: player(id: 7) {
36
-
...arsenalPlayer
37
-
}
38
-
salah: player(id: 283) {
39
-
...liverpoolPlayer
40
-
}
41
-
alexander_Arnold: player(id: 285) {
42
-
...liverpoolPlayer
43
-
}
44
-
}
45
-
46
-
fragmentarsenalPlayeronPlayer {
47
-
first_name
48
-
second_name
49
-
total_points
50
-
}
51
-
52
-
fragmentliverpoolPlayeronPlayer {
53
-
web_name
54
-
UpcomingFixtures(first: 2) {
55
-
is_home
56
-
difficulty
86
+
queryManager {
87
+
manager(id: 1528511) {
88
+
player_first_name
89
+
player_last_name
90
+
leagues {
91
+
classic {
92
+
name
93
+
entry_rank
94
+
}
95
+
}
96
+
squad(gwId: 8) {
97
+
picks {
98
+
element {
99
+
web_name
100
+
}
101
+
is_captain
102
+
}
103
+
}
57
104
}
58
105
}
59
106
```
60
107
61
108
Produces the following response:
62
109
63
110
```json
64
-
{
65
-
"data": {
66
-
"saka": {
67
-
"first_name": "Bukayo",
68
-
"second_name": "Saka",
69
-
"total_points": 172
70
-
},
71
-
"odegaard": {
72
-
"first_name": "Martin",
73
-
"second_name": "Ødegaard",
74
-
"total_points": 171
75
-
},
76
-
"salah": {
77
-
"web_name": "Salah",
78
-
"UpcomingFixtures": [
79
-
{
80
-
"is_home": true,
81
-
"difficulty": 2
111
+
"data": {
112
+
"manager": {
113
+
"player_first_name": "Austin",
114
+
"player_last_name": "Musiku",
115
+
"leagues": {
116
+
"classic": [
117
+
{
118
+
"name": "Arsenal",
119
+
"entry_rank": 575740
120
+
},
121
+
{
122
+
"name": "Kenya",
123
+
"entry_rank": 56656
124
+
},
125
+
{
126
+
"name": "Gameweek 1",
127
+
"entry_rank": 3334468
128
+
},
129
+
//...
130
+
{
131
+
"name": "DEVELOPERS PREMIER LEAGUE",
132
+
"entry_rank": 4
133
+
}
134
+
]
135
+
},
136
+
"squad": {
137
+
"picks": [
138
+
{
139
+
"element": {
140
+
"web_name": "Areola"
82
141
},
83
-
{
84
-
"is_home": false,
85
-
"difficulty": 3
86
-
}
87
-
]
88
-
},
89
-
"alexander_Arnold": {
90
-
"web_name": "Alexander-Arnold",
91
-
"UpcomingFixtures": [
92
-
{
93
-
"is_home": true,
94
-
"difficulty": 2
142
+
"is_captain": false
143
+
},
144
+
// ...
145
+
{
146
+
"element": {
147
+
"web_name": "Haaland"
95
148
},
96
-
{
97
-
"is_home": false,
98
-
"difficulty": 3
99
-
}
100
-
]
101
-
}
102
-
}
103
-
}
149
+
"is_captain": true
150
+
}
151
+
]
152
+
}
153
+
}
154
+
}
104
155
```
105
156
106
157
### Example query for a getting a specific gameweek's data
107
158
108
159
```graphql
109
-
{
110
-
gameweek(id: 15) {
160
+
queryGameweek6{
161
+
gameweek(id: 6) {
111
162
id
112
163
deadline_time
113
164
finished
@@ -127,36 +178,34 @@ Produces the following response:
127
178
{
128
179
"data": {
129
180
"gameweek": {
130
-
"id": 15,
131
-
"deadline_time": "2022-11-05T13:30:00Z",
181
+
"id": 6,
182
+
"deadline_time": "2023-09-23T12:30:00Z",
132
183
"finished": true,
133
-
"highest_score": 123,
134
-
"avg_points": 53,
184
+
"highest_score": 142,
185
+
"avg_points": 68,
135
186
"chip_plays": [
136
187
{
137
188
"chip_name": "bboost",
138
-
"num_played": 83164
189
+
"num_played": 78528
139
190
},
140
191
{
141
192
"chip_name": "freehit",
142
-
"num_played": 61596
193
+
"num_played": 60517
143
194
},
144
195
{
145
196
"chip_name": "wildcard",
146
-
"num_played": 150759
197
+
"num_played": 283333
147
198
},
148
199
{
149
200
"chip_name": "3xc",
150
-
"num_played": 44333
201
+
"num_played": 192955
151
202
}
152
203
]
153
204
}
154
205
}
155
206
}
156
207
```
157
208
158
-
For a full list of available queries and mutations, check out the schema explorer at [graphql-fpl-api.up.railway.app/graphql](https://graphql-fpl-api.up.railway.app/graphql)
159
-
160
209
## Contributing
161
210
162
211
I could use some help with writing tests. If you're interested in helping out, please feel free to open a pull request.
0 commit comments