|
2 | 2 | # Django ledger Graphql Api |
3 | 3 |
|
4 | 4 | ## Usage and installation |
| 5 | +Install Required Packages |
5 | 6 |
|
6 | | - |
7 | | - |
8 | | - |
9 | | - |
| 7 | +First, install the necessary packages: |
10 | 8 | ``` |
11 | 9 | pip install graphene-django |
12 | 10 | ``` |
13 | 11 | ``` |
14 | | -pip install django-graphql-auth |
| 12 | +pip install django-oauth-toolkit |
| 13 | +``` |
| 14 | +Enable Graphql by navigating to ./django_ledger/settings.py |
| 15 | +``` |
| 16 | + DJANGO_LEDGER_GRAPHQL_SUPPORT_ENABLED = True |
15 | 17 | ``` |
16 | 18 | Makemigrations |
17 | 19 | ``` |
18 | 20 | python manage.py makemigrations |
19 | 21 | python manage.py migrate |
20 | | -``` |
21 | | -Runserver |
| 22 | +``` |
| 23 | +Start the Django development server: |
22 | 24 | ``` |
23 | 25 | python manage.py runserver |
24 | | -``` |
25 | | -make sure you are loged in the main application go to the admin |
26 | | -site http://127.0.0.1:8000/admin/django_ledger/entitymodel/ choose your |
27 | | -entity and get your slug name from the entity choosen or |
28 | | - |
29 | | -Open new tab and navigate to |
30 | | -``` |
31 | | -http://127.0.0.1:8000/graphql/ |
32 | | -``` |
33 | | -paste this to the console and run the query |
34 | | - |
35 | 26 | ``` |
36 | | -{ |
37 | | - allEntityList{ |
38 | | - slug |
39 | | - name |
40 | | - } |
41 | | -``` |
42 | | -this will return the current user logged in slug and name, use the slug for other queries (Slugname:String!) |
43 | | -## sample graphql Query |
44 | | -paste this at the graphql console and run the query |
| 27 | +on the admin site ensure to add an Application with the fields |
45 | 28 | ``` |
46 | | -{ |
47 | | - allEntityList{ |
48 | | - edges{ |
49 | | - node{ |
50 | | - slug |
51 | | - name |
52 | | - } |
53 | | - } |
54 | | -} |
55 | | -``` |
56 | | -this will return the current user logged in slug and name, use the slug for other queries (Slugname:String!) |
57 | | -## sample graphql Query |
58 | | -paste this at the graphql console and run the query |
59 | | -``` |
60 | | -allCustomers(slugName:"jusper-onderi-ondieki-db23x1y8"){ |
61 | | - edges { |
62 | | - node { |
63 | | - customerName |
64 | | - city |
65 | | - state |
66 | | - active |
67 | | - |
68 | | - } |
69 | | - } |
70 | | -} |
| 29 | +client type: confidential |
| 30 | +Authorization grant type: Resource owner password-based |
71 | 31 | ``` |
72 | 32 |
|
73 | | -# Query results |
74 | | -``` |
75 | | -"allCustomers": { |
76 | | - "edges": [ |
77 | | - { |
78 | | - "node": { |
79 | | - "customerName": "booka", |
80 | | - "city": "kenya", |
81 | | - "state": "huj", |
82 | | - "active": true |
83 | | - } |
84 | | - }, |
85 | | - { |
86 | | - "node": { |
87 | | - "customerName": "stats", |
88 | | - "city": "kenya", |
89 | | - "state": "huj", |
90 | | - "active": true |
91 | | - } |
92 | | - }, |
93 | | - { |
94 | | - "node": { |
95 | | - "customerName": "Brooke Weaver", |
96 | | - "city": "South Michelleborough", |
97 | | - "state": "WA", |
98 | | - "active": true |
99 | | - } |
100 | | - }, |
101 | | - { |
102 | | - "node": { |
103 | | - "customerName": "Tamara Wilson", |
104 | | - "city": "Castilloport", |
105 | | - "state": "WV", |
106 | | - "active": true |
107 | | - } |
108 | | - }, |
| 33 | +Use Postman or Thunder Client to make a POST request to obtain an access token. Send a request to the following URL: |
109 | 34 |
|
110 | | - } |
111 | | - ] |
112 | | - } |
113 | | - } |
114 | | -} |
| 35 | +``` |
115 | 36 |
|
| 37 | +http://127.0.0.1:8000/api/v1/o/token/ |
116 | 38 | ``` |
117 | | -## Using graphql-auth |
118 | | -Register user |
| 39 | +Include the following JSON body in your request: |
119 | 40 | ``` |
120 | | -mutation { |
121 | | - register( |
122 | | - |
123 | | - username: "new_user", |
124 | | - password1: "dave123456", |
125 | | - password2: "dave123456", |
126 | | - ) { |
127 | | - success, |
128 | | - errors, |
129 | | - token, |
130 | | -
|
131 | | - } |
| 41 | +{ |
| 42 | + "username": "eric", |
| 43 | + "password": "eric", |
| 44 | + "client_id": "cXFFgnvWhWiZDofGkwiwUBdfuxfNnLsmBtAVsVXv", |
| 45 | + "client_secret": "dave101", |
| 46 | + "grant_type": "password" |
132 | 47 | } |
133 | 48 | ``` |
134 | | -Returns a token and a succes message |
| 49 | +If the request is successful, you will receive a response like this: |
135 | 50 | ``` |
136 | 51 | { |
137 | | - "data": { |
138 | | - "register": { |
139 | | - "success": true, |
140 | | - "errors": null, |
141 | | - "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6Im5ld191c2VyIiwiZXhwIjoxNjQ5NzY2Nzc4LCJvcmlnSWF0IjoxNjQ5NzY2NDc4fQ.cHaPq8CjQy60ifUawR4Pnyyu_E_SCU2J6CapBK0P8P4" |
142 | | - } |
143 | | - } |
| 52 | + "access_token": "YPMh29n648qpahgtOjrDHMDy5bt81e", |
| 53 | + "expires_in": 36000, |
| 54 | + "token_type": "Bearer", |
| 55 | + "scope": "read write", |
| 56 | + "refresh_token": "huBiNKw9IhtuYPKVNR9i4WnQesMqEl" |
144 | 57 | } |
145 | 58 | ``` |
146 | | -for more detail usage visit the documentation |
147 | | -https://django-graphql-auth.readthedocs.io/en/latest/quickstart/ |
| 59 | +Now you can use the access token to authenticate your GraphQL requests. |
| 60 | + |
0 commit comments