-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,018 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
"esnext", | ||
"idps", | ||
"gamecenter", | ||
"GraphQLID", | ||
"gsutil", | ||
"jsonb", | ||
"knexfile", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/* SPDX-License-Identifier: MIT */ | ||
|
||
export * from "./auth"; | ||
export * from "./updateUser"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */ | ||
/* SPDX-License-Identifier: MIT */ | ||
|
||
import { | ||
GraphQLBoolean, | ||
GraphQLFieldConfig, | ||
GraphQLID, | ||
GraphQLInputObjectType, | ||
GraphQLNonNull, | ||
GraphQLObjectType, | ||
GraphQLString, | ||
} from "graphql"; | ||
import db, { User } from "../../db"; | ||
import { Context } from "../context"; | ||
import { UserType } from "../types"; | ||
import { fromGlobalId, validate, ValidationError } from "../utils"; | ||
|
||
type UpdateUserInput = { | ||
id: string; | ||
username?: string | null; | ||
email?: string | null; | ||
name?: string | null; | ||
picture?: string | null; | ||
timeZone?: string | null; | ||
locale?: string | null; | ||
}; | ||
|
||
/** | ||
* @example | ||
* mutation M { | ||
* updateUser(input: { id: "xxx", email: "new@email.com" }, dryRun: false) { | ||
* user { | ||
* id | ||
* } | ||
* } | ||
* } | ||
*/ | ||
export const updateUser: GraphQLFieldConfig<unknown, Context> = { | ||
description: "Updates the user account.", | ||
|
||
type: new GraphQLObjectType({ | ||
name: "UpdateUserPayload", | ||
fields: { | ||
user: { type: UserType }, | ||
}, | ||
}), | ||
|
||
args: { | ||
input: { | ||
type: new GraphQLInputObjectType({ | ||
name: "UpdateUserInput", | ||
fields: { | ||
id: { type: new GraphQLNonNull(GraphQLID) }, | ||
username: { type: GraphQLString }, | ||
email: { type: GraphQLString }, | ||
name: { type: GraphQLString }, | ||
picture: { type: GraphQLString }, | ||
timeZone: { type: GraphQLString }, | ||
locale: { type: GraphQLString }, | ||
}, | ||
}), | ||
}, | ||
dryRun: { type: new GraphQLNonNull(GraphQLBoolean), defaultValue: false }, | ||
}, | ||
|
||
async resolve(self, args, ctx) { | ||
const input = args.input as UpdateUserInput; | ||
const dryRun = args.dryRun as boolean; | ||
const id = fromGlobalId(input.id, "User"); | ||
|
||
// Check permissions | ||
ctx.ensureAuthorized((user) => user.id === id || user.admin); | ||
|
||
// Validate and sanitize user input | ||
const [data, errors] = validate(input, (value) => ({ | ||
username: value("username").isUsername(), | ||
email: value("email").isLength({ max: 100 }).isEmail(), | ||
name: value("name").isLength({ min: 2, max: 100 }), | ||
picture: value("picture").isLength({ max: 100 }), | ||
time_zone: value("timeZone").isLength({ max: 50 }), | ||
locale: value("locale").isLength({ max: 10 }), | ||
})); | ||
|
||
// Once a new username is provided and it passes the initial | ||
// validation, check if it's not used by any other user. | ||
if (input.username && !("username" in errors)) { | ||
const exists = await db | ||
.table<User>("user") | ||
.where({ username: input.username }) | ||
.whereNot({ id }) | ||
.first("id") | ||
.then((x) => Boolean(x)); | ||
if (exists) { | ||
errors.username = ["Username is not available."]; | ||
} | ||
} | ||
|
||
if (Object.keys(errors).length > 0) { | ||
throw new ValidationError(errors); | ||
} | ||
|
||
if (Object.keys(data).length === 0) { | ||
throw new ValidationError({ _: ["The input values cannot be empty."] }); | ||
} | ||
|
||
if (dryRun) { | ||
return { user: await ctx.userById.load(id) }; | ||
} | ||
|
||
const [user] = await db | ||
.table("user") | ||
.where({ id }) | ||
.update({ ...data, updated_at: db.fn.now() }) | ||
.returning("*"); | ||
|
||
return { user }; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/* SPDX-License-Identifier: MIT */ | ||
|
||
export * from "./map"; | ||
export * from "./relay"; | ||
export * from "./validator"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */ | ||
/* SPDX-License-Identifier: MIT */ | ||
|
||
import { mapTo, mapToMany, mapToManyValues, mapToValues } from "./map"; | ||
|
||
test("mapTo()", () => { | ||
const result = mapTo( | ||
[ | ||
{ id: 2, name: "b" }, | ||
{ id: 1, name: "a" }, | ||
], | ||
[1, 2], | ||
(x) => x.id | ||
); | ||
expect(result).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"id": 1, | ||
"name": "a", | ||
}, | ||
Object { | ||
"id": 2, | ||
"name": "b", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test("mapToMany()", () => { | ||
const result = mapToMany( | ||
[ | ||
{ id: 2, name: "b" }, | ||
{ id: 1, name: "a" }, | ||
{ id: 1, name: "c" }, | ||
], | ||
[1, 2], | ||
(x) => x.id | ||
); | ||
expect(result).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
Object { | ||
"id": 1, | ||
"name": "a", | ||
}, | ||
Object { | ||
"id": 1, | ||
"name": "c", | ||
}, | ||
], | ||
Array [ | ||
Object { | ||
"id": 2, | ||
"name": "b", | ||
}, | ||
], | ||
] | ||
`); | ||
}); | ||
|
||
test("mapToValues()", () => { | ||
const result = mapToValues( | ||
[ | ||
{ id: 2, name: "b" }, | ||
{ id: 1, name: "a" }, | ||
{ id: 3, name: "c" }, | ||
], | ||
[1, 2, 3, 4], | ||
(x) => x.id, | ||
(x) => x?.name || null | ||
); | ||
expect(result).toMatchInlineSnapshot(` | ||
Array [ | ||
"a", | ||
"b", | ||
"c", | ||
null, | ||
] | ||
`); | ||
}); | ||
|
||
test("mapToManyValues()", () => { | ||
const result = mapToManyValues( | ||
[ | ||
{ id: 2, name: "b" }, | ||
{ id: 2, name: "c" }, | ||
{ id: 1, name: "a" }, | ||
], | ||
[1, 2], | ||
(x) => x.id, | ||
(x) => x?.name || null | ||
); | ||
expect(result).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
"a", | ||
], | ||
Array [ | ||
"b", | ||
"c", | ||
], | ||
] | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */ | ||
/* SPDX-License-Identifier: MIT */ | ||
|
||
// These helper functions are intended to be used in data loaders for mapping | ||
// entity keys to entity values (db records). See `../context.ts`. | ||
// https://github.com/graphql/dataloader | ||
|
||
export function mapTo<R, K>( | ||
records: ReadonlyArray<R>, | ||
keys: ReadonlyArray<K>, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */ | ||
/* SPDX-License-Identifier: MIT */ | ||
|
||
import { fromGlobalId as parse } from "graphql-relay"; | ||
|
||
/** | ||
* Converts (Relay) global ID into a raw database ID. | ||
*/ | ||
export function fromGlobalId(globalId: string, expectedType: string): string { | ||
const { id, type } = parse(globalId); | ||
|
||
if (expectedType && type !== expectedType) { | ||
throw new Error( | ||
`Expected an ID of type '${expectedType}' but got '${type}'.` | ||
); | ||
} | ||
|
||
return id; | ||
} |
Oops, something went wrong.