Apollo and graphql working!

This commit is contained in:
John Shaver 2017-07-06 10:28:41 -07:00
parent 548e291c5c
commit 5fbecda9f6
4 changed files with 39 additions and 4 deletions

View File

@ -1,12 +1,13 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { getQueryParamByName } from './utils.js';
import ScoutOverview from './containers/ScoutOverview.js';
class App extends Component {
render() {
const scoutID = getQueryParamByName("id", this.props.slug);
return (
<div className="App">
Hello World!
<ScoutOverview scoutID={scoutID} />
</div>
);
}

View File

@ -0,0 +1,22 @@
import React from "react";
import { gql, graphql } from 'react-apollo';
function ScoutOverview({data, scoutID}) {
console.log(data);
return (
<div>
{data.getScout && data.getScout.displayName}
</div>
);
}
export default graphql(gql`
query GetScoutData($scoutID:ID!){
getScout(id: $scoutID){
displayName
}
}
`,{options : ({scoutID}) => {
console.log("Building var! scoutId: ", scoutID);
return {variables: {scoutID}};
}})(ScoutOverview);

View File

@ -15,9 +15,13 @@ const client = new ApolloClient({
})
});
const loc = window.location;
console.log("location: ", loc);
ReactDOM.render(
<ApolloProvider client={client}>
<App />
<App slug={loc.pathname} query={loc.search} />
</ApolloProvider>,
document.getElementById('root')
);

8
src/utils.js Normal file
View File

@ -0,0 +1,8 @@
export function getQueryParamByName(name, url) {
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}