Skip to content

Instantly share code, notes, and snippets.

@samundrak
Created November 15, 2018 12:19
Show Gist options
  • Save samundrak/efe0d273187f1f5f201f7317cb35cf0e to your computer and use it in GitHub Desktop.
Save samundrak/efe0d273187f1f5f201f7317cb35cf0e to your computer and use it in GitHub Desktop.
node-graphql-apollo-boost
import 'cross-fetch/polyfill';
import ApolloClient, { gql } from 'apollo-boost';
import 'dotenv/config';
const client = new ApolloClient({
uri: 'https://api.github.com/graphql',
request: operation => {
operation.setContext({
headers: {
authorization: `Bearer ${process.env.GITHUB_ACCESS_TOKEN}`,
},
});
},
});
const GET_REPOSITORIES_OF_ORGANIZATION = gql`
query($organization: String!) {
organization(login: $organization) {
name
url
repositories(first: 5) {
edges {
node {
name
url
}
}
}
}
}
`;
client
.query({
query: GET_REPOSITORIES_OF_ORGANIZATION,
variables: {
organization: 'the-road-to-learn-react',
},
})
.then(console.log);
const ADD_STAR = gql`
mutation AddStar($repositoryId: ID!) {
addStar(input: { starrableId: $repositoryId }) {
starrable {
id
viewerHasStarred
}
}
}
`;
client
.mutate({
mutation: ADD_STAR,
variables: {
repositoryId: 'MDEwOlJlcG9zaXRvcnk2MzM1MjkwNw==',
},
})
.then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment