aerogear / offix

GraphQL Offline Client and Server
https://offix.dev
Apache License 2.0
758 stars 45 forks source link

Datastore Hooks Generation #765

Open Eunovo opened 4 years ago

Eunovo commented 4 years ago

We want to generate hooks for Models, see example below;

export const useFindTodos = (filter?: Filter<Todo>) => useQuery(TodoModel, filter);

The problem here is that to generate the above hook, we need to generate TodoModel but to generate TodoModel, we need to generate code to initialise DataStore like this

export const datastore = new DataStore({
  dbName: "offix-datasync",
  replicationConfig: {
    client: {
      url: "http://localhost:5400/graphql",
      wsUrl: "ws://localhost:5400/graphql",
    }
  }
});

export const TodoModel = datastore.setupModel<Todo>(schema.Todo);

The problem here is that the user will need to edit this config. We don't want users to edit any generated file. How do we save users from boilerplate code without generating the config?

Possible Solutions

There was a suggestion to generate something like this

let TodoModel
let UserModel
export function createModels(datastore: DataStore) {
   TodoModel = datastore.setupModel<Todo>(schema.Todo);
   UserModel = datastore.setupModel<User>(schema.User);
}

export { TodoModel, UserModel }; 

This can work. Users will have to do something like this

import {  createModels, schema } from "./generated"; 

const datastore = ... // create datastore

// if users want to edit schema, they'll have to do it here

createModels(datastore);

export * from "./generated";

@wtrocki @kingsleyzissou

wtrocki commented 4 years ago

Yep. That will work. Not priority for MVP though