Skip to content

Commit f625e8a

Browse files
committed
connect frontend with backend
1 parent 7a77ff0 commit f625e8a

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

app/app/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# Application definition
3232

3333
INSTALLED_APPS = [
34+
'corsheaders',
3435
'django.contrib.admin',
3536
'django.contrib.auth',
3637
'django.contrib.contenttypes',
@@ -49,6 +50,8 @@
4950
}
5051

5152
MIDDLEWARE = [
53+
'corsheaders.middleware.CorsMiddleware',
54+
'django.middleware.common.CommonMiddleware',
5255
'django.middleware.security.SecurityMiddleware',
5356
'django.contrib.sessions.middleware.SessionMiddleware',
5457
'django.middleware.common.CommonMiddleware',
@@ -59,6 +62,12 @@
5962
'django.contrib.auth.middleware.AuthenticationMiddleware',
6063
]
6164

65+
# CORS_ALLOWED_WHITELIST = (
66+
# 'localhost:3000'
67+
# )
68+
CORS_ALLOWED_ORIGINS = [
69+
'http://localhost:3000'
70+
]
6271
AUTHENTICATION_BACKENDS = [
6372
'graphql_jwt.backends.JSONWebTokenBackend',
6473
'django.contrib.auth.backends.ModelBackend',

react-books-client/src/Root.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
import React from "react";
22
import withRoot from "./withRoot";
3+
import {Query} from 'react-apollo';
4+
import {gql} from "apollo-boost";
35

4-
const Root = () => <div>Root</div>;
6+
const Root = () => (
7+
<Query query={GET_TRACKS_Q}>
8+
{({ data, loading, error })=> {
9+
if (loading) return <div>loading...</div>
10+
if (error) return <div>error</div>
11+
12+
return <div>{JSON.stringify(data)}</div>
13+
}}
14+
15+
</Query>
16+
);
17+
18+
const GET_TRACKS_Q = gql`
19+
{
20+
books {
21+
id
22+
title
23+
author
24+
description
25+
}
26+
}
27+
`
528

629
export default withRoot(Root);

react-books-client/src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ import ReactDOM from "react-dom";
33
import Root from "./Root";
44
import * as serviceWorker from "./serviceWorker";
55

6-
ReactDOM.render(<Root />, document.getElementById("root"));
6+
import { ApolloProvider } from 'react-apollo'
7+
import ApolloClient from 'apollo-boost'
8+
9+
10+
const client = new ApolloClient({
11+
uri:"http://localhost:8000/graphql/"
12+
})
13+
14+
ReactDOM.render(
15+
<ApolloProvider client={client}>
16+
<Root />,
17+
</ApolloProvider>,
18+
document.getElementById("root")
19+
);
720

821
// If you want your app to work offline and load faster, you can change
922
// unregister() to register() below. Note this comes with some pitfalls.

0 commit comments

Comments
 (0)