Can someone explain how to post to a REST endpoint using APPBRIDGE?

Shopify Partner
14 1 0

The bulk of the embedded app is using GraphQL. but the app has a requirement to store some global app settings in the shop's admin Metafields.

 

I need to run the following REST endpoint to add, update:

POST /admin/metafields.json

Any help would be greatly appreciated!!

 

Thanks, Mike

0 Likes
Shopify Partner
132 4 12
0 Likes
Shopify Partner
14 1 0

Hi Thomas,

I already know that this is the endpoint to use. I do not know how to use it.

For example, I know how to do a query with GraphQL:

<Query query={GET_PRODUCTS_BY_ID} variables={{ ids: store.get('ids') }}>
{({ data, loading, error }) => {
if (loading) return <div>Loading…</div>;
if (error) return <div>{error.message}</div>;
console.log(data);
 
So how do I use the endpoint you noted? Can you point me to a code example?
 
Thanks!
Mike
0 Likes
Shopify Partner
132 4 12

What framework / programming language are you using?

0 Likes
Shopify Partner
14 1 0

Node.js React.

I based the app on this example project.

Here are the pertinent includes. 

const Koa = require("koa");
const next = require("next");
const { default: createShopifyAuth } = require("@shopify/koa-shopify-auth");
const dotenv = require("dotenv");
const { verifyRequest } = require("@shopify/koa-shopify-auth");
const session = require("koa-session");
const { default: graphQLProxy } = require("@shopify/koa-shopify-graphql-proxy");
import ApolloClient from 'apollo-boost';
import { ApolloProvider } from 'react-apollo';
'@shopify/polaris';
 
0 Likes
Shopify Partner
132 4 12

I mainly use php, but have used nodejs in the past, this should create a post request:

 

const request = require('request')
request.post('https://yourstore.myshopify.com/admin/metafields.json', {
json: {
"metafield": {
    "namespace": "inventory",
    "key": "warehouse",
    "value": 25,
    "value_type": "integer"
  }
  }
}, (error, res, body) => {
  if (error) {
    console.error(error)
    return
  }
  console.log(body)
});
0 Likes
Shopify Partner
14 1 0

Thanks Thomas!

 

I tried the code snippet but get the following error:

Failed to compile.
./components/ManageUserTypes.jsModule not found: Can't resolve 'request' in 'C:\Users\mkramer\Dropbox\Shopify\CaseSizeSelector App\Node.jsDevelopment\src\case-size-selector\components'

 

Any suggestions where to find request?

0 Likes
Shopify Partner
132 4 12
Cd into your project dir and run:
npm install request

Or install it globally run:
npm install request -g

And try again :)
0 Likes
Highlighted
Shopify Partner
14 1 0

I installed 'request' as suggested, but ran into  a compile error looking for 'fs' which I then installed. This caused the same compile error after installing 'fs', so I uninstalled both components, as this seems to be a dead end.

 

In the Shopify documentation, it says that APPBRIDGE (which is part of the installed stack '@shopify') is backwards compatible with EASDK. I would imagine that request would be in there somewhere, but so far I can't find it...

 

Any further suggestions would be greatly appreciated!

0 Likes