Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation covering the response as struct context option #1264

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion docs/usage/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ response = client.query(query: query, variables: variables)

### Output
#### Success
If the request is successful these methods will all return a [`ShopifyAPI::Clients::HttpResponse`](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/clients/http_response.rb) object, which has the following methods:
If the request is successful these methods will all return a [`ShopifyAPI::Clients::HttpResponse`](https://github.com/Shopify/shopify-api-ruby/blob/main/lib/shopify_api/clients/http_response.rb) object, which has the following methods:
| Methods | Type | Notes |
|---------|------|-------|
| `code` |`Integer`| HTTP Response code, e.g. `200`|
Expand All @@ -137,6 +137,35 @@ This library also supports cursor-based pagination for GraphQL Admin API request

After making a request, the `next_page_info` and `prev_page_info` can be found on the response object and be used in the query param in other requests.

## Response as Struct
By default the response body is returned as a `Hash{String, Untyped}`. If you would like to return the response body as a `Struct`, you can pass `response_as_struct: true` to the `ShopifyAPI::Context.setup` method.
Then you can access the object with both dot and hash notation.

```ruby
ShopifyAPI::Context.setup(
api_key: ShopifyApp.configuration.api_key,
api_secret_key: ShopifyApp.configuration.secret,
...
response_as_struct: true
)

# Make a graphql query
response = client.query(
query: CREATE_PRODUCTS_MUTATION,
variables: {
input: {
title: random_title,
variants: [{ price: random_price }],
},
},
)
# Access result with dot notation
created_product2 = response.body.data.productCreate.product
# Access result with hash notation
created_product = response.body["data"]["productCreate"]["product"]

```

## Proxy a GraphQL Query

If you would like to give your front end the ability to make authenticated graphql queries to the Shopify Admin API, the `shopify_api` gem makes proxy-ing a graphql request easy! The gem provides a utility function which will accept the raw request body (a GraphQL query), the headers, and the cookies (optional). It will add authentication to the request, proxy it to the Shopify Admin API, and return a `ShopifyAPI::Clients::HttpResponse`. An example utilization of this in Rails is shown below:
Expand Down
Loading