GraphQL works with Mongoose

This commit is contained in:
Riccardo
2021-01-03 10:31:08 +01:00
parent c752e3ec80
commit b5129342e3
17 changed files with 3588 additions and 261 deletions

View File

@@ -1,29 +1,10 @@
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { setContext } from '@apollo/client/link/context';
import { AUTH_TOKEN } from './constants';
import { split } from '@apollo/client';
import { WebSocketLink } from '@apollo/client/link/ws';
import { getMainDefinition } from '@apollo/client/utilities';
// import AppointmentList from './components/AppointmentList';
// class App extends Component {
// render() {
// return <AppointmentList />;
// }
// }
// export default App;
// import React from 'react';
import ReactDOM from 'react-dom';
import './styles/index.css';
import './styles/tachyons.min.css'
import App from './components/App';
// // import * as serviceWorker from './serviceWorker';
// import * as serviceWorker from './serviceWorker';
// // 1
// 1
import {
ApolloProvider,
ApolloClient,
@@ -36,57 +17,112 @@ const httpLink = createHttpLink({
uri: 'http://localhost:4000'
});
// attach the auth_token to all requests to GraphQL server
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem(AUTH_TOKEN);
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
};
});
const wsLink = new WebSocketLink({
uri: `ws://localhost:4000/graphql`,
options: {
reconnect: true,
connectionParams: {
authToken: localStorage.getItem(AUTH_TOKEN)
}
}
});
const link = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return (
kind === 'OperationDefinition' &&
operation === 'subscription'
);
},
wsLink,
authLink.concat(httpLink)
);
// 3
const client = new ApolloClient({
link,
link: httpLink,
cache: new InMemoryCache()
});
// 4
ReactDOM.render(
<BrowserRouter>
<ApolloProvider client={client}>
<App />
</ApolloProvider>
</BrowserRouter>,
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
document.getElementById('root')
);
// serviceWorker.unregister();
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
// reportWebVitals();
// import React from 'react';
// import { BrowserRouter } from 'react-router-dom';
// import { setContext } from '@apollo/client/link/context';
// import { AUTH_TOKEN } from './constants';
// import { split } from '@apollo/client';
// import { WebSocketLink } from '@apollo/client/link/ws';
// import { getMainDefinition } from '@apollo/client/utilities';
// // import AppointmentList from './components/AppointmentList';
// // class App extends Component {
// // render() {
// // return <AppointmentList />;
// // }
// // }
// // export default App;
// // import React from 'react';
// import ReactDOM from 'react-dom';
// import './styles/index.css';
// import './styles/tachyons.min.css'
// import App from './components/App';
// // // import * as serviceWorker from './serviceWorker';
// // // 1
// import {
// ApolloProvider,
// ApolloClient,
// createHttpLink,
// InMemoryCache
// } from '@apollo/client';
// // 2
// const httpLink = createHttpLink({
// uri: 'http://localhost:4000'
// });
// // attach the auth_token to all requests to GraphQL server
// const authLink = setContext((_, { headers }) => {
// const token = localStorage.getItem(AUTH_TOKEN);
// return {
// headers: {
// ...headers,
// authorization: token ? `Bearer ${token}` : ''
// }
// };
// });
// const wsLink = new WebSocketLink({
// uri: `ws://localhost:4000/graphql`,
// options: {
// reconnect: true,
// connectionParams: {
// authToken: localStorage.getItem(AUTH_TOKEN)
// }
// }
// });
// const link = split(
// ({ query }) => {
// const { kind, operation } = getMainDefinition(query);
// return (
// kind === 'OperationDefinition' &&
// operation === 'subscription'
// );
// },
// wsLink,
// authLink.concat(httpLink)
// );
// // 3
// const client = new ApolloClient({
// link,
// cache: new InMemoryCache()
// });
// // 4
// ReactDOM.render(
// <BrowserRouter>
// <ApolloProvider client={client}>
// <App />
// </ApolloProvider>
// </BrowserRouter>,
// document.getElementById('root')
// );
// // serviceWorker.unregister();
// // If you want to start measuring performance in your app, pass a function
// // to log results (for example: reportWebVitals(console.log))
// // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
// // reportWebVitals();