mobxjs/mst-gql

Root type identifier

chertkovalex opened this issue · 3 comments

As mentioned in documentation,

it is recommended that every uniquely identifyable concept in your application, does have an id field of the graphQL ID type. By default, any object types for which this is true, is considered to be a "root type"

Our schema:

type DemoMessage {
  key: ID!
  id: Float!
}

Is generated model.base:

    key: types.identifier,
    id: types.union(types.undefined, types.number),

For specific business purposes we can't use an id field as an identifier. In our case, we have a filed called key which is an identifier, but the object is not considered as a root type. Is there any way to specify that identifier field should not only to be an id for detecting root types?

Unfortunately id is hard coded and the runtime only looks for this field in the graphql query results to identify the objects.

That might be something we can change. I'll investigate as I want mst-gql to be as backend-agnostic as possible

That would be great.

One thing that also worth considering is not to stick to requiring the id field to be of type ID. In case of Hasura the id fields will be a scalar type named after the postgres type of the id field, eg. bigint or uuid. --fieldOverrides seems to provides a solution for this, but it is a generic and not related to id-, which need specific attention as the automatic root type detection algorithm looks for specifically the field id and ID! type. So --fieldOverrides only works together with --roots in case of id field overrides.

function autoDetectRootTypes() {
return types
.filter(
(type) =>
objectTypes.includes(type.name) &&
type.fields.some(
(field) =>
field.name === "id" &&
skipNonNull(field.type).kind === "SCALAR" &&
skipNonNull(field.type).name === "ID"
)
)
.map((t) => t.origName)
}

Moreover it maybe useful to allow specifying id field name per root entity as different entities may have different id fields, eg. some called id others called key, etc.