Comments (15)
try this in your type definition:
{
// this will create a hole where id will still be used to create the spine.
where: () => 'FALSE',
}
from join-monster.
Hmm doesn't seem to work.
{
Relationship: {
sqlTable: 'relationships',
uniqueKey: 'id',
fields: {
otherUser: {
where: () => 'FALSE',
resolve: () => ({ id: 222 })
}
}
}
}
from join-monster.
This is what type looks like.
type Relationship {
id: Int
state: String
otherUser: User
}
from join-monster.
@tonyxiao for fields you can just define the resolver yourself -- as long as sqlColumn isn't defined on the field -- it won't attempt to fetch that column. Remove the where.
from join-monster.
@tonyxiao I see whats happening -- I stared at your code a little bit longer. Let me see if I can find a way to bypass the assumption join monster is making here in your use case.
from join-monster.
@dreamcodez That works for scalar fields, but not for object fields.
from join-monster.
Works for scalars
https://github.com/stems/join-monster/blob/ec920cdcb69b6c79a72ff3325b712ca1810f1244/src/query-ast-to-sql-ast/index.js#L107
Does not work for objects
https://github.com/stems/join-monster/blob/ec920cdcb69b6c79a72ff3325b712ca1810f1244/src/query-ast-to-sql-ast/index.js#L95
from join-monster.
@tonyxiao Yeah this seems like it will be a common use case. Just because as ObjectType is tagged with sqlTable does not necessarily mean it should be handled by JM in all fields in the schema. We need an option to opt-out. I'll think about the best way to add this in for the next release.
For now, you can fork and monkey-patch it in. You can add a property like so
{
Relationship: {
sqlTable: 'relationships',
uniqueKey: 'id',
fields: {
otherUser: {
// tell joinMonster this is a no-op
jmIgnore: true,
resolve: () => ({ id: 222 })
}
}
}
}
And then you can open query-ast-to-sql-ast/index.js
and go to line 53 and add the following block.
if (field.jmIgnore) {
sqlASTNode.type = 'noop'
return
}
This will tell joinMonster's AST to not compile any SQL for that branch of the schema.
Keep in mind that the code in the src/
directory is compiled by babel so changes to the code need to eventually be built and propagated to dist/
.
from join-monster.
@acarl005 actually it's still useful to be able to specify stuff like sqlDeps
. The columns here can actually be important for resolutions of custom objects.
{
sqlTable: 'relationships',
uniqueKey: 'id',
fields: {
otherUser: {
sqlDeps: [ 'user_id', 'target_id' ],
}
}
}
from join-monster.
Hm. I'm a little bit confused. You say you want a "completely custom resolver for the child." I assume that means you want to hit the network on your own. By "completely custom," are you saying you'll be writing your own SQL query? If so, joinMonster
won't be querying that table at that level in the schema so specifying sqlDeps
can't really be done. You will have to inspect the resolveInfo
on your own and execute your own logic for whether or not to include certain columns in your custom query.
On the other hand, maybe thats not what you mean. Maybe you still want Join Monster to join on the user table again and get the data, and then run a resolve
function on the data it brings back. If thats the case, its already possible. Even if joinMonster
fetches a field for you, it won't cause any resolver to be ignored. It just allows you to omit it in many cases.
from join-monster.
@acarl005 in addition to having join-monster complete ignore an object field (which should totally be an option), what I meant to say is that it's also useful to have computed object fields, not just computed scalar fields, that have depend on other fields. (Think the fullname example).
In my particular case, I want to retrieve otherUser
object based on the values of user_id
and target_id
columns, and I want to make sure those values are available on the parent
argument in the resolver.
from join-monster.
Ah okay now I understand. You do want to write your own resolver, but that resolver still needs access to columns from the parent object. Yes, I agree, this is an important capability! I'll get this into the next release. Or I'll happily accept a PR if its more urgent.
from join-monster.
Correct! Will try my best to submit a PR next week. By the way (let me know if this is better as a separate issue), will join monster batch across multiple calls in the same request? Here's an example
type User {
pictures: [Picture]
}
type Picture {
url: String
}
type RootQuery {
searchUsers(term: String): [User]
}
// via graphql-tools
const RootQuery = {
searchForUsers: () => [...] // Do something to retrieve list of matching user ids from outside of SQL
}
const User = {
sqlTable: 'users'
fields: {
picture: {
sqlJoin(userTable, picturesTable) {
return `${userTable}.id = ${picturesTable}.user_id`
},
resolver(parent, args, context, resolveInfo) {
return joinMonster(resolveInfo, {}, sql => {
return knex.raw(sql)
})
}
}
}
}
const Picture: {
sqlTable: 'pictures'
}
and I make the following graphql query
{
searchUsers(term: "hello") {
picture {
url
}
}
}
Is it possible for joinMonster to batch the requests like the following instead of one request per user?
select url from pictures
inner join users
on pictures.user_id = users.id
where users.id IN ($idsRetrievedFromSearch)
from join-monster.
Yes, you can do this with sqlBatch
. Here is the section on this in the documentation.
from join-monster.
Newest version includes two new field options: jmIgnoreAll
and jmIgnoreTable
. jmIgnoreAll
will ignore all the join monster properties. jmIgnoreTable
will ignore only the sqlTable
tag on the object type, allowing you to specify sqlDeps
instead.
This isn't in the documentation yet but I'll update that soon.
from join-monster.
Related Issues (20)
- Update to compat version of relay
- Missing results when using batch HOT 2
- sqlPaginate always adds total aggregation even if it's not even used
- Typescript type errors HOT 1
- Link to demo does not work HOT 5
- tsd failing locally HOT 2
- Fails if I extend GraphQLObjectType HOT 1
- Must call joinMonster in a resolver on a field where the type is decorated with \"sqlTable\" HOT 9
- Cannot read properties of undefined (reading 'sqlTable') HOT 2
- Failing type defs
- Issue with GlobalIDs not transforming back to "native" uuid4 IDs
- bug in joinMonster.getNode(): def.args not iterable HOT 4
- Querying the same relation multiple times as aliases with different args results in wrong data HOT 5
- Remove requirement for uniqueKey to be defined
- Pagination with dynamic sorting does not support null values. HOT 1
- joinMonster creating invalid MySQL sql HOT 1
- Unsupport many-to-many relation on graphql v15.8 and join-monster v3.1.1 using postgres? HOT 1
- Batching JoinMonster calls
- The HTTP server example code is broken due to deprecation of kao-graphql HOT 1
- join-monster fails to work in a react native deployment HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from join-monster.