Last active
December 1, 2018 15:54
-
-
Save stubailo/d0b9f169c759094e6d3108921898696f to your computer and use it in GitHub Desktop.
Call a GraphQL API with apollo-fetch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createApolloFetch } = require('apollo-fetch'); | |
const fetch = createApolloFetch({ | |
uri: 'https://1jzxrj179.lp.gql.zone/graphql', | |
}); | |
fetch({ | |
query: '{ posts { title }}', | |
}).then(res => { | |
console.log(res.data); | |
}); | |
// You can also easily pass variables for dynamic arguments | |
fetch({ | |
query: `query PostsForAuthor($id: Int!) { | |
author(id: $id) { | |
firstName | |
posts { | |
title | |
votes | |
} | |
} | |
}`, | |
variables: { id: 1 }, | |
}).then(res => { | |
console.log(res.data); | |
}); |
For @Ganesh1991 or anyone else that ends up here, this where I ended up for an auth header (and should work for modifying the request too):
After the
const fetch = createApolloFetch({
uri: 'https://1jzxrj179.lp.gql.zone/graphql',
});
add something like this:
fetch.use({ request, options }, next) => {
if (!options.headers) {
options.headers = {};
}
options.headers['authorization'] = 'your-auth-header-here';
next();
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can we send a header or authorization parameters?