SOFA icon indicating copy to clipboard operation
SOFA copied to clipboard

REST API returning fully nested data structure

Open mxcd opened this issue 4 years ago • 7 comments

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

mxcd avatar Jun 04 '21 13:06 mxcd

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.

Rudolf-LF avatar Sep 10 '21 07:09 Rudolf-LF

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 ?

dragma avatar Jan 21 '22 12:01 dragma

Same here! any updates?

Fetching everything on huge graphql schema is overkilling (and causes circular dependencies sometime)

Yehonal avatar Feb 04 '22 18:02 Yehonal

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

Yehonal avatar Feb 05 '22 08:02 Yehonal

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)

Urigo avatar Apr 17 '24 11:04 Urigo

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

Yehonal avatar Apr 17 '24 15:04 Yehonal

We really hope to find a production use case we could make sure it really being solved in a live environment

Urigo avatar Apr 17 '24 18:04 Urigo