The GraphQL buildContext helper currently performs “on‑demand” account creation whenever an authenticated request arrives for a user that has no Accounts document yet
#6878
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Core idea: coalesce concurrent account creation for the same
userIdso that only onecreateAccountruns and all callers share the result.In
packages/api-core/src/util/buildContext.js:Map(accountCreationPromisesByUserId) keyed byuserIdto track in‑flight account creation promises.ensureAccountForUser(context, userId):context.auth.accountByUserId(context, userId)and returns immediately if an account exists (fast path).userId, it awaits and returns that same promise (no duplicatecreateAccount).context.mutations.createAccount(context.getInternalContext(), { emails, name, profile, userId })call.accountByUserIdand only logs if the account is still missing.finallyblock.null).account = await ensureAccountForUser(context, userId);In
packages/api-core/src/util/buildContext.test.js:coalesces concurrent account creation for the same user into a single createAccount callbuildContextcalls for the same newuserId:createdAccountvariable.accountByUserIdto returncreatedAccount.createAccountto:createdAccount = { _id: "CONCURRENT_ACCOUNT_ID", userId },getInternalContextstub on the test context:getInternalContext() { return { ...this, isInternalCall: true }; }createAccountis called exactly once.account,accountId, anduserId.Other potential solutions considered: