Skip to content

Instantly share code, notes, and snippets.

@LucasCalazans
Last active March 17, 2022 21:37
Show Gist options
  • Save LucasCalazans/0b9b8ada91f928563efe82d3bc546cb9 to your computer and use it in GitHub Desktop.
Save LucasCalazans/0b9b8ada91f928563efe82d3bc546cb9 to your computer and use it in GitHub Desktop.
Use graphql with Magento 2
define([
'./graphql-client',
'text!Magento_Catalog/graphql/search-terms.graphql',
], function(graphql, searchTermsQuery) {
const query = graphql.gql(searchTermsQuery);
return function(term) {
return graphql.client
.query({
query: query,
variables: {
term: term,
},
})
.then(function(response) {
return response.data && response.data.result;
});
};
});
define(['apollo-boost', 'mage/url'], function(ApolloAmd, urlBuilder) {
const ApolloClient = ApolloAmd.ApolloClient;
const gql = ApolloAmd.gql;
const url = urlBuilder.build('graphql');
const client = new ApolloClient({ url: url });
return {
gql: gql,
client: client,
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment