REST API returning fully nested data structure
I am facing a problem concerning the depthLimit setting:
I implemented an API template using Apollo GraphQL: https://github.com/mxcd/api-template
There I am using sofa like so:
https://github.com/mxcd/api-template/blob/master/src/index.ts#L25
app.use('/api', useSofa({
basePath: '/api',
schema,
depthLimit: 1
}));
The GraphQL application is deployed here: https://swapi.mxcd.de/graphql
Requesting a film via REST (https://swapi.mxcd.de/api/film/1), I get a huge result set although the depthLimit is set to 1
Is this the expected behavior? I would expect to only receive a film JSON with no additional relations resolved but links indicated via IDs
I noticed this too. Using Express server.
Tried:
app.use('/api', useSofa({
basePath: '/api',
schema,
depthLimit: 1
}));
and
app.use('/api', useSofa({
basePath: '/api',
schema,
depthLimit: 0
}));
also tried:
app.use('/api', useSofa({
basePath: '/api',
schema,
ignore: ['Visit.business']
}));
but the nested data are still retrieved for the REST endpoint.
Maybe worth to mention that I changed the method to POST.
I have the same problem here.
Making one query for a single Node completely breaks node and my database due to heavy calculation of every related nested nodes.
Plus, nested data types implies also nested required parameters for that data, like in @mxcd example in the url he provided https://swapi.mxcd.de/api/film/1.
It asks for
- filmSpeciesLimit
- filmSpeciesOffset
- filmSpeciesSpeciesHomeworldHomeworldResidentsLimit
- filmSpeciesSpeciesHomeworldHomeworldResidentsOffset
- filmSpeciesSpeciesHomeworldHomeworldResidentsResidentsStarshipsLimit
- filmSpeciesSpeciesHomeworldHomeworldResidentsResidentsStarshipsOffset
- ... and so on
In fact, I couldn't even make a request on his REST API
@mxcd @Rudolf-LF, have you found any solutions ?
Same here! any updates?
Fetching everything on huge graphql schema is overkilling (and causes circular dependencies sometime)
For now I'm using the graphql-tools to remove nested fields. It's an hack but it does the job:
const includeTypes = [
'String',
'Float',
'Boolean',
'Int',
// Custom scalars
'DateTime',
'JSON',
];
const schema = wrapSchema({
schema: originalSchema,
transforms: [
new FilterObjectFields((_typeName, _fieldName, _fieldConfig) => {
const type = originalSchema.getType(
//Removes all the extra characters from the type, e.g.: [String]! becomes String
//TODO improve this logic
_fieldConfig.type.toString().replace(/[\[\]\!]/g, ''),
);
const isOperation = _typeName === 'Query' || _typeName === 'Mutation';
const isObjectType =
!!type && !includeTypes.includes(type.toString()) && !isOperation;
const isObjectTypeWithArgs =
isObjectType &&
_fieldConfig.args &&
Object.keys(_fieldConfig.args).length > 0;
const isRelay =
(_fieldName === 'nodes' || _fieldName === 'pageData') &&
!isObjectTypeWithArgs &&
isObjectType;
if ((isObjectType || isObjectTypeWithArgs) && !isRelay) {
return false;
}
return true;
}),
],
});
For now it guesses the nested objects via some "rules". However, combining graphql decorators to instruct SOFA on changing its behaviour could be a good idea imho
Thanks everyone for commenting here We are aiming to tackle this soon Is anyone in this thread willing to go on a call with us, walk us through your current setup and your needs, get a shared Slack channel going with The Guild and test the new versions with us till we get it right? If yes, simply email me (my email is on my GitHub profile)
Thanks everyone for commenting here We are aiming to tackle this soon Is anyone in this thread willing to go on a call with us, walk us through your current setup and your needs, get a shared Slack channel going with The Guild and test the new versions with us till we get it right? If yes, simply email me (my email is on my GitHub profile)
I'm willing to help with this! Although I'm not actively working on a project using sofa anymore. But I will very soon
We really hope to find a production use case we could make sure it really being solved in a live environment