Skip to content

Commit

Permalink
Fix issue with mutations, deprecate graphqlRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
langpavel committed Mar 16, 2017
1 parent 1a7e45b commit 08be6ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/core/createApolloClient/createApolloClient.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const client = new ApolloClient({
networkInterface: createNetworkInterface({
uri: '/graphql',
opts: {
// Additional fetch options like `credentials` or `headers`
credentials: 'include',
},
}),
Expand Down
36 changes: 27 additions & 9 deletions src/store/createHelpers.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import fetch from '../core/fetch';

const getGraphQLStringMessage = queryString => `Plain GraphQL string found.
You should precompile GraphQL queries by require them from *.graphql file.
Query: ${queryString.trimLeft().substring(0, 60)}`;
const graphqlRequestDeprecatedMessage = `\`graphqlRequest\` has been deprecated.
You should use Apollo: \`client.query({ query, variables...})\` or \`client.mutate()\`
Don't forget to enclose your query to gql\`…\` tag or import *.graphql file.
See docs at http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient\\.query`;

function createGraphqlRequest(apolloClient) {
return async function graphqlRequest(queryOrString, variables) {
return async function graphqlRequest(queryOrString, variables, options = {}) {
if (__DEV__) {
// eslint-disable-next-line no-console
console.error(graphqlRequestDeprecatedMessage);
}

const { skipCache } = options;
let query = queryOrString;
if (typeof queryOrString === 'string') {
if (__DEV__) {
// eslint-disable-next-line no-console
console.trace(getGraphQLStringMessage(queryOrString));
}
const gql = await require.ensure(['graphql-tag'], require => require('graphql-tag'), 'graphql-tag');
query = gql([queryOrString]);
}

if (skipCache) {
return apolloClient.networkInterface.query({ query, variables });
}

let isMutation = false;
if (query.definitions) {
isMutation = query.definitions.some(definition => definition && (definition.operation === 'mutation'));
}
if (isMutation) {
return apolloClient.mutate({ mutation: query, variables });
}
return apolloClient.query({ query, variables });
};
}
Expand Down Expand Up @@ -46,9 +61,12 @@ export default function createHelpers(config) {
const graphqlRequest = createGraphqlRequest(config.apolloClient);

return {
apolloClient: config.apolloClient,
client: config.apolloClient,
history: config.history,
fetch: fetchKnowingCookie,
// @deprecated('Use `client` instead')
apolloClient: config.apolloClient,
// @deprecated('Use `client.query()` or `client.mutate()` instead')
graphqlRequest,
};
}

1 comment on commit 08be6ff

@manhhailua
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I am expecting to see. Thank you, @langpavel!

Please sign in to comment.