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

How to use api-codegen-preset in checkout UI extensions? #1081

Open
nboliver-ventureweb opened this issue Jun 19, 2024 · 11 comments
Open

How to use api-codegen-preset in checkout UI extensions? #1081

nboliver-ventureweb opened this issue Jun 19, 2024 · 11 comments

Comments

@nboliver-ventureweb
Copy link

Is it possible to use api-codegen-preset within a checkout UI extension? I tried setting it up according to the docs, but it didn't work.

Here is the .graphqlrc.ts:

import { ApiType, shopifyApiProject } from '@shopify/api-codegen-preset';

// See https://www.npmjs.com/package/@shopify/storefront-api-client
export default {
  schema: 'https://shopify.dev/storefront-graphql-direct-proxy/2024-04',
  documents: ['./src/**/*.{js,ts,jsx,tsx}'],
  projects: {
    default: shopifyApiProject({
      apiType: ApiType.Storefront,
      apiVersion: '2024-04',
      documents: ['./src/**/*.{js,ts,jsx,tsx}'],
      outputDir: './types',
    }),
  },
};

Here is the output:

✔ Parse Configuration
⚠ Generate outputs
  ❯ Generate to ./types/storefront-2024-04.schema.json
    ✔ Load GraphQL schemas
    ✖
      Unable to find any GraphQL type definitions for the following pointers:
      - ./src/**/*.{js,ts,jsx,tsx}
    ◼ Generate
  ❯ Generate to ./types/storefront.types.d.ts
    ✔ Load GraphQL schemas
    ✖
      Unable to find any GraphQL type definitions for the following pointers:
      - ./src/**/*.{js,ts,jsx,tsx}
    ◼ Generate
  ❯ Generate to ./types/storefront.generated.d.ts
    ✔ Load GraphQL schemas
    ✖
      Unable to find any GraphQL type definitions for the following pointers:
      - ./src/**/*.{js,ts,jsx,tsx}
    ◼ Generate

Am I missing something, or does it not work in a UI extension?
I double checked the paths were correct.
I confirmed the query was named:

      const { data } = await query(
        `#graphql 
        query getProductsOnOffer($handle: String!, $first: Int!) {
          collection(handle: $handle) {
            products(first: $first) {
              nodes {
                id
                title
                images(first:1){
                  nodes {
                    url
                  }
                }
                variants(first: 1) {
                  nodes {
                    id
                    price {
                      amount
                    }
                  }
                }
              }
            }
          }
        }`,
        {
          variables: { handle: 'automated-collection', first: 5 },
        },
      );
@matteodepalo
Copy link
Contributor

Hi @nboliver-ventureweb, what version of api-codegen-preset are you using?

Copy link
Contributor

We are closing this issue because we did not hear back regarding additional details we needed to resolve this issue. If the issue persists and you are able to provide the missing clarification we need, feel free to respond and reopen this issue.

We appreciate your understanding as we try to manage our number of open issues.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2024
@pinktonio
Copy link

@nboliver-ventureweb were you able to fix it?

@nboliver-ventureweb
Copy link
Author

@matteodepalo I'm not sure what version it was, it would have been the latest version out when this ticket was created. I have since removed it from the project because it wasn't working.

@pinktonio No, I haven't had a chance to revisit it, but would be keen to get it working!

@pinktonio
Copy link

I tried multiple versions. 1.0.1, 1.0.0 and 0.0.7 but couldn't get it to work. It couldn't find any of my #graphql comments on my named queries and mutations.

@matteodepalo matteodepalo reopened this Aug 1, 2024
@matteodepalo
Copy link
Contributor

@pinktonio
Copy link

@matteodepalo Yes I did.

I finally got it to work by using the /* GraphQL */ comment before my graphql code instead of #graphql inside the code.

@ujike-ryota
Copy link

ujike-ryota commented Sep 22, 2024

I had the same issue, but I was able to resolve it. I hope this helps others as well.

// .graphqlrc.ts
import {ApiType, shopifyApiProject} from '@shopify/api-codegen-preset';

export default {
  schema: 'https://shopify.dev/admin-graphql-direct-proxy',
  documents: ['src/graphql/*.ts'], // This doesn't work
  projects: {
    default: shopifyApiProject({
      apiType: ApiType.Admin,
      apiVersion: '2024-10',
      outputDir: './types',
      documents: ['src/graphql/*.ts'], // This configuration works
    }),
    documents: ['src/graphql/*.ts'], // This doesn't work
  },
};

This is my configuration file. One thing to be careful about is that you need to include documents within shopifyApiProject. If you don't, the path you reference will be overwritten, and **/*.{ts,tsx} will always be referenced.

// src/graphql/query.ts
export const SHOP_QUERY = /* GraphQL */ `
  query Shop {
    shop {
      name
    }
  }
`;

This is the query I wrote. There is another important thing to note here: you must use /* GraphQL */ as the magic comment instead of #graphql. While #graphql was referenced when I was working with Hydrogen, for some reason, @shopify/api-codegen-preset's pluckConfig doesn't seem to function correctly.

@matteodepalo
Copy link
Contributor

Glad you hear that you've solved this! I'm going to close this issue then.

@nboliver-ventureweb
Copy link
Author

@ujike-ryota Thanks for providing that code example. I'll try implementing the same way 🤞

@matteodepalo Given that the working configuration differs from what it listed in the @shopify/api-codegen-preset docs, perhaps it would be helpful to add some specific documentation to this repo in the hopes of making the setup easy for others to figure out?

@matteodepalo
Copy link
Contributor

@nboliver-ventureweb good point, I'll reopen this so that we can keep track of the change to the docs.

@matteodepalo matteodepalo reopened this Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants