neo4j / graphql

A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations.
https://neo4j.com/docs/graphql-manual/current/
Apache License 2.0
498 stars 147 forks source link

OGM returned types #2718

Open Tanner-Scadden opened 1 year ago

Tanner-Scadden commented 1 year ago

Is your feature request related to a problem? Please describe. Types returned from the OGM do not match the selection set.

Describe the solution you'd like A way for us to have types generated either through the selection set given instead of the full type.

Ex:

type Form {
  id: ID! @id 
  name: String!
}

const forms = await ogm.models("Form").find({ selectionSet: `{ id }` })
// Request: typeof forms === Pick<Form, 'id'>[]
// Current: typeof forms === Form[];

Describe alternatives you've considered Could also add a way to pass in a selection object that mirrors the selection set, similar to a traditional ORM. However this would limit the ability to pass in arguments into nested selects.

Additional context Not sure if this is possible, but would add a lot of power to the OGM, especially when people want to use it in a restful or rpc API.

mjfwebb commented 1 year ago

A spontaneous thought is that we could theoretically allow passing a generic to the .find method, so that the user could describe the type they would expect back. It would look something like this:

const forms = await ogm.models("Form").find<{ id: string }>({ selectionSet: `{ id }` })

Though I agree having it infer the resulting type based on the input selection set would be really nice!

Tanner-Scadden commented 1 year ago

That would be a good starting point, especially if it can compare the generic to the original type to ensure it's possible!