apollo
apollo copied to clipboard
Apollo queries inside methods rather than Smart queries do not seem to use the default client httpEndpoint. Instead they seem to generate a relative path
I have set up the default client config with a full HTTPS URL as the httpEndpoint
(api.madefair.org). However, in the production build, queries in methods seem to use a relative path to /graphql
which result in a 404. The strange thing is, these methods work fine in the development build and also work fine with smart queries.
Here is one of my methods:
getEntry(slug) {
const apolloClient = this.$apolloProvider.clients.defaultClient; // This gets the default client
console.log(apolloClient); // this successfully returns the default client, so no error here
this.loading = true;
const variables = {
slug:slug,
site:this.$i18n.locale
}
return new Promise((resolve, reject) => {
apolloClient.query({
query:GET_ENTRY,
variables:variables
}).then(({data}) => {
this.entry = data.entry;
this.loading = false;
}).catch(err => {
this.error = "Sorry, we can't find that article";
console.log(err);
this.loading = false;
})
});
}
Here is my default client. config
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
import schema from './schema.json';
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData: schema
})
export default ({req, app}) => {
const token = process.env.GRAPHQL_TOKEN
// let headersVal = {
// 'Accept-Language': app.i18n.locale
// }
return {
httpEndpoint:'https://api.madefair.org/graphql',
getAuth: () => `Bearer ${token}`,
cache: new InMemoryCache({ fragmentMatcher }),
}
}
The error I get is as follows:
POST https://stage.madefair.org/graphql 404
384b416.js:1 Error: Network error: Unexpected token < in JSON at position 0
Notice that the URL it is attempting to query is not the URL in the http Endpoint. so it seems to have used a relative URL of just graphql
and appended it to the current client URL. where could it have created that from?
I am assuming the unexpected token error is just because its HTML returned from a 404, rather than json. So the only thing I need to solve here, is to ensure Apollo Client always uses the httpEndpoint provided in the default client config.
@toddpadwick I have the same problem, did you manage to fix this?
Hey @costas90 Sort of... but not in the way you might be hoping for: I ended up switching back to Smart Queries throughout as I found a way to trigger Smart Queries manually just like a method: Check this solution out. 'Onurkose's solution works quite well and so I've now found I don't ever need to use Apollo inside a method again. hope that helps!