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

fix(dgraph): id generation overlap #10719

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

korzhanov
Copy link

@korzhanov korzhanov commented Apr 24, 2024

☕️ Reasoning

Dgraph will generate the ID by itself. In Dgraph, every node has a unique 64-bit identifier that you can expose in GraphQL using the ID type. An ID is auto-generated, immutable and never reused. Each type can have at most one ID field.

The ID type works great when you need to use an identifier on nodes and don’t need to set that identifier externally
DGraph type docs

Since next-auth.js or auth.js creates a new user id, this causes an error and does not save the entry to dgraph

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

📌 Resources

Dgraph will generate the ID by itself. In Dgraph, every node has a unique 64-bit identifier that you can expose in GraphQL using the ID type. An ID is auto-generated, immutable and never reused. Each type can have at most one ID field.

The ID type works great when you need to use an identifier on nodes and don’t need to set that identifier externally
@korzhanov korzhanov requested a review from ndom91 as a code owner April 24, 2024 21:23
Copy link

vercel bot commented Apr 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ❌ Failed (Inspect) Apr 24, 2024 9:24pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Apr 24, 2024 9:24pm

@github-actions github-actions bot added adapters Changes related to the core code concerning database adapters dgraph @auth/dgraph-adapter labels Apr 24, 2024
@ndom91 ndom91 changed the title Fix createUser at DGraph Adapter fix(dgraph): id generation overlap Apr 28, 2024
@ndom91
Copy link
Member

ndom91 commented Apr 28, 2024

Just curious why the current tests pass and why this isn't necessary for createSession or createVerificationToken, for example, then? 🤔

@korzhanov
Copy link
Author

@ndom91 Sorry, I didn't see your comment in this thread without mention.

Just curious why the current tests pass and why this isn't necessary for createSession or createVerificationToken, for example, then? 🤔

The test uses a predefined id value id: () => "0x0a0a00a00" that falls under the DGraph requirements.

runBasicTests({
adapter: DgraphAdapter(params),
db: {
id: () => "0x0a0a00a00",
async disconnect() {
await c.run(/* GraphQL */ `
mutation {
deleteUser(filter: {}) {
numUids
}
deleteVerificationToken(filter: {}) {
numUids
}
deleteSession(filter: {}) {
numUids
}
deleteAccount(filter: {}) {
numUids
}
}
`)
},

And in reality, the random UUID is invalid, it doesn't match the DGraph UID conditions.

Situation similar to the FaunaDB adapter

runBasicTests({
adapter: FaunaAdapter(client),
db: {
// UUID is not a valid ID in Fauna (see https://docs.fauna.com/fauna/current/reference/fql_reference/types#id)
id: () => String(Math.floor(Math.random() * 10 ** 18)),
user: async (id) => {
const response = await client.query<FaunaUser>(fql`User.byId(${id})`)
if (response.data instanceof NullDocument) return null
return format.from(response.data)
},

And I think maybe createSession and createVerificationToken don't use external generated ids, only internal ones

@adriangalilea
Copy link

I spent several hours trying to get the dgraph adapter to work on next-auth only to realize that: DgraphClientError: unknown field really meant it's the overlapping of ID's, and finally arrived here...

I tried discussing on the forum but seems dead.

What's the status of this PR?

I've noticed Dgraph adapter and Dgraph adapter docs are outdated in several ways:

Dgraph now allows HS512 so this is wrong now on the dgraph adapter docs.

Also I faced some edge runtime issues when trying to call auth() from the middleware.

Can we make this PR cover all graph adapter fixes so it's easier to track? @korzhanov

@korzhanov
Copy link
Author

korzhanov commented Jun 26, 2024

@adriangalilea same story bro.
I spent a very long time trying to figure out what the problem was. I tried the @auth/dgraph-adapter, but it didn't work at all. I had to install @next-auth/dgraph-adapter
It worked, but with errors.
I ended up spending a few hours to understand how the adapter works, fixed one line as described in this PR. And instead of a dependency in package.json, I copied the code separately and plugged it in as a component until the official package update comes out. Some time :)

I would like this PR to at least allow correct user creation. I don't have much hope for the rest - I'll have to find solutions on my own :(

Maybe @ndom91, @beynar or @balazsorban44 will help us

@adriangalilea
Copy link

@korzhanov I created a issue with a repro repo now so it stays open and let's us track it, I also reached out to the Dgraph team both on the forum(which seems dead) and their support, who replied previously so hopefully they can chime in as I pointed out both the the Github issue and this very PR.

@adriangalilea
Copy link

Hey @korzhanov I fixed:
id issues, in this PR you are only fixing user creation, but you may have issues with sessions and linking accounts. I linked the repo on the main issue. (code is just proof of concept)

edge runtime issues, which seems to be not related to dgraph.

The JWT algo encoding is outdated and should be ideally updated so that it uses the same that next-auth uses by default, so overall would be simpler, and docs can be trimmed down too, this part should be removed.

Check it out if you wish, I think we can fix it fully, I may be able to also sort the documentation too, would love someone from the Dgraph team to chime in as I mentioned.

@korzhanov Do you want me to create a new PR so I cover all the mentioned issues, or do you prefer to update this one together?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters Changes related to the core code concerning database adapters dgraph @auth/dgraph-adapter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants