Skip to content

Commit

Permalink
setStoragesを使ってリクエスト数削減
Browse files Browse the repository at this point in the history
  • Loading branch information
kgtkr committed Jan 16, 2024
1 parent 7832e8c commit b053bc4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 53 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"

services:
server:
image: ghcr.io/kgtkr/anontown-server:20240114090236576-8130ec4
image: ghcr.io/kgtkr/anontown-server:20240116045245741-be819fe
ports:
- "8080:8080"
environment:
Expand Down
3 changes: 2 additions & 1 deletion packages/client/codegen.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
overwrite: true
schema: "./schema.json"
documents: ["./src/**/*.gql", "./src/**/*.ts", "./src/**/*.tsx"]
documents:
["./src/**/*.gql", "./src/**/*.ts", "./src/**/*.tsx", "!./src/generated/**/*"]
generates:
src/generated/graphql/:
preset: "client"
Expand Down
2 changes: 1 addition & 1 deletion packages/client/schema.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ const StorageQueryDocument = graphql(/* GraphQL */ `
}
`);

const SetStorageMutationDocument = graphql(/* GraphQL */ `
mutation StorageCollectionHooks_setStorageMutation(
$key: String!
$value: String!
const SetStoragesMutationDocument = graphql(/* GraphQL */ `
mutation StorageCollectionHooks_setStoragesMutation(
$input: SetStoragesInput!
) {
setStorage(key: $key, value: $value) {
setStorages(input: $input) {
__typename
}
}
Expand Down Expand Up @@ -144,15 +143,16 @@ function refetchQueries(key: string): InternalRefetchQueriesInclude {
}

export function useSetStorage<T>(storageCollection: StorageCollection<T>) {
const [mutation, result] = useMutation(SetStorageMutationDocument);
const [mutation, result] = useMutation(SetStoragesMutationDocument);

return [
async (value: T) => {
const key = getKey(storageCollection, value);
await mutation({
variables: {
key,
value: JSON.stringify(value),
input: {
storages: [{ key, value: JSON.stringify(value) }],
},
},
refetchQueries: refetchQueries(key),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,24 @@ export async function convert9To10(
value: JSON.stringify(x),
})),
];
await Promise.all(
keyValues.map(async ({ key, value }) => {
const result = await gqlClient.mutate<
GA.SetStorageMutation,
GA.SetStorageMutationVariables
>({
mutation: GA.SetStorageDocument,
variables: {
key,
value,
},
context: {
headers: createHeaders(token.id, token.key),
},
});
if (result.errors !== undefined && result.errors.length > 0) {
throw new Error(result.errors[0].message);
}
})
);

const result = await gqlClient.mutate<
GA.SetStoragesMutation,
GA.SetStoragesMutationVariables
>({
mutation: GA.SetStoragesDocument,
variables: {
input: {
storages: keyValues,
},
},
context: {
headers: createHeaders(token.id, token.key),
},
});
if (result.errors !== undefined && result.errors.length > 0) {
throw new Error(result.errors[0].message);
}

return {
ver: "10",
Expand Down
24 changes: 13 additions & 11 deletions packages/client/src/domains/entities/storage/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ export async function migration(token: GA.TokenMasterFragment) {
sto !== undefined ? JSON.parse(sto.value) : Sto.initStorage
);

await gqlClient.mutate<GA.SetStorageMutation, GA.SetStorageMutationVariables>(
{
mutation: GA.SetStorageDocument,
variables: {
key: Sto.verArray[0],
value: JSON.stringify(storage),
},
context: {
headers: createHeaders(token.id, token.key),
await gqlClient.mutate<
GA.SetStoragesMutation,
GA.SetStoragesMutationVariables
>({
mutation: GA.SetStoragesDocument,
variables: {
input: {
storages: [{ key: Sto.verArray[0], value: JSON.stringify(storage) }],
},
}
);
},
context: {
headers: createHeaders(token.id, token.key),
},
});
}
24 changes: 13 additions & 11 deletions packages/client/src/gql/storage.gql
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
fragment storage on Storage{
key
value
fragment storage on Storage {
key
value
}

query findStorages($query:StorageQuery!){
storages(query:$query){
...storage
}
query findStorages($query: StorageQuery!) {
storages(query: $query) {
...storage
}
}

mutation setStorage($key:String!,$value:String!){
setStorage(key:$key,value:$value){
...storage
mutation setStorages($input: SetStoragesInput!) {
setStorages(input: $input) {
storages {
...storage
}
}
}
}

0 comments on commit b053bc4

Please sign in to comment.