Install the Apollo Boost for requireJS
Last active
March 17, 2022 21:37
-
-
Save LucasCalazans/0b9b8ada91f928563efe82d3bc546cb9 to your computer and use it in GitHub Desktop.
Use graphql with Magento 2
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
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; | |
}); | |
}; | |
}); |
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
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