Shopify / shopify-app-js

MIT License
251 stars 99 forks source link

PrismaClientValidationError: Invalid `this.getSessionTable().upsert()` invocation #1200

Closed lwollnikowski closed 1 month ago

lwollnikowski commented 1 month ago

Issue summary

Before opening this issue, I have:

I develop an extension to Shopify theme. When I run shopify app dev, then I go to Shopify admin of the shop which has my extension installed and I choose one of the elements, in the loaded page / iframe I see prisma exception:

PrismaClientValidationError: 10:00:48 │ remix │ Invalid this.getSessionTable().upsert() invocation in 10:00:48 │ remix │ /home/lw/project/shopify-personalization-client-editor/node_modules/@shopify/shopify-app-session-storage-prisma/dist/cjs/prisma.js:29:42 10:00:48 │ remix │ 10:00:48 │ remix │ 26 await this.ready; 10:00:48 │ remix │ 27 const data = this.sessionToRow(session); 10:00:48 │ remix │ 28 try { 10:00:48 │ remix │ → 29 await this.getSessionTable().upsert({ 10:00:48 │ remix │ where: { 10:00:48 │ remix │ id: "offline_hapies-lukasz.myshopify.com" 10:00:48 │ remix │ }, 10:00:48 │ remix │ update: { 10:00:48 │ remix │ id: "offline_hapies-lukasz.myshopify.com", 10:00:48 │ remix │ shop: "hapies-lukasz.myshopify.com", 10:00:48 │ remix │ state: "", 10:00:48 │ remix │ isOnline: false, 10:00:48 │ remix │ scope: "read_metaobject_definitions,read_metaobjects,unauthenticated_read_metaobjects,write_content,write_products,write_themes", 10:00:48 │ remix │ expires: null, 10:00:48 │ remix │ accessToken: "shpua_bb5a12f5029f61944daa9b2e4028ec12", 10:00:48 │ remix │ userId: null, 10:00:48 │ remix │ firstName: null, 10:00:48 │ remix │ lastName: null, 10:00:48 │ remix │ email: null, 10:00:48 │ remix │ accountOwner: false, 10:00:48 │ remix │ locale: null, 10:00:48 │ remix │ collaborator: false, 10:00:48 │ remix │ emailVerified: false 10:00:48 │ remix │ }, 10:00:48 │ remix │ create: { 10:00:48 │ remix │ id: "offline_hapies-lukasz.myshopify.com", 10:00:48 │ remix │ shop: "hapies-lukasz.myshopify.com", 10:00:48 │ remix │ state: "", 10:00:48 │ remix │ isOnline: false, 10:00:48 │ remix │ scope: "read_metaobject_definitions,read_metaobjects,unauthenticated_read_metaobjects,write_content,write_products,write_themes", 10:00:48 │ remix │ expires: null, 10:00:48 │ remix │ accessToken: "shpua_bb5a12f5029f61944daa9b2e4028ec12", 10:00:48 │ remix │ userId: null, 10:00:48 │ remix │ firstName: null, 10:00:48 │ remix │ ~~~~~ 10:00:48 │ remix │ lastName: null, 10:00:48 │ remix │ email: null, 10:00:48 │ remix │ accountOwner: false, 10:00:48 │ remix │ locale: null, 10:00:48 │ remix │ collaborator: false, 10:00:48 │ remix │ emailVerified: false 10:00:48 │ remix │ } 10:00:48 │ remix │ }) 10:00:48 │ remix │ 10:00:48 │ remix │ Unknown argument firstName. Available options are marked with ?. 10:00:48 │ remix │ at Sn (/home/lw/project/shopify-personalization-client-editor/node_modules/@prisma/client/runtime/library.js:115:7526) 10:00:48 │ remix │ at _n.handleRequestError (/home/lw/project/shopify-personalization-client-editor/node_modules/@prisma/client/runtime/library.js:122:6570) 10:00:48 │ remix │ at _n.handleAndLogRequestError (/home/lw/project/shopify-personalization-client-editor/node_modules/@prisma/client/runtime/library.js:122:6235) 10:00:48 │ remix │ at _n.request (/home/lw/project/shopify-personalization-client-editor/node_modules/@prisma/client/runtime/library.js:122:5919) 10:00:48 │ remix │ at async l (/home/lw/project/shopify-personalization-client-editor/node_modules/@prisma/client/runtime/library.js:131:9116) 10:00:48 │ remix │ at async PrismaSessionStorage.storeSession (/home/lw/project/shopify-personalization-client-editor/node_modules/@shopify/shopify-app-session-storage-prisma/dist/cjs/prisma.js:29:13) 10:00:48 │ remix │ at async TokenExchangeStrategy.authenticate (file:///home/lw/project/shopify-personalization-client-editor/node_modules/@shopify/shopify-app-remix/dist/esm/server/authenticate/admin/strategies/token-exchange.mjs:33:13) 10:00:48 │ remix │ at async Object.authenticateAdmin [as admin] (file:///home/lw/project/shopify-personalization-client-editor/node_modules/@shopify/shopify-app-remix/dist/esm/server/authenticate/admin/authenticate.mjs:79:29) 10:00:48 │ remix │ at async loader (/home/lw/project/shopify-personalization-client-editor/app/routes/app.tsx:22:3) 10:00:48 │ remix │ at async Object.callRouteLoader (/home/lw/project/shopify-personalization-client-editor/node_modules/@remix-run/server-runtime/dist/data.js:66:16) { 10:00:48 │ remix │ clientVersion: '5.16.2' 10:00:48 │ remix │ }

Problems started some time after I updated dependencies, at first it was everything alright, problems started with no apparent reason. Maybe I just don't understand something in Shopify's ecosystem.

My Prisma database is default - one migration executed successfully, one table Session with appropriate columns:

id TEXT not null primary key, shop TEXT not null, state TEXT not null, isOnline BOOLEAN default false not null, scope TEXT, expires DATETIME, accessToken TEXT not null, userId BIGINT

Expected behavior

Authenticate request successfully, write request data to Session table without problems.

Actual behavior

Exception described above, Session table stays unchanged (no new data about shop which made request)

Steps to reproduce the problem

  1. I started the project by using shopify-remix-app-template when dependencies had corresponding latest versions:

    "@shopify/shopify-api": "10.0.0", "@shopify/shopify-app-remix": "2.8.2", "@shopify/shopify-app-session-storage-prisma": "4.0.2", "@prisma/client": "5.15.0", "prisma": "5.15.0",

  2. then updated to latest dependencies to incorporate changes from 2024-07 Shopify API release.

  3. Remove data in Session table in Prisma database, to force authentication request.

  4. When you want to see extension page in Shopify Admin, you see exception.

Debug logs

Output before exception:

09:51:30 │ remix │ [shopify-api/INFO] version 11.1.0, environment Remix 09:51:30 │ remix │ (node:41659) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time 09:51:30 │ remix │ (Use node --trace-warnings ... to show where the warning was created) 09:51:30 │ remix │ [shopify-app/INFO] Authenticating admin request 09:51:30 │ remix │ [shopify-app/DEBUG] Attempting to authenticate session token | {sessionToken: {"search":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczpcL1wvaGFwaWVzLWx1a2Fzei5teXNob3BpZnkuY29tXC9hZG1pbiIsImRlc3QiOiJodHRwczpcL1wvaGFwaWVzLWx1a2Fzei5teXNob3BpZnkuY29tIiwiYXVkIjoiZDFkMDcxNjIxMjJmYWQ5N2I2OGE2N2E1MW VkOWRkMzEiLCJzdWIiOiI4NjE0MTk2MDM2NiIsImV4cCI6MTcyMDc3MDc0NCwibmJmIjoxNzIwNzcwNjg0LCJpYXQiOjE3MjA3NzA2ODQsImp0aSI6ImJhMzFmZWUzLWUwMWUtNDMyMy1iNTU4LTIxODc3NmZlZTNmYSIsInNpZCI6ImFjNDU4MWNiLTMzZDEtNDY2My1hMDc0LWRjODc0NjJhNTFkNyIs InNpZyI6IjEyYjQ3MmQxZGIwZmRkZTU3YjAwOTU2ZjdkZGEwMGRlODczZTI2ZDY4YTI0YTg5OTU0NmVjMjMzODM0ZGJiOGYifQ.lZ2m3NN1890K9k9VjxbU489B7z5v3iNUIdh36Fdtrp4"}} 09:51:30 │ remix │ [shopify-app/DEBUG] Validating session token 09:51:30 │ remix │ [shopify-app/DEBUG] Session token is valid | {payload: {"iss":"https://hapies-lukasz.myshopify.com/admin","dest":"https://hapies-lukasz.myshopify.com","aud":"d1d07162122fad97b68a67a51ed9dd31","sub":"861 41960366","exp":1720770744,"nbf":1720770684,"iat":1720770684,"jti":"ba31fee3-e01e-4323-b558-218776fee3fa","sid":"ac4581cb-33d1-4663-a074-dc87462a51d7","sig":"12b472d1db0fdde57b00956f7dda00de873e26d68a24a899546ec233834dbb8f"}} 09:51:30 │ remix │ [shopify-app/DEBUG] Session token is valid | {shop: hapies-lukasz.myshopify.com, payload: [object Object]} 09:51:30 │ remix │ [shopify-app/DEBUG] Loading session from storage | {sessionId: offline_hapies-lukasz.myshopify.com} 09:51:30 │ remix │ [shopify-app/INFO] Authenticating admin request 09:51:30 │ remix │ [shopify-app/DEBUG] Attempting to authenticate session token | {sessionToken: {"search":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczpcL1wvaGFwaWVzLWx1a2Fzei5teXNob3BpZnkuY29tXC9hZG1pbiIsImRlc3QiOiJodHRwczpcL1wvaGFwaWVzLWx1a2Fzei5teXNob3BpZnkuY29tIiwiYXVkIjoiZDFkMDcxNjIxMjJmYWQ5N2I2OGE2N2E1MW VkOWRkMzEiLCJzdWIiOiI4NjE0MTk2MDM2NiIsImV4cCI6MTcyMDc3MDc0NCwibmJmIjoxNzIwNzcwNjg0LCJpYXQiOjE3MjA3NzA2ODQsImp0aSI6IjkwN2VjNDliLWRiMTAtNDIyYS1hMDY3LWQ2YjU3MDVhNzdiNiIsInNpZCI6ImFjNDU4MWNiLTMzZDEtNDY2My1hMDc0LWRjODc0NjJhNTFkNyIs InNpZyI6ImYyYjFiN2VmMWNiYjBkMDdhMzUyZGI3YjQ5NzU0ZGIxNzFkNmViZTZiYzk5MDI0NTA2MWRlYzJlMWIwM2M1MDkifQ.EXUQrt6d90teHBoeBMF_GyBYsLZqnccwOYjkm_el3_E"}} 09:51:30 │ remix │ [shopify-app/DEBUG] Validating session token 09:51:30 │ remix │ [shopify-app/DEBUG] Session token is valid | {payload: {"iss":"https://hapies-lukasz.myshopify.com/admin","dest":"https://hapies-lukasz.myshopify.com","aud":"d1d07162122fad97b68a67a51ed9dd31","sub":"861 41960366","exp":1720770744,"nbf":1720770684,"iat":1720770684,"jti":"907ec49b-db10-422a-a067-d6b5705a77b6","sid":"ac4581cb-33d1-4663-a074-dc87462a51d7","sig":"f2b1b7ef1cbb0d07a352db7b49754db171d6ebe6bc990245061dec2e1b03c509"}} 09:51:30 │ remix │ [shopify-app/DEBUG] Session token is valid | {shop: hapies-lukasz.myshopify.com, payload: [object Object]} 09:51:30 │ remix │ [shopify-app/DEBUG] Loading session from storage | {sessionId: offline_hapies-lukasz.myshopify.com} 09:51:30 │ remix │ [shopify-app/INFO] No valid session found 09:51:30 │ remix │ [shopify-app/INFO] Requesting offline access token 09:51:30 │ remix │ [shopify-app/INFO] No valid session found 09:51:30 │ remix │ [shopify-app/INFO] Requesting offline access token 09:51:30 │ remix │ [shopify-api/INFO] Creating new session | {shop: hapies-lukasz.myshopify.com, isOnline: false} 09:51:30 │ remix │ PrismaClientValidationError:

EDIT: As a test, I removed @shopify/shopify-api dependency, because I see that @shopify/shopify-app-remix forces correct dependency version. So, my package.json looked like this:

        "@shopify/shopify-app-remix": "3.0.2",
        "@shopify/shopify-app-session-storage-prisma": "4.0.5",

This set: shopify-api: "10.0.0" but Shopify CLI shows [shopify-api/INFO] version 11.1.0, environment Remix. Authentication successful, but no latest Shopify REST API (2024-07), the latest is 2024-04.

When I change shopify-app-session-storage-prisma to next version (5.0.0), this automatically sets shopify-api: 11.1.0. Authentication ends with an exception, latest Shopify REST API (2024-07) is accessible / is in node_modules of shopify-api

This is pretty messed up... Then what's the purpose of explicit @shopify/shopify-api dependency in shopify-app-remix-template when shopify-api version is determined by @shopify/shopify-app-remix?

paulomarg commented 1 month ago

Hi, thanks for reporting this. If you've recently updated to v5+ of the Prisma storage package, you'll need to tweak your schema since we changed it to store some user data as well.

Hope this helps!

github-actions[bot] commented 1 month ago

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.

lwollnikowski commented 1 month ago

Yeah I was blind, I didn't follow migration guide. After those steps, everything is working.

Thanks for dependency fixes @paulomarg, now everything is organized as I would expect.

  • Just one last thing: in the future, please be careful about posting tokens online as they can be used to make API requests. I suggest you uninstall and reinstall the app in the store contained in the logs here to reset the exposed tokens.

Thanks for the advice, already reinstalled.

Thanks for help one more time @paulomarg!