GraphQL call from Nodejs CLI app

Tiber
Excursionist
18 3 1

I am trying to figure out how to make GraphQL request from a Nodejs, React App created with the Shopify App CLI. I see the GraphQL requests in "server->handlers->mutations" and the client.js in "server->handlers" setting up ApolloClient. But I am not sure how to make calls to the GraphQl requests in the mutations folder from pages->index.js page?

//server->handlers->Client.js
import ApolloClient from "apollo-boost";

export const createClient = (shop, accessToken) => {
  return new ApolloClient({
    uri: `https://${shop}/admin/api/2021-01/graphql.json`,
    request: operation => {
      operation.setContext({
        headers: {
          "X-Shopify-Access-Token": accessToken,
          "User-Agent": `shopify-app-node ${
            process.env.npm_package_version
          } | Shopify App CLI`
        }
      });
    }
  });
};

 

//server->handlers->index.js
import { createClient } from "./client";
import { getOneTimeUrl } from "./mutations/get-one-time-url";
import { getSubscriptionUrl } from "./mutations/get-subscription-url";
import { registerWebhooks } from "./register-webhooks";

export { createClient, getOneTimeUrl, getSubscriptionUrl, registerWebhooks };

And in _app.js

const client = new ApolloClient({
  fetch: fetch,
  fetchOptions: {
    credentials: "include",
  },
});
class MyApp extends App {
  render() {
    const { Component, pageProps, shopOrigin } = this.props;
    return (
      <AppProvider i18n={translations}>
        <Provider
          config={{
            apiKey: API_KEY,
            shopOrigin: shopOrigin,
            forceRedirect: true,
          }}
        >
          <ApolloProvider client={client}>
            <Component {...pageProps} />
          </ApolloProvider>
        </Provider>
      </AppProvider>
    );
  }
}

 

0 Likes