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

apollo client導入 #10

Closed
hiro-lapis opened this issue Jul 30, 2023 · 9 comments
Closed

apollo client導入 #10

hiro-lapis opened this issue Jul 30, 2023 · 9 comments
Labels
frontend Extra attention is needed

Comments

@hiro-lapis
Copy link
Owner

hiro-lapis commented Jul 30, 2023

nuxt用のapolloがあるので、それを使用する

同梱パッケージ
graphql-tag : raphQL クエリとスキーマ定義を文字列として記述し、それを JavaScript オブジェクトに変換するためのライブラリ

やること

golang

  • CORS設定をした上でGraphqlリゾルバへ繋ぐ設定をする

nuxt

  • apllo導入
  • apllo設定
  • サーバのエントリポイントへリクエスト送信

めも
・コンテナ起動時に実行されるのはappのルートディレクトリにあるmain.go
こいつを実行させたままでいいから、go run cmd/server.goでGraphqlサーバを起動する必要がある

・コーディング後は再度go run ...する必要あり
*追々自動実行するのをserver.goに移すのもあり

@hiro-lapis hiro-lapis added the frontend Extra attention is needed label Jul 30, 2023
@hiro-lapis
Copy link
Owner Author

hiro-lapis commented Aug 10, 2023

Uncaught SyntaxError: Unexpected string (at instanceOf.mjs?v=b224f08b:12:36)というエラーが出る。
現状、graphqlのバージョンを16.6.0に下げるしかない

https://stackoverflow.com/questions/76527409/react-vitejs-unexpected-string

https://stackoverflow.com/questions/76530650/node-modules-broken-when-using-vite-react-apollo-client

@hiro-lapis
Copy link
Owner Author

hiro-lapis commented Aug 10, 2023

https://stackoverflow.com/questions/76530650/node-modules-broken-when-using-vite-react-apollo-client
Edit: this should be fixed with 16.7.1 now.
ということで、7/23のアプデで修正されたみたい

@hiro-lapis
Copy link
Owner Author

https://pnpm.io/ja/package_json#pnpmoverrides

overrides設定でインストールするバージョンを指定できるみたいなので、これで行く

@hiro-lapis
Copy link
Owner Author

直ってないやん😂

image

@hiro-lapis
Copy link
Owner Author

↓のISSUE が解消したらバージョン上げる
graphql/graphql-js#2317

@hiro-lapis
Copy link
Owner Author

とりあえずエラーは解消🙌
image

@hiro-lapis
Copy link
Owner Author

キターーーーーーーーーーーー!!!

image

@hiro-lapis
Copy link
Owner Author

画面の初期表示でAPIリクエストを飛ばしたい時はuseQuery()を使用

画面の操作に応じてリクエストを実行する場合はuseAsyncQuery()を使う
*注意

const asyncFetchTodo = async () => {
  // for grqphql, useAsyncQuery, (REST API =useFetch)
  // passing generic type to useAsyncQuery, it will be inferred
  const { data } = await useAsyncQuery<Todos>(query, {})
  // APIで取得したdataもvalueに格納されている
  datas.value = data.value?.todos as unknown as Todo[]
  console.log('fetched!!')
  console.log(datas.value)
  console.log('data.value')
  console.log(data.value)
}

上ののように、画面の初期表示にuseAsyncQueryを使うとawaitで待ってもレスポンスが返ってくるまでに変数への代入処理が実行されてしまう。
SSRで画面初期表示でデータ取得したいときはuseQueryを使い、result取得を待って代入処理するようにする。

@hiro-lapis
Copy link
Owner Author

nuxt.config.tsでSSRをONにしているため、
・初期実行関数でクライアントサイド依存のオブジェクトを使わない
・windowオブジェクトなどブラウザonlyのオブジェクトはonMounted()で実行
・もしくは画面操作に応じた関数でのみ実行
するように注意

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant