Graphql-js: Question: When would info.fieldNodes.length > 1 ?

Created on 24 Nov 2016  路  5Comments  路  Source: graphql/graphql-js

What would a query and/or schema look like for the resolve argument info.fieldNodes.length > 1 (formaly info.fieldASTs)?

I can't find any reference to this and almost always see people doing info.fieldNodes[0] in resolve functions!

question

Most helpful comment

If you see people writing info.fieldNodes[0] in resolve functions, then they will fail to properly support fragments that introduce overlapping fields!

All 5 comments

query OverlappingFieldsExample {
  fieldA { 
    fieldB {
      fieldC
    }
  }
  ...FragX
}

fragment FragX on Query {
  fieldA {
    fieldB {
      fieldD
    }
    ...FragY
  }
}

fragment FragY on TypeA {
  fieldB {
    fieldE
  }
}

If you were to look at info.fieldNodes in the fieldA field, you would see that info.fieldNodes.length === 2 and in fieldB info.fieldNodes.length === 3

This would produce a result like:

{
  "data": {
    "fieldA": { 
      "fieldB": {
        "fieldC": "valueC",
        "fieldD": "valueD",
        "fieldE": "valueE"
      }
    }
  }
}

If you see people writing info.fieldNodes[0] in resolve functions, then they will fail to properly support fragments that introduce overlapping fields!

Thanks for clearing that up!

I updated the graphql version to 0.9.1 the info.fieldNodes value is coming as undefined..

resolve(root, args, info, context) {
console.log(info.fieldNodes)
....}

can someone please advise on this

But it seems like code like this is ok:

https://github.com/apollographql/graphql-tools/blob/master/src/stitching/getResponseKeyFromInfo.ts#L9

Because overlapping fragments will only overlap if they have the same (or no) alias?

And presumably identical arguments?

https://github.com/graphql/graphql-js/blob/14f260bb91fe58416e038fb444301adb405c24ae/src/execution/execute.js#L691

Was this page helpful?
0 / 5 - 0 ratings