os-guild / graphql-to-mongodb Goto Github PK
View Code? Open in Web Editor NEWAllows for generic run-time generation of filter types for existing graphql types and parsing client requests to mongodb find queries
Allows for generic run-time generation of filter types for existing graphql types and parsing client requests to mongodb find queries
I have provided a resolver to my subtype , but by doing so I get the above warning when I start.
the type schema is as :
RootType { // this as the mongoargs/resovler directives
...
Trade { // this is of type t_tradeType in the schema
...
TradeId : [{ id: "aaa", idtype: "added"}] // basically TradeId is an array of id+idtype object
...
anotherField
...
}
...
}
when I add a resolver on TradeId ( I need be able to pick the element of array and filter the rest) I get the warning on the subject. The issue I see is that the parent now is not of t_tradeType but some child field of Trade. If I remove the @mongQueryArgs/@mongoqueryResolver from the root type.. it works as expected.
t_tradeType: {
TradeId: (parent, args,,) => {
console.log("PARENT %j",parent); /// this somehow logs{ anotherField: "xxxxx" }
... FILTER LOGIC
... return new array with ONLY the element I need
}
}
But anotherField is not the parent of TradeId, its a sibling.. why is it being passed as the parent.
Appriciate the help.
Hello. First of all, thank you very much for creating this useful library. I would like to know how can I use this library with type-graphql? More precisely, I don't know where I can find second argument for getMongoDbProjection. As I understand, it's generated by type-graphql under the hood.
Everything I need is just to create projection from GraphQL query info.
Thanks.
I'm trying to integrate this package into an existing NodeJS service wich has the schema definition written in some .graphql files. However i'm having some troubles making the resolvers run :/ I've setup a small example project wich describe the actual situation I'm facing, you can find the repository here
https://github.com/GimignanoF/GraphQLFilters
Essentially i have setup two queries. One is using the approach I'm currently using, wich works just fine. The other one tries to use this package, however when i run that query i get this error as GraphQL result
Expected Iterable, but did not find one for field RootQuery.pages.
So, is there any way I can use this package in this scenario? Or should i rewrote the entire schema (wich is quite big) in the source code, rather then parsing it from the .graphql files?
Hi, can you please provide an example of how to setup the connection to the database or how you do it? a middleware that returns the connected client and the db instance in the context? this is what I did
Before I dig too deep, I was wondering if this library supports simple relationships, like a book with an authorId - can I pull an author object back on a book query?
Also, is there a better place to ask questions like this that aren't issues? I couldn't find anything on Stackoverflow. Do you guys have a gitter room, slack room, or forum?
export default {
type: new GraphQLList(propertyType),
args: getGraphQLQueryArgs(propertyType),
resolve: getMongoDbQueryResolver(
propertyType,
async (filter, projection, options, obj, args, context) => {
console.log(filter);
console.log(projection);
console.log(options);
return await db.collection('properties').find(filter, projection, options).toArray();
}
)
};
What console.log gives
{ Class: { '$eq': 'RESIDENTIAL' } }
{ _id: 1, City: 1, Class: 1 }
{ sort: { _id: 1 }, limit: 5, skip: 5 }
What my query is:
query{
properties(
filter: {
Class: {EQ: "RESIDENTIAL"}
},
sort: {_id: ASC},
pagination: { limit: 5, skip: 5 }
) {
_id
City
Class
}
}
I get everything back. I have well over 1000+ records.
I was wondering if there was a way to integrate this with NestJS? I found myself implementing the features manually until I ran into your project. I tried to translate your code into the way NestJS is implementing their code, but ran into issues. Would very much appreciate any pointers you have.
I am trying to make things more simple and was wanting to get way form using the the plain graphql language and use graphql-tools from Apollo.
Say I want to get a specific person record and filter by _id
person (
filter: {
_id: { EQ: "5e1e2d653a32a05f51f621c1" }
}
) {
name {
lastName
}
age
}
Currently that will generate a filter like so
{ _id: { '$eq': '5e1e2d653a32a05f51f621c1' }
Can you detect when "_id" is used and make it essentially do this instead?
{ _id: { '$eq': new ObjectId('5e1e2d653a32a05f51f621c1') }
projection logs out {}
here for any query, e.g.
query {
viewer {
id
firstName
fullName
}
}
const viewer = {
type: ViewerType,
resolve: getMongoDbQueryResolver(
ViewerType,
async (_filter, projection, options, _a, _b, ctx: Context) => {
console.log(projection);
if (ctx.userId) {
return await User.findOne({ _id: ctx.userId }, projection, options);
}
}
)
};
Any chance you can give an example on how to use mutations? such as insert, create, etc
README.md describes an aspect of the main use case as:
To implement, we'll define the peron query field in our GraphQL scheme like so:
people: { ... }
I assume "peron" was intended to be written as "person", but would you please describe a little bit more about this "person query field" and why it is named "people" as opposed to "person"? Thanks!
Hey,
Is there an implementation for inserts?
Yes? Could you show an example?
No? Should I take a shot at it?
Otherwise it works quite well so far :)
I think there is some kind of caching issue.
The FilterTypes seem to get cached and when I change the fields in the schema (and create a new schema), I still get the old FilterTypes. Only when I restart the app and the schema is created from scratch (empty cache in graphql-to-mongodb) I get the correct FilterTypes.
Running clearTypesCache()
before creating the schema helps. Not sure if this is the way it should work?
How could this be used in Apollo.
person: {
type: new GraphQLList(PersonTypeExpanded),
args: getGraphQLQueryArgs(PersonType),
resolve: getMongoDbQueryResolver(PersonType,
async (filter, projection, options, source, args, context) =>
await context.db.collection('person')
.find(filter, projection, options).toArray()
)
}
This returns cannot read proprety 'resolve' of undefined
when using person( filter: personType, sort: personType): [personTypeExpanded]
, and trying to retrieve a field defined in personTypeExpanded
.
Is there way to support custom projections? Say I had the following GraphQL schema:
const viewer = new GraphQLObjectType({
name: 'Viewer',
description: 'The logged in user',
fields: {
id: {
type: new GraphQLNonNull(GraphQLID)
},
firstName: {
type: new GraphQLNonNull(GraphQLString),
},
lastName: {
type: new GraphQLNonNull(GraphQLString),
},
fullName: {
type: new GraphQLNonNull(GraphQLString)
}
}
});
But my MongoDB schema has User.name.first
and not User.firstName
. Is there a way to map these?
I needed it for my project and added it to my git clone of this project so I can do the following...
{
"$or": [
{
"payload.fileInfo.size": {
"$exists": true
}
},
{
"payload.fileInfo.size": {
"$gt": "12345"
}
}
]
}
Every leaf type has an opr: exist | not exist and I removed the deprecated opr. I think its a good feature to have for people.
Good evening, the idea they propose seems fantastic to me. I am using the module, my only problem is that the type filters are not loading the fields of the referenced types. I would appreciate if you can help me with this problem. Regards
If using a Union type field together with the getMongoDbQueryResolver
GraphQL schema validation fails with the error:
TypeError: graphQLType.getFields is not a function
at ...\node_modules\graphql-to-mongodb\lib\src\common.js:38:38
Does this support filtering on DateTime fields? Will be nice to add to example.
I'm trying to figure out the syntax for using OR and AND in queries. Do you have a simple example?
I'm trying to build it to test but get this message...
src/mongoDbFilter.ts:108:68 - error TS2345: Argument of type 'GraphQLObjectFilter | GraphQLLeafFilter' is not assignable to parameter of type 'GraphQLObjectFilter'.
Type 'GraphQLLeafFilter' is not assignable to type 'GraphQLObjectFilter'.
Types of property 'opr' are incompatible.
Type 'MongoDbLeafOperators' is not assignable to type '"exists" | "not_exists"'.
Type '"$eq"' is not assignable to type '"exists" | "not_exists"'.108 const nestedFilter = parseMongoDbFilter(fieldType, fieldFilter, ...excludedFields);
I'm new to graphQL and got the simplest server up and running, but I've been trying to get the Apollo server working because I want to use GraphQLPlayground instead of GraphIQL - https://github.com/apollographql/apollo-server.
Do you guys have an example of graphql-to-mongodb working with the Apollo server? I'm having some trouble getting the two to play nicely together.
Can we integrate with mongoose and SDL with Apollo Server?
Hi
I just read through your docs and think a simple example repo would be great to have a quick start to see how your package is used on a server. Thanks
I don't see a basic contains or like filter for strings. Is this present or on the roadmap?
e.g.
document with description = "A very fun toy"
filter: {
description: { CONTAINS: "toy" }
}
...should return the document.
It would be nice to have "StartsWith" or "EndsWith", or even RegEx functionality.
Hi,
Would you add example with Apollo Server 3 new directive syntax?
https://www.graphql-tools.com/docs/schema-directives#declaring-schema-directives
Thanks.
Possible to return the total doc count so I can setup client side pagination?
{
properties(filter: {
SystemID: {EQ: 457397}
Photos: {
Sequence: {
EQ: 0
}
}
}
) {
_id
Photos {
Sequence
}
}
}
Photos is part of another GraphQLObjectType as I don't know of a way to have nested fields.
export default new GraphQLObjectType({
name: 'Photos',
fields: {
MediaID: { type: GraphQLInt },
MediaTypeID: { type: GraphQLInt },
Sequence: { type: GraphQLInt },
MediaUrl: { type: GraphQLString },
Caption: { type: GraphQLString },
Description: { type: GraphQLString },
FileSize: { type: GraphQLInt },
Width: { type: GraphQLInt },
Height: { type: GraphQLInt },
Duration: { type: GraphQLInt },
CreateDate: { type: GraphQLString },
UpdateDate: { type: GraphQLString },
},
});
My Properties:
export default new GraphQLObjectType({
name: 'Property',
fields: {
_id: {
type: new GraphQLNonNull(GraphQLID)
},
SystemID: { type: GraphQLInt },
Class: { type: GraphQLString },
PropertyType: { type: GraphQLString },
Area: { type: GraphQLString },
SearchPrice: { type: GraphQLInt },
ListingPrice: { type: GraphQLInt },
AddressNumber: { type: GraphQLString },
...... more fields between .....
DocTimestamp: { type: GraphQLString },
Photos: {
type: GraphQLList(photoType),
},
},
});
So filter will auto populate the Photos. Sort will not. While you can populate the field, the filter operations doesn't work. It will return everything in the array.
I don't know if this was intended or not. I was looking to sort out my Photos and/or filter them being a nested object.
The photos are embedded into the Mongodb document itself, so they aren't in a separate collection. I was trying to save from having to make multiple calls to each of the properties for photos. Meaning I have over 3,000 property listings. So that would have to return 3,000 database calls to grab the photo without caching / data loader.
On my listings page, I only wanted to return the photo with sequence 0, on the details page, I want to return only the single property, with all the photos to that property. (real estate project).
I supposed I could use a different method of handling this, but the issue to report here, is FILTER will auto populate the nested list, no errors. Sort will not auto populate the nested list.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.