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

Bring from mongodb only the fields specified in GraphQL #16

Open
rvizzari opened this issue Dec 6, 2019 · 1 comment
Open

Bring from mongodb only the fields specified in GraphQL #16

rvizzari opened this issue Dec 6, 2019 · 1 comment
Assignees

Comments

@rvizzari
Copy link
Contributor

rvizzari commented Dec 6, 2019

No description provided.

@rvizzari
Copy link
Contributor Author

rvizzari commented Dec 6, 2019

Improvement description:

Currently when a graphQl query is executed we are going to mongodb to get this data, but we are bring all the document properties from mongo.
A good improvement should be use the .select() function from mongoose only asking for the specified properties in graphQL query

e.g.

NOW:

graphQL query:

{
  type {
        propertyOne
        propertyTwo
  }
}

Mongo Query:
return Model.find({})

IMPROVEMENT:

Mongo Query:

return Model.find({}).select('propertyOne propertyTwo')

In this way we can make more effective and performant queries

POSSIBLE SOLUTION:

In the graphql query resolve() method, the forth parameter info:

type GraphQLFieldResolveFn = (
  source?: any,
  args?: {[argName: string]: any},
  context?: any,
  info?: GraphQLResolveInfo
) => any

contains more info related about our GraphQL query

I have been playing with this parameters and the fields requested in the graphql query are there and if we catch them this code works fine 😀😀😀:

async resolve (_, _, _, info) {
        const requestedFields = info.fieldNodes[0].selectionSet.selections.map(selection => selection.name.value).join(' ')
        const result = type.model.find({}).select(requestedFields)
        return result
}

I'll leave this here just to not forget and resume this later in a new branch.

@rvizzari rvizzari self-assigned this Dec 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant