Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 34 forks source link

Allow returning GraphQLID type for id fields #16

Closed mattleff closed 6 years ago

mattleff commented 7 years ago

We are using objection-graphql in our API server with Relay on the client side. Relay encourages a global ID for each graph object (Docs & Spec). This aids in object identification for caching, as well as cache invalidation and refreshing. The GraphQL Spec states that an ID should be a string. This prevents simply using an id column from the models, at least without type conversion. Additionally, since the ID needs to be globally unique it would need to include the model name/class (i.e., 1Movie, 2Person) to ensure that for a given database id number the generated GraphQL ID is unique.

I'm wondering if you would be open to either of the following solutions:

  1. Making a special case for fields named id that would return the GraphQLID type.
  2. Making an optional method to override the type for a property, something like:
const graphQlSchema = graphQlBuilder()
  .model(Movie)
  .model(Person)
  .model(Review)
  .overridePropertyType( (modelClass, propName, defaultType ) {
    return propName === 'id' ? GraphQLID : defaultType;
  } )
  .build();
koskimas commented 7 years ago

I'm open to option 2. I don't think this should be enforced.