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

How would I query with UUID id types? #67

Closed
jocubeit opened this issue Jun 12, 2016 · 11 comments
Closed

How would I query with UUID id types? #67

jocubeit opened this issue Jun 12, 2016 · 11 comments

Comments

@jocubeit
Copy link

jocubeit commented Jun 12, 2016

I have an existing schema that uses UUIDs for table IDs. I'm finding I get an error when trying to query a field.

{
  authorizations(id: "40fbe19f-1500-4ff7-a2b1-f5d2108a30d1") {
    algorithm
    expiresAt
    issuedAt
    issuerId
    realm
  }
}
{
  "data": {
    "authorizations": null
  },
  "errors": [
    {
      "message": "Invalid ID '40fbe19f-1500-4ff7-a2b1-f5d2108a30d1'.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "stack": "Error: Invalid ID '40fbe19f-1500-4ff7-a2b1-f5d2108a30d1'.\n    at exports.fromID.encodedString (/usr/local/lib/node_modules/postgraphql/dist/graphql/types.js:48:27)\n    at /usr/local/lib/node_modules/postgraphql/dist/graphql/query/createSingleQueryField.js:47:39\n    at /usr/local/lib/node_modules/postgraphql/dist/graphql/resolveTableSingle.js:107:22\n    at next (native)\n    at step (/usr/local/lib/node_modules/postgraphql/dist/graphql/resolveTableSingle.js:17:191)\n    at /usr/local/lib/node_modules/postgraphql/dist/graphql/resolveTableSingle.js:17:451\n    at /usr/local/lib/node_modules/postgraphql/dist/graphql/resolveTableSingle.js:17:99\n    at /usr/local/lib/node_modules/postgraphql/dist/graphql/resolveTableSingle.js:116:18\n    at resolveOrError (/usr/local/lib/node_modules/postgraphql/node_modules/graphql/execution/execute.js:457:12)\n    at resolveField (/usr/local/lib/node_modules/postgraphql/node_modules/graphql/execution/execute.js:448:16)\n    at /usr/local/lib/node_modules/postgraphql/node_modules/graphql/execution/execute.js:256:18\n    at Array.reduce (native)\n    at executeFields (/usr/local/lib/node_modules/postgraphql/node_modules/graphql/execution/execute.js:254:50)\n    at executeOperation (/usr/local/lib/node_modules/postgraphql/node_modules/graphql/execution/execute.js:196:10)\n    at /usr/local/lib/node_modules/postgraphql/node_modules/graphql/execution/execute.js:114:13\n    at new Promise (/usr/local/lib/node_modules/postgraphql/node_modules/core-js/library/modules/es6.promise.js:193:7)"
    }
  ]
}

GraphiQL has no problem generating the Queries and Mutations.

Awesome project btw.

@ferdinandsalis
Copy link
Contributor

Hi @JurgenJocubeit, see this issue here — #42. The id field argument generated by PostGraphQL expects the id generated from the GraphQL Type name and the primary keys of the table. Hope this clears up the confusion. Let me know.

@jocubeit
Copy link
Author

jocubeit commented Jun 13, 2016

I read the thread you suggested and I'm sorry this is all new to me; and I still don't follow. Can I not use UUID primary key types? If I can, how would I query for a single record given an id?

I have a vague feeling that what you're saying is that it isn't possible because the id field is accessed via rowID and the new ID is a concatenation of the type and id for Relay compatibility? Can you please help explain this to me... I've only just been exposed to GraphQL and Relay and come from a Rails/ActiveRecord/PostgreSQL/REST background.

@ferdinandsalis
Copy link
Contributor

I am pretty new to all of this too ☺️ So I will try my best to explain. Yes, you are right that the id is generated from the primary key (which is accessible as rowID on the type) and the type name — see this createTableType.js#L60-L63 and types.js#L33. And yes, Relay uses this id to refetch an object which it already has partially fetched.

So from my understanding you can still use UUID as primary keys however you would query the field with the generated id.

@jocubeit
Copy link
Author

Thank you for your responses @ferdinandsalis, I'm glad my understanding is ok. I'll keep playing around, I think since I have taken an existing database which was used for traditional AR access in a REST-based system, I might be missing some fundamental knowledge on how to query. Even though this project generates the queries and mutations, I think I could benefit from some pg functions to make querying easier.

@calebmer
Copy link
Collaborator

Hi @JurgenJocubeit, Relay requires that our ids be globally unique so we can always refetch a node no matter the type. Not everyone uses UUIDs, most of the time serial ids are used, so we concatenate the table name with the row ID and base64 encode the thing. That's how we get the ids to be used. Just so you know, it's generally not recommended to manually construct your ID.

As a side note, I want to implement fields sometime soon that query by unique constraints, so userByRowId for example. This would allow the exact querying you are trying to do right now.

In the meantime you can implement this exact field by extending PostGraphQL with procedures.

@jocubeit
Copy link
Author

That's exactly what I want. For instance, in my authorizations table I have foreign keys for issuer_id and subject_id, both of which are of type User. At the moment, PostGraphQL doesn't generate fields for these references. So, I guess I have to create some query procedures to return those.

@ferdinandsalis
Copy link
Contributor

@calebmer thanks for jumping in ☺️

On 13 Jun 2016 13:39 +0200, Jurgen Jocubeitnotifications@github.com, wrote:

That's exactly what I want. For instance, in myauthorizationstable I have foreign keys forissuer_idandsubject_id, both of which are of typeUser. At the moment, PostGraphQL doesn't generate fields for these references. So, I guess I have to create some query procedures to return those.


You are receiving this because you were mentioned.
Reply to this email directly,view it on GitHub(#67 (comment)), ormute the thread(https://github.com/notifications/unsubscribe/AAUyKDGbbJ-7QejbbbP8_KXENFDOtkpIks5qLUFXgaJpZM4IzyWZ).

@ferdinandsalis
Copy link
Contributor

@JurgenJocubeit are all your questions answered?

@jocubeit
Copy link
Author

So Automatic Relation Detection as per the README isn't implemented yet; or is implemented but only for Integer primary keys and not UUIDs?

@calebmer
Copy link
Collaborator

No relationship detection for any primary key (compound, Int, string) is implemented. But selecting a single object by its non-global id is not yet supported. And by global id I mean the base64 concatenated version of the table name + the primary key.

@calebmer
Copy link
Collaborator

@JurgenJocubeit check out #70 where I implement the userByRowId fields plus any other fields for unique constraints. Please post comments if it satisfies your use case.

calebmer added a commit that referenced this issue Jun 19, 2016
* feat(graphql): add fields based on unique constraints

Closes #67

* fix: allow arbitrary ordering of constraints
benjie added a commit that referenced this issue Jan 27, 2020
Fix UUID type with `pgExtendedTypes`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants