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

relations with arguments not showing up on model #46

Closed
chrisdrackett opened this issue Jun 30, 2019 · 9 comments · Fixed by #86 or #90
Closed

relations with arguments not showing up on model #46

chrisdrackett opened this issue Jun 30, 2019 · 9 comments · Fixed by #86 or #90
Labels
bug Something isn't working

Comments

@chrisdrackett
Copy link
Collaborator

I have a schema that looks like the following:

type User {
  createdAt: DateTime!
  email: String!
  followers(after: String, before: String, first: Int, last: Int, orderBy: UserOrderByInput, skip: Int, where: UserWhereInput): [User!]
  following(after: String, before: String, first: Int, last: Int, orderBy: UserOrderByInput, skip: Int, where: UserWhereInput): [User!]
  id: ID!
  items(after: String, before: String, first: Int, last: Int, orderBy: ItemOrderByInput, skip: Int, where: ItemWhereInput): [Item!]
  itemsCreated(after: String, before: String, first: Int, last: Int, orderBy: ItemOrderByInput, skip: Int, where: ItemWhereInput): [Item!]
  name: String!
  projects(after: String, before: String, first: Int, last: Int, orderBy: ProjectOrderByInput, skip: Int, where: ProjectWhereInput): [Project!]
  updatedAt: DateTime!
}

but after running mst-gql I get the following:

/**
 * UserBase
 * auto generated base class for the model UserModel.
 */
export const UserModelBase = MSTGQLObject
  .named('User')
  .props({
    __typename: types.optional(types.literal("User"), "User"),
    createdAt: types.maybe(types.frozen()),
    email: types.maybe(types.string),
    id: types.identifier,
    name: types.maybe(types.string),
    updatedAt: types.maybe(types.frozen()),
  })
  .views(self => ({
    get store() {
      return self.__getStore<typeof RootStore.Type>()
    }
  }))

export class UserModelSelector extends QueryBuilder {
  get createdAt() { return this.__attr(`createdAt`) }
  get email() { return this.__attr(`email`) }
  get id() { return this.__attr(`id`) }
  get name() { return this.__attr(`name`) }
  get updatedAt() { return this.__attr(`updatedAt`) }
}
export function selectFromUser() {
  return new UserModelSelector()
}

export const userModelPrimitives = selectFromUser().createdAt.email.name.updatedAt

I found that if I remove the args MST picks things up correctly. For example if I change followers to be followers: [User!] I then see it correctly in my store:

followers: types.optional(types.array(MSTGQLRef(types.late((): any => UserModel))), []),

I don't actually use these args, so I can remove them from the generated schema.graphql but I would need to setup a script or similar to do this every time the file is generated.

@elie222 elie222 added the bug Something isn't working label Jul 9, 2019
@beepsoft
Copy link
Collaborator

I run into the same issue. It seems that resolveFieldsAndImports() in generate.js explicitly filters out fields with arguments.

.filter(field => field.args.length === 0)

Is this necessary, is there any reason for this behaviour? If so, then can this be made optional?

@chrisdrackett
Copy link
Collaborator Author

chrisdrackett commented Aug 30, 2019

I'm not sure if there is a reason behind this (@mweststrate?) and haven't had a chance to dig in myself yet.

@beepsoft
Copy link
Collaborator

Until someone can clarify this I added a --allowFieldArgs option to the generator and created a pull request in case this turns out to be useful.

@chrisdrackett
Copy link
Collaborator Author

@beepsoft does it seem to work without issue with args?

@beepsoft
Copy link
Collaborator

Yes, in my app it seems to generate the approriate definitions and when querying via the store it fills these properties correctly. In my cases these are arrays of objects. If the type is specified as root with --root Foo

then I get

bar: types.optional(types.array(MSTGQLRef(types.late(() => FooModel))), [])

otherwise

bar: types.optional(types.array(types.late(() => FooModel)), [])

I think that's how it is supposed to work.

@chrisdrackett
Copy link
Collaborator Author

If this is the case I see no reason not to make it the default :)

@beepsoft
Copy link
Collaborator

Yep. :-)

@beepsoft
Copy link
Collaborator

beepsoft commented Sep 7, 2019

@chrisdrackett Can you please fix this by deleting that filter line?

@chrisdrackett
Copy link
Collaborator Author

I’m currently out of town. The earliest I could get to it is Tuesday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants